Skip to content

Commit 235dfba

Browse files
committed
resolve: Avoid finalizing extern prelude entries more than once
1 parent 05abce5 commit 235dfba

File tree

4 files changed

+9
-19
lines changed

4 files changed

+9
-19
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18461846
let extern_prelude_ambiguity = || {
18471847
self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)).is_some_and(|entry| {
18481848
entry.item_binding.map(|(b, _)| b) == Some(b1)
1849-
&& entry.flag_binding.as_ref().and_then(|pb| pb.get().binding()) == Some(b2)
1849+
&& entry.flag_binding.as_ref().and_then(|pb| pb.get().0.binding()) == Some(b2)
18501850
})
18511851
};
18521852
let (b1, b2, misc1, misc2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() {

compiler/rustc_resolve/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ struct ExternPreludeEntry<'ra> {
10311031
/// `flag_binding` is `None`, or when `extern crate` introducing `item_binding` used renaming.
10321032
item_binding: Option<(NameBinding<'ra>, /* introduced by item */ bool)>,
10331033
/// Binding from an `--extern` flag, lazily populated on first use.
1034-
flag_binding: Option<Cell<PendingBinding<'ra>>>,
1034+
flag_binding: Option<Cell<(PendingBinding<'ra>, /* finalized */ bool)>>,
10351035
}
10361036

10371037
impl ExternPreludeEntry<'_> {
@@ -1042,7 +1042,7 @@ impl ExternPreludeEntry<'_> {
10421042
fn flag() -> Self {
10431043
ExternPreludeEntry {
10441044
item_binding: None,
1045-
flag_binding: Some(Cell::new(PendingBinding::Pending)),
1045+
flag_binding: Some(Cell::new((PendingBinding::Pending, false))),
10461046
}
10471047
}
10481048
}
@@ -2245,14 +2245,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22452245
fn extern_prelude_get_flag(&self, ident: Ident, finalize: bool) -> Option<NameBinding<'ra>> {
22462246
let entry = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident));
22472247
entry.and_then(|entry| entry.flag_binding.as_ref()).and_then(|flag_binding| {
2248-
let binding = match flag_binding.get() {
2248+
let (pending_binding, finalized) = flag_binding.get();
2249+
let binding = match pending_binding {
22492250
PendingBinding::Ready(binding) => {
2250-
if finalize {
2251+
if finalize && !finalized {
22512252
self.cstore_mut().process_path_extern(self.tcx, ident.name, ident.span);
22522253
}
22532254
binding
22542255
}
22552256
PendingBinding::Pending => {
2257+
debug_assert!(!finalized);
22562258
let crate_id = if finalize {
22572259
self.cstore_mut().process_path_extern(self.tcx, ident.name, ident.span)
22582260
} else {
@@ -2264,7 +2266,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22642266
})
22652267
}
22662268
};
2267-
flag_binding.set(PendingBinding::Ready(binding));
2269+
flag_binding.set((PendingBinding::Ready(binding), finalize || finalized));
22682270
binding.or_else(|| finalize.then_some(self.dummy_binding))
22692271
})
22702272
}

tests/ui/crate-loading/invalid-rlib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,3 @@
66
#![no_std]
77
use ::foo; //~ ERROR invalid metadata files for crate `foo`
88
//~| NOTE failed to mmap file
9-
//~^^ ERROR invalid metadata files for crate `foo`
10-
//~| NOTE failed to mmap file
11-
//~| NOTE duplicate diagnostic

tests/ui/crate-loading/invalid-rlib.stderr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ LL | use ::foo;
66
|
77
= note: failed to mmap file 'auxiliary/libfoo.rlib'
88

9-
error[E0786]: found invalid metadata files for crate `foo`
10-
--> $DIR/invalid-rlib.rs:7:7
11-
|
12-
LL | use ::foo;
13-
| ^^^
14-
|
15-
= note: failed to mmap file 'auxiliary/libfoo.rlib'
16-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
17-
18-
error: aborting due to 2 previous errors
9+
error: aborting due to 1 previous error
1910

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

0 commit comments

Comments
 (0)