Rollup merge of #128932 - bvanjoi:issue-128813, r=petrochenkov

skip updating when external binding is existed

Fixes #128813

For following code:

```rs
extern crate core;

fn f() {
    use ::core;
}

macro_rules! m {
    () => {
        extern crate std as core;
    };
}

m!();

fn main() {}
```

- In the first loop, we define `extern crate core` and `use ::core` will be referred to `core` (yes, it does not consider if there are some macros that are not expanded. Ideally, this should be delayed if there are some unexpanded macros in the root, but I didn't change it like that because it seems like a huge change).
- Then `m` is expanded, which makes `extern_prelude('core')` return `std` rather than `core`, causing the inconsistency.

r? `@petrochenkov`
This commit is contained in:
Matthias Krüger 2024-08-20 22:21:56 +02:00 committed by GitHub
commit 2e58d62fec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 179 additions and 17 deletions

View File

@ -896,7 +896,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
self.r.potentially_unused_imports.push(import);
let imported_binding = self.r.import(binding, import);
if parent == self.r.graph_root {
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
let ident = ident.normalize_to_macros_2_0();
if let Some(entry) = self.r.extern_prelude.get(&ident) {
if expansion != LocalExpnId::ROOT && orig_name.is_some() && !entry.is_import() {
self.r.dcx().emit_err(
errors::MacroExpandedExternCrateCannotShadowExternArguments {
@ -913,14 +914,21 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
let entry = self
.r
.extern_prelude
.entry(ident.normalize_to_macros_2_0())
.entry(ident)
.or_insert(ExternPreludeEntry { binding: None, introduced_by_item: true });
// Binding from `extern crate` item in source code can replace
// a binding from `--extern` on command line here.
entry.binding = Some(imported_binding);
if orig_name.is_some() {
entry.introduced_by_item = true;
}
// Binding from `extern crate` item in source code can replace
// a binding from `--extern` on command line here.
if !entry.is_import() {
entry.binding = Some(imported_binding)
} else if ident.name != kw::Underscore {
self.r.dcx().span_delayed_bug(
item.span,
format!("it had been define the external module '{ident}' multiple times"),
);
}
}
self.r.define(parent, ident, TypeNS, imported_binding);
}

View File

@ -1289,8 +1289,7 @@ ui/imports/auxiliary/issue-52891.rs
ui/imports/auxiliary/issue-55811.rs
ui/imports/auxiliary/issue-56125.rs
ui/imports/auxiliary/issue-59764.rs
ui/imports/auxiliary/issue-85992-extern-1.rs
ui/imports/auxiliary/issue-85992-extern-2.rs
ui/imports/auxiliary/issue-85992-extern.rs
ui/imports/issue-109148.rs
ui/imports/issue-109343.rs
ui/imports/issue-113953.rs

View File

@ -1,6 +1,6 @@
#[macro_export]
macro_rules! m {
() => {
use issue_85992_extern_2::Outcome;
use empty::Outcome;
}
}

View File

@ -1,11 +1,11 @@
//@ edition: 2021
//@ compile-flags: --extern issue_85992_extern_1 --extern issue_85992_extern_2
//@ aux-build: issue-85992-extern-1.rs
//@ aux-build: issue-85992-extern-2.rs
//@ compile-flags: --extern issue_85992_extern --extern empty
//@ aux-build: issue-85992-extern.rs
//@ aux-build: empty.rs
issue_85992_extern_1::m!();
issue_85992_extern::m!();
use crate::issue_85992_extern_2;
//~^ ERROR unresolved import `crate::issue_85992_extern_2`
use crate::empty;
//~^ ERROR unresolved import `crate::empty`
fn main() {}

View File

@ -1,8 +1,8 @@
error[E0432]: unresolved import `crate::issue_85992_extern_2`
error[E0432]: unresolved import `crate::empty`
--> $DIR/issue-85992.rs:8:5
|
LL | use crate::issue_85992_extern_2;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `issue_85992_extern_2` in the root
LL | use crate::empty;
| ^^^^^^^^^^^^ no `empty` in the root
error: aborting due to 1 previous error

View File

@ -0,0 +1,18 @@
//@ edition: 2021
// issue#128813
extern crate core;
macro_rules! m {
() => {
extern crate std as core;
//~^ ERROR: the name `core` is defined multiple times
};
}
m!();
fn main() {
use ::core;
}

View File

@ -0,0 +1,22 @@
error[E0259]: the name `core` is defined multiple times
--> $DIR/multiple-extern-by-macro-for-buitlin.rs:9:9
|
LL | extern crate core;
| ------------------ previous import of the extern crate `core` here
...
LL | extern crate std as core;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ `core` reimported here
...
LL | m!();
| ---- in this macro invocation
|
= note: `core` must be defined only once in the type namespace of this module
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you can use `as` to change the binding name of the import
|
LL | extern crate std as other_core;
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0259`.

View File

@ -0,0 +1,19 @@
//@ edition: 2021
//@ aux-build: empty.rs
// issue#128813
extern crate empty;
macro_rules! m {
() => {
extern crate std as empty;
//~^ ERROR: the name `empty` is defined multiple times
};
}
m!();
fn main() {
use ::empty;
}

View File

@ -0,0 +1,22 @@
error[E0259]: the name `empty` is defined multiple times
--> $DIR/multiple-extern-by-macro-for-custom.rs:10:9
|
LL | extern crate empty;
| ------------------- previous import of the extern crate `empty` here
...
LL | extern crate std as empty;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `empty` reimported here
...
LL | m!();
| ---- in this macro invocation
|
= note: `empty` must be defined only once in the type namespace of this module
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you can use `as` to change the binding name of the import
|
LL | extern crate std as other_empty;
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0259`.

View File

@ -0,0 +1,19 @@
//@ edition: 2021
// issue#128813
extern crate non_existent;
//~^ ERROR: can't find crate for `non_existent`
macro_rules! m {
() => {
extern crate std as non_existent;
//~^ ERROR: the name `non_existent` is defined multiple times
};
}
m!();
fn main() {
use ::non_existent;
}

View File

@ -0,0 +1,29 @@
error[E0463]: can't find crate for `non_existent`
--> $DIR/multiple-extern-by-macro-for-inexist.rs:5:1
|
LL | extern crate non_existent;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
error[E0259]: the name `non_existent` is defined multiple times
--> $DIR/multiple-extern-by-macro-for-inexist.rs:10:9
|
LL | extern crate non_existent;
| -------------------------- previous import of the extern crate `non_existent` here
...
LL | extern crate std as non_existent;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `non_existent` reimported here
...
LL | m!();
| ---- in this macro invocation
|
= note: `non_existent` must be defined only once in the type namespace of this module
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you can use `as` to change the binding name of the import
|
LL | extern crate std as other_non_existent;
|
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0259, E0463.
For more information about an error, try `rustc --explain E0259`.

View File

@ -0,0 +1,18 @@
//@ edition: 2021
// issue#128813
extern crate core as _;
macro_rules! m {
() => {
extern crate std as _;
};
}
m!();
fn main() {
use ::_;
//~^ ERROR: expected identifier, found reserved identifier `_`
}

View File

@ -0,0 +1,8 @@
error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-extern-by-macro-for-underscore.rs:16:11
|
LL | use ::_;
| ^ expected identifier, found reserved identifier
error: aborting due to 1 previous error