Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ declare_features! (
(unstable, macro_attr, "1.91.0", Some(143547)),
/// Allow `macro_rules!` derive rules
(unstable, macro_derive, "1.91.0", Some(143549)),
/// Make `macro_rules!` implicitly pub even without `macro_export`
(unstable, macro_implicit_pub, "CURRENT_RUSTC_VERSION", Some(148610)),
/// Give access to additional metadata about declarative macro meta-variables.
(unstable, macro_metavar_expr, "1.61.0", Some(83527)),
/// Provides a way to concatenate identifiers using metavariable expressions.
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,11 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
if let hir::ItemKind::Use(_, hir::UseKind::ListStem) = &item.kind {
return;
}
// Do not warn for macro_rules definitions which are pub and unreachable by default,
// only check their associated use re-exports.
if let hir::ItemKind::Macro(_, ast::MacroDef { macro_rules: true, .. }, _) = &item.kind {
return;
}
self.perform_lint(cx, "item", item.owner_id.def_id, item.vis_span, true);
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
let ident = ident.normalize_to_macros_2_0();
self.r.macro_names.insert(ident);
let is_macro_export = ast::attr::contains_name(&item.attrs, sym::macro_export);
let vis = if is_macro_export {
let vis = if is_macro_export || self.r.tcx().features().macro_implicit_pub() {
Visibility::Public
} else {
Visibility::Restricted(CRATE_DEF_ID)
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ symbols! {
macro_derive,
macro_escape,
macro_export,
macro_implicit_pub,
macro_lifetime_matcher,
macro_literal_matcher,
macro_metavar_expr,
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/feature-gates/feature-gate-macro-implicit-pub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ edition:2018
#![crate_type = "lib"]

macro_rules! not_pub {
() => {}
}

pub use not_pub; //~ ERROR: `not_pub` is only public within the crate, and cannot be re-exported outside [E0364]
21 changes: 21 additions & 0 deletions tests/ui/feature-gates/feature-gate-macro-implicit-pub.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0364]: `not_pub` is only public within the crate, and cannot be re-exported outside
--> $DIR/feature-gate-macro-implicit-pub.rs:8:9
|
LL | pub use not_pub;
| ^^^^^^^
|
help: consider adding a `#[macro_export]` to the macro in the imported module
--> $DIR/feature-gate-macro-implicit-pub.rs:4:1
|
LL | / macro_rules! not_pub {
LL | | () => {}
LL | | }
| |_^
help: in case you want to use the macro within this crate only, reduce the visibility to `pub(crate)`
|
LL | pub(crate) use not_pub;
| +++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0364`.
16 changes: 16 additions & 0 deletions tests/ui/macros/auxiliary/implicit-pub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@ edition:2018
#![feature(macro_implicit_pub)]
#[allow(unused_macros)]
macro_rules! fake_pub {
() => {}
}

macro_rules! real_pub {
() => {}
}

pub mod inner {
pub use real_pub;
}

pub use real_pub as real_pub_reexport;
10 changes: 10 additions & 0 deletions tests/ui/macros/implicit-pub-macros2-linted-still.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ edition:2018
#![feature(decl_macro)]
#![crate_type = "lib"]
#![deny(unreachable_pub)]

mod inner {
pub macro m($inner_str:expr) { //~ ERROR: unreachable `pub` item [unreachable_pub]
struct S;
}
}
17 changes: 17 additions & 0 deletions tests/ui/macros/implicit-pub-macros2-linted-still.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: unreachable `pub` item
--> $DIR/implicit-pub-macros2-linted-still.rs:7:5
|
LL | pub macro m($inner_str:expr) {
| ---^^^^^^^^
| |
| help: consider restricting its visibility: `pub(crate)`
|
= help: or consider exporting it for use by other crates
note: the lint level is defined here
--> $DIR/implicit-pub-macros2-linted-still.rs:4:9
|
LL | #![deny(unreachable_pub)]
| ^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

8 changes: 8 additions & 0 deletions tests/ui/macros/implicit-pub-still-unreachable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ aux-build:implicit-pub.rs
//@ edition:2018

extern crate implicit_pub;

fn main() {
implicit_pub::inner::fake_pub!(); //~ error: failed to resolve: could not find `fake_pub` in `inner` [E0433]
}
9 changes: 9 additions & 0 deletions tests/ui/macros/implicit-pub-still-unreachable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0433]: failed to resolve: could not find `fake_pub` in `inner`
--> $DIR/implicit-pub-still-unreachable.rs:7:26
|
LL | implicit_pub::inner::fake_pub!();
| ^^^^^^^^ could not find `fake_pub` in `inner`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0433`.
12 changes: 12 additions & 0 deletions tests/ui/macros/implicit-pub-unreachable-lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ edition:2018
#![crate_type = "lib"]
#![feature(macro_implicit_pub)]
#![deny(unreachable_pub)]

macro_rules! not_reexported {
() => {}
}

mod inner {
pub use not_reexported; //~ ERROR: unreachable `pub` item [unreachable_pub]
}
17 changes: 17 additions & 0 deletions tests/ui/macros/implicit-pub-unreachable-lint.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: unreachable `pub` item
--> $DIR/implicit-pub-unreachable-lint.rs:11:13
|
LL | pub use not_reexported;
| --- ^^^^^^^^^^^^^^
| |
| help: consider restricting its visibility: `pub(crate)`
|
= help: or consider exporting it for use by other crates
note: the lint level is defined here
--> $DIR/implicit-pub-unreachable-lint.rs:4:9
|
LL | #![deny(unreachable_pub)]
| ^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

7 changes: 7 additions & 0 deletions tests/ui/macros/implicit-pub-unreachable-no-lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//@ check-pass
//@ edition:2018
#![crate_type = "lib"]

macro_rules! not_reexported {
() => {}
}
10 changes: 10 additions & 0 deletions tests/ui/macros/implicit-pub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ check-pass
//@ aux-build:implicit-pub.rs
//@ edition:2018

extern crate implicit_pub;

fn main() {
implicit_pub::inner::real_pub!();
implicit_pub::real_pub_reexport!();
}
Loading