Convert impossible cases in macro resolution into assertions

This commit is contained in:
Jane Losare-Lusby
2025-10-06 10:55:52 -07:00
parent 8111a2d6da
commit 6920a8e8fc
2 changed files with 22 additions and 17 deletions
+14 -7
View File
@@ -3,7 +3,7 @@
use rustc_ast::{self as ast, NodeId};
use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::{DefKind, MacroKinds, Namespace, NonMacroAttrKind, PartialRes, PerNS};
use rustc_middle::bug;
use rustc_middle::{bug, span_bug};
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
use rustc_session::parse::feature_err;
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
@@ -677,14 +677,21 @@ struct Flags: u8 {
innermost_binding,
binding,
)
|| flags.contains(Flags::MACRO_RULES)
&& innermost_flags.contains(Flags::MODULE)
&& !this.disambiguate_macro_rules_vs_modularized(
binding,
innermost_binding,
)
{
Some(AmbiguityKind::MacroRulesVsModularized)
} else if flags.contains(Flags::MACRO_RULES)
&& innermost_flags.contains(Flags::MODULE)
{
// should be impossible because of visitation order in
// visit_scopes
//
// we visit all macro_rules scopes (e.g. textual scope macros)
// before we visit any modules (e.g. path-based scope macros)
span_bug!(
orig_ident.span,
"ambiguous scoped macro resolutions with path-based \
scope resolution as first candidate"
)
} else if innermost_binding.is_glob_import() {
Some(AmbiguityKind::GlobVsOuter)
} else if innermost_binding
+8 -10
View File
@@ -2226,16 +2226,14 @@ fn disambiguate_macro_rules_vs_modularized(
// Some non-controversial subset of ambiguities "modularized macro name" vs "macro_rules"
// is disambiguated to mitigate regressions from macro modularization.
// Scoping for `macro_rules` behaves like scoping for `let` at module level, in general.
match (
self.binding_parent_modules.get(&macro_rules),
self.binding_parent_modules.get(&modularized),
) {
(Some(macro_rules), Some(modularized)) => {
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
&& modularized.is_ancestor_of(*macro_rules)
}
_ => false,
}
//
// panic on index should be impossible, the only name_bindings passed in should be from
// `resolve_ident_in_scope_set` which will always refer to a local binding from an
// import or macro definition
let macro_rules = &self.binding_parent_modules[&macro_rules];
let modularized = &self.binding_parent_modules[&modularized];
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
&& modularized.is_ancestor_of(*macro_rules)
}
fn extern_prelude_get_item<'r>(