Skip to content

Commit 8c500c7

Browse files
Auto merge of #145113 - petrochenkov:lessfinalize, r=<try>
resolve: Do not finalize shadowed bindings
2 parents 2886b36 + 5feb92e commit 8c500c7

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
429429
// to detect potential ambiguities.
430430
let mut innermost_result: Option<(NameBinding<'_>, Flags)> = None;
431431
let mut determinacy = Determinacy::Determined;
432+
// Shadowed bindings don't need to be marked as used or non-speculatively loaded.
433+
macro finalize_scope() {
434+
if innermost_result.is_none() { finalize } else { None }
435+
}
432436

433437
// Go through all the scopes and try to resolve the name.
434438
let break_result = self.visit_scopes(
@@ -494,13 +498,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
494498
_ => Err(Determinacy::Determined),
495499
},
496500
Scope::Module(module, derive_fallback_lint_id) => {
497-
let (adjusted_parent_scope, finalize) =
501+
let (adjusted_parent_scope, adjusted_finalize) =
498502
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..)) {
499-
(parent_scope, finalize)
503+
(parent_scope, finalize_scope!())
500504
} else {
501505
(
502506
&ParentScope { module, ..*parent_scope },
503-
finalize.map(|f| Finalize { used: Used::Scope, ..f }),
507+
finalize_scope!().map(|f| Finalize { used: Used::Scope, ..f }),
504508
)
505509
};
506510
let binding = this.reborrow().resolve_ident_in_module_unadjusted(
@@ -513,7 +517,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
513517
} else {
514518
Shadowing::Restricted
515519
},
516-
finalize,
520+
adjusted_finalize,
517521
ignore_binding,
518522
ignore_import,
519523
);
@@ -562,7 +566,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
562566
None => Err(Determinacy::Determined),
563567
},
564568
Scope::ExternPrelude => {
565-
match this.reborrow().extern_prelude_get(ident, finalize.is_some()) {
569+
match this.reborrow().extern_prelude_get(ident, finalize_scope!().is_some())
570+
{
566571
Some(binding) => Ok((binding, Flags::empty())),
567572
None => Err(Determinacy::determined(
568573
this.graph_root.unexpanded_invocations.borrow().is_empty(),
@@ -599,8 +604,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
599604
if matches!(ident.name, sym::f16)
600605
&& !this.tcx.features().f16()
601606
&& !ident.span.allows_unstable(sym::f16)
602-
&& finalize.is_some()
603-
&& innermost_result.is_none()
607+
&& finalize_scope!().is_some()
604608
{
605609
feature_err(
606610
this.tcx.sess,
@@ -613,8 +617,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
613617
if matches!(ident.name, sym::f128)
614618
&& !this.tcx.features().f128()
615619
&& !ident.span.allows_unstable(sym::f128)
616-
&& finalize.is_some()
617-
&& innermost_result.is_none()
620+
&& finalize_scope!().is_some()
618621
{
619622
feature_err(
620623
this.tcx.sess,

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(arbitrary_self_types)]
1616
#![feature(assert_matches)]
1717
#![feature(box_patterns)]
18+
#![feature(decl_macro)]
1819
#![feature(if_let_guard)]
1920
#![feature(iter_intersperse)]
2021
#![feature(rustc_attrs)]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Non-existent path in `--extern` doesn't result in an error if it's shadowed by `extern crate`.
2+
3+
//@ check-pass
4+
//@ compile-flags: --extern something=/path/to/nowhere
5+
6+
extern crate std as something;
7+
8+
fn main() {
9+
something::println!();
10+
}

tests/ui/rust-2018/uniform-paths/deadlock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//@ compile-flags:--extern foo --extern bar
33

44
use bar::foo; //~ ERROR can't find crate for `bar`
5-
use foo::bar; //~ ERROR can't find crate for `foo`
5+
use foo::bar;
66
//~^^ ERROR unresolved imports `bar::foo`, `foo::bar`
77

88
fn main() {}

tests/ui/rust-2018/uniform-paths/deadlock.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error[E0463]: can't find crate for `bar`
44
LL | use bar::foo;
55
| ^^^ can't find crate
66

7-
error[E0463]: can't find crate for `foo`
8-
--> $DIR/deadlock.rs:5:5
9-
|
10-
LL | use foo::bar;
11-
| ^^^ can't find crate
12-
137
error[E0432]: unresolved imports `bar::foo`, `foo::bar`
148
--> $DIR/deadlock.rs:4:5
159
|
@@ -18,7 +12,7 @@ LL | use bar::foo;
1812
LL | use foo::bar;
1913
| ^^^^^^^^
2014

21-
error: aborting due to 3 previous errors
15+
error: aborting due to 2 previous errors
2216

2317
Some errors have detailed explanations: E0432, E0463.
2418
For more information about an error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)