Skip to content

Commit 712cc8f

Browse files
committed
resolve: Do not finalize shadowed bindings
I.e. do not mark them as used, or non-speculative loaded, or similar. Previously they were sometimes finalized during early resolution, causing issues like #144793 (comment).
1 parent fce0e74 commit 712cc8f

File tree

13 files changed

+31
-26
lines changed

13 files changed

+31
-26
lines changed

compiler/rustc_macros/src/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syn::punctuated::Punctuated;
55
use syn::spanned::Spanned;
66
use syn::{
77
AttrStyle, Attribute, Block, Error, Expr, Ident, Pat, ReturnType, Token, Type, braced,
8-
parenthesized, parse_macro_input, parse_quote, token,
8+
parenthesized, parse_macro_input, token,
99
};
1010

1111
mod kw {

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)]

library/std/src/sys/pal/unix/stack_overflow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ mod imp {
7272
use crate::sync::OnceLock;
7373
use crate::sync::atomic::{Atomic, AtomicBool, AtomicPtr, AtomicUsize, Ordering};
7474
use crate::sys::pal::unix::os;
75-
use crate::{io, mem, panic, ptr};
75+
use crate::{io, mem, ptr};
7676

7777
// Signal handler for the SIGSEGV and SIGBUS handlers. We've got guard pages
7878
// (unmapped pages) at the end of every thread's stack, so if a thread ends

src/tools/clippy/tests/ui/useless_attribute.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub mod redundant_imports_issue {
153153
() => {};
154154
}
155155

156-
#[expect(redundant_imports)]
156+
#[expect(unused_imports)]
157157
pub(crate) use empty;
158158

159159
empty!();

src/tools/clippy/tests/ui/useless_attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub mod redundant_imports_issue {
153153
() => {};
154154
}
155155

156-
#[expect(redundant_imports)]
156+
#[expect(unused_imports)]
157157
pub(crate) use empty;
158158

159159
empty!();

src/tools/rust-analyzer/crates/hir-expand/src/builtin/derive_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tracing::debug;
1212

1313
use crate::{
1414
ExpandError, ExpandResult, MacroCallId,
15-
builtin::quote::{dollar_crate, quote},
15+
builtin::quote::dollar_crate,
1616
db::ExpandDatabase,
1717
hygiene::span_with_def_site_ctxt,
1818
name::{self, AsName, Name},

src/tools/rust-analyzer/crates/hir-expand/src/builtin/fn_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax_bridge::syntax_node_to_token_tree;
1919

2020
use crate::{
2121
EditionedFileId, ExpandError, ExpandResult, Lookup as _, MacroCallId,
22-
builtin::quote::{WithDelimiter, dollar_crate, quote},
22+
builtin::quote::{WithDelimiter, dollar_crate},
2323
db::ExpandDatabase,
2424
hygiene::{span_with_call_site_ctxt, span_with_def_site_ctxt},
2525
name,

src/tools/rust-analyzer/crates/hir-expand/src/builtin/quote.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ mod tests {
229229
use span::{Edition, ROOT_ERASED_FILE_AST_ID, SpanAnchor, SyntaxContext};
230230
use syntax::{TextRange, TextSize};
231231

232-
use super::quote;
233-
234232
const DUMMY: tt::Span = tt::Span {
235233
range: TextRange::empty(TextSize::new(0)),
236234
anchor: SpanAnchor {

src/tools/rustfmt/src/config/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ mod test {
516516
#[allow(dead_code)]
517517
mod mock {
518518
use super::super::*;
519-
use crate::config_option_with_style_edition_default;
520519
use rustfmt_config_proc_macro::config_type;
521520

522521
#[config_type]

0 commit comments

Comments
 (0)