Skip to content

Loosen shadowing check inside macro contexts. #99191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 21 additions & 11 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2842,17 +2842,27 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// but we still conservatively report an error, see
// issues/33118#issuecomment-233962221 for one reason why.
let binding = binding.expect("no binding for a ctor or static");
self.report_error(
ident.span,
ResolutionError::BindingShadowsSomethingUnacceptable {
shadowing_binding: pat_src,
name: ident.name,
participle: if binding.is_import() { "imported" } else { "defined" },
article: binding.res().article(),
shadowed_binding: binding.res(),
shadowed_binding_span: binding.span,
},
);
// To respect macro hygiene rules, we should not produce errors if
// the binding is from a scope outside of the macro the ident is from.
// This prevents problems like issues/99018. At the same time, we
// still want to have statics and consts defined inside macros to be
// properly visible, so we need to do an asymmetric check.
let ident_context = ident.span.ctxt().normalize_to_macro_rules();
let mut binding_context = binding.span.ctxt();
binding_context.normalize_to_macros_2_0_and_adjust(ident_context.outer_expn());
if ident_context == binding_context {
self.report_error(
ident.span,
ResolutionError::BindingShadowsSomethingUnacceptable {
shadowing_binding: pat_src,
name: ident.name,
participle: if binding.is_import() { "imported" } else { "defined" },
article: binding.res().article(),
shadowed_binding: binding.res(),
shadowed_binding_span: binding.span,
},
);
}
None
}
Res::Def(DefKind::ConstParam, def_id) => {
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/hygiene/shadow-static-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// build-pass

// Check that a macro doesn't generate an error when a let binding shadows a
// static defined outside the macro.

macro_rules! h {
() => {
let x = 2;
}
}

#[allow(non_upper_case_globals)]
static x: usize = 3;

fn main() {
h!();
}
15 changes: 15 additions & 0 deletions src/test/ui/hygiene/shadow-static-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Check that a macro generated static does cause a shadowing error

macro_rules! h {
() => {
#[allow(non_upper_case_globals)]
static x: usize = 3;
}
}

h!();

fn main() {
let x = 2;
//~^ ERROR let bindings cannot shadow statics
}
12 changes: 12 additions & 0 deletions src/test/ui/hygiene/shadow-static-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0530]: let bindings cannot shadow statics
--> $DIR/shadow-static-2.rs:13:7
|
LL | static x: usize = 3;
| -------------------- the static `x` is defined here
...
LL | let x = 2;
| ^ cannot be named the same as a static

error: aborting due to previous error

For more information about this error, try `rustc --explain E0530`.