Skip to content

Commit 3c85396

Browse files
committed
Add feature to set path-based visibility of macro_rules to pub by default
1 parent e49169e commit 3c85396

10 files changed

Lines changed: 15 additions & 5 deletions

File tree

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ declare_features! (
568568
(unstable, macro_attr, "1.91.0", Some(143547)),
569569
/// Allow `macro_rules!` derive rules
570570
(unstable, macro_derive, "1.91.0", Some(143549)),
571+
/// Make `macro_rules!` implicitly pub even without `macro_export`
572+
(unstable, macro_implicit_pub, "1.94.0", Some(148610)),
571573
/// Give access to additional metadata about declarative macro meta-variables.
572574
(unstable, macro_metavar_expr, "1.61.0", Some(83527)),
573575
/// Provides a way to concatenate identifiers using metavariable expressions.

compiler/rustc_lint/src/builtin.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,11 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
12881288
if let hir::ItemKind::Use(_, hir::UseKind::ListStem) = &item.kind {
12891289
return;
12901290
}
1291+
// Do not warn for macro_rules definitions which are pub and unreachable by default,
1292+
// only check their associated use re-exports.
1293+
if let hir::ItemKind::Macro(_, ast::MacroDef { macro_rules: true, .. }, _) = &item.kind {
1294+
return;
1295+
}
12911296
self.perform_lint(cx, "item", item.owner_id.def_id, item.vis_span, true);
12921297
}
12931298

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12991299
let ident = ident.normalize_to_macros_2_0();
13001300
self.r.macro_names.insert(ident);
13011301
let is_macro_export = ast::attr::contains_name(&item.attrs, sym::macro_export);
1302-
let vis = if is_macro_export {
1302+
let vis = if is_macro_export || self.r.tcx().features().macro_implicit_pub() {
13031303
Visibility::Public
13041304
} else {
13051305
Visibility::Restricted(CRATE_DEF_ID)

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,7 @@ symbols! {
13801380
macro_derive,
13811381
macro_escape,
13821382
macro_export,
1383+
macro_implicit_pub,
13831384
macro_lifetime_matcher,
13841385
macro_literal_matcher,
13851386
macro_metavar_expr,

tests/ui/macros/auxiliary/implicit-pub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//@ edition:2018
2+
#![feature(macro_implicit_pub)]
23
pub mod inner {
34
#[allow(unused_macros)]
45
macro_rules! fake_pub {
56
() => {}
67
}
78

8-
#[macro_export]
99
macro_rules! real_pub {
1010
() => {}
1111
}

tests/ui/macros/implicit-pub-still-unreachable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ aux-build:implicit-pub.rs
22
//@ edition:2018
3+
#![feature(macro_implicit_pub)]
34

45
extern crate implicit_pub;
56

tests/ui/macros/implicit-pub-still-unreachable.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0433]: failed to resolve: could not find `fake_pub` in `inner`
2-
--> $DIR/implicit-pub-still-unreachable.rs:7:26
2+
--> $DIR/implicit-pub-still-unreachable.rs:8:26
33
|
44
LL | implicit_pub::inner::fake_pub!();
55
| ^^^^^^^^ could not find `fake_pub` in `inner`

tests/ui/macros/implicit-pub-unreachable-lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@ edition:2018
22
#![crate_type = "lib"]
3+
#![feature(macro_implicit_pub)]
34
#![deny(unreachable_pub)]
45

5-
#[macro_export]
66
macro_rules! not_reexported {
77
() => {}
88
}

tests/ui/macros/implicit-pub-unreachable-lint.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | pub use not_reexported;
88
|
99
= help: or consider exporting it for use by other crates
1010
note: the lint level is defined here
11-
--> $DIR/implicit-pub-unreachable-lint.rs:3:9
11+
--> $DIR/implicit-pub-unreachable-lint.rs:4:9
1212
|
1313
LL | #![deny(unreachable_pub)]
1414
| ^^^^^^^^^^^^^^^

tests/ui/macros/implicit-pub.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ check-pass
22
//@ aux-build:implicit-pub.rs
33
//@ edition:2018
4+
#![feature(macro_implicit_pub)]
45

56
extern crate implicit_pub;
67

0 commit comments

Comments
 (0)