Skip to content

Commit 37c9478

Browse files
authored
Rollup merge of rust-lang#111761 - bvanjoi:fix-109148, r=petrochenkov
fix(resolve): not defined `extern crate shadow_name` Fixes rust-lang#109148 ## Why does rust-lang#109148 panic? When resolving `use std::xx` it enters `visit_scopes` from `early_resolve_ident_in_lexical_scope`, and iters twice during the loop: |iter| `scope` | `break_result` | result | |-|-|-|-| | 0 | `Module` pointed to root | binding pointed to `Undetermined`, so result is `None` | scope changed to `ExternPrelude` | | 1 | `ExternPrelude` | binding pointed to `std` | - | Then, the result of `maybe_resolve_path` is `Module(std)`, so `import.imported_module.set` is executed. Finally, during the `finalize_import` of `use std::xx`, `resolve_path` returns `NonModule` because `Binding(Ident(std), Module(root)`'s binding points to `extern crate blah as std`, which causes the assertion to fail at `assert!(import.imported_module.get().is_none());`. ## Investigation The question is why `#[a] extern crate blah as std` is not defined as a binding of `std::xxx`, which causes the iteration twice during `visit_scopes` when resolving `std::xxx`. Ideally, the value of `break_result.is_some()` should have been valid in the first iteration. After debugging, I found that because `#[a] extern crate blah as std` had been dummied by `placeholder` during `collect_invocations`, so it had lost its attrs, span, etc..., so it will not be defined. However, `expand_invoc` added them back, then the next `build_reduced_graph`, `#[a] extern crate blah as std` would have been defined, so it makes the result of `resolved_path` unexpected, and the program panics. ## Try to solve I think there has two-way to solve this issue: - Expand invocations before the first `resolve_imports` during `fully_expand_fragment`. However, I do not think this is a good idea because it would mess up the current design. - As my PR described: do not define to `extern crate blah as std` during the second `build_reduced_graph`, which is very easy and more reasonable. r? `@petrochenkov`
2 parents ee08dd8 + c41b208 commit 37c9478

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+5
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,11 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
873873
let msg = "macro-expanded `extern crate` items cannot \
874874
shadow names passed with `--extern`";
875875
self.r.tcx.sess.span_err(item.span, msg);
876+
// `return` is intended to discard this binding because it's an
877+
// unregistered ambiguity error which would result in a panic
878+
// caused by inconsistency `path_res`
879+
// more details: https://github.com/rust-lang/rust/pull/111761
880+
return;
876881
}
877882
}
878883
let entry = self.r.extern_prelude.entry(ident.normalize_to_macros_2_0()).or_insert(

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl Determinacy {
106106
/// A specific scope in which a name can be looked up.
107107
/// This enum is currently used only for early resolution (imports and macros),
108108
/// but not for late resolution yet.
109-
#[derive(Clone, Copy)]
109+
#[derive(Clone, Copy, Debug)]
110110
enum Scope<'a> {
111111
DeriveHelpers(LocalExpnId),
112112
DeriveHelpersCompat,

tests/ui/imports/issue-109148.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// edition: 2021
2+
3+
// https://github.com/rust-lang/rust/pull/111761#issuecomment-1557777314
4+
macro_rules! m {
5+
() => {
6+
extern crate core as std;
7+
//~^ ERROR macro-expanded `extern crate` items cannot shadow names passed with `--extern`
8+
}
9+
}
10+
11+
m!();
12+
13+
use std::mem;
14+
15+
fn main() {}

tests/ui/imports/issue-109148.stderr

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: macro-expanded `extern crate` items cannot shadow names passed with `--extern`
2+
--> $DIR/issue-109148.rs:6:9
3+
|
4+
LL | extern crate core as std;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
...
7+
LL | m!();
8+
| ---- in this macro invocation
9+
|
10+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)