Skip to content

Commit 94ed5c7

Browse files
authored
Rollup merge of #147412 - yaahc:macro-res-assertions, r=petrochenkov
Convert impossible cases in macro resolution into assertions associated discussion with `@petrochenkov:` [#t-compiler/help > understanding early name resolution of imports @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/understanding.20early.20name.20resolution.20of.20imports/near/542936677)
2 parents 5c13022 + 6920a8e commit 94ed5c7

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use Namespace::*;
33
use rustc_ast::{self as ast, NodeId};
44
use rustc_errors::ErrorGuaranteed;
55
use rustc_hir::def::{DefKind, MacroKinds, Namespace, NonMacroAttrKind, PartialRes, PerNS};
6-
use rustc_middle::bug;
6+
use rustc_middle::{bug, span_bug};
77
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
88
use rustc_session::parse::feature_err;
99
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
@@ -677,14 +677,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
677677
innermost_binding,
678678
binding,
679679
)
680-
|| flags.contains(Flags::MACRO_RULES)
681-
&& innermost_flags.contains(Flags::MODULE)
682-
&& !this.disambiguate_macro_rules_vs_modularized(
683-
binding,
684-
innermost_binding,
685-
)
686680
{
687681
Some(AmbiguityKind::MacroRulesVsModularized)
682+
} else if flags.contains(Flags::MACRO_RULES)
683+
&& innermost_flags.contains(Flags::MODULE)
684+
{
685+
// should be impossible because of visitation order in
686+
// visit_scopes
687+
//
688+
// we visit all macro_rules scopes (e.g. textual scope macros)
689+
// before we visit any modules (e.g. path-based scope macros)
690+
span_bug!(
691+
orig_ident.span,
692+
"ambiguous scoped macro resolutions with path-based \
693+
scope resolution as first candidate"
694+
)
688695
} else if innermost_binding.is_glob_import() {
689696
Some(AmbiguityKind::GlobVsOuter)
690697
} else if innermost_binding

compiler/rustc_resolve/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,16 +2226,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22262226
// Some non-controversial subset of ambiguities "modularized macro name" vs "macro_rules"
22272227
// is disambiguated to mitigate regressions from macro modularization.
22282228
// Scoping for `macro_rules` behaves like scoping for `let` at module level, in general.
2229-
match (
2230-
self.binding_parent_modules.get(&macro_rules),
2231-
self.binding_parent_modules.get(&modularized),
2232-
) {
2233-
(Some(macro_rules), Some(modularized)) => {
2234-
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
2235-
&& modularized.is_ancestor_of(*macro_rules)
2236-
}
2237-
_ => false,
2238-
}
2229+
//
2230+
// panic on index should be impossible, the only name_bindings passed in should be from
2231+
// `resolve_ident_in_scope_set` which will always refer to a local binding from an
2232+
// import or macro definition
2233+
let macro_rules = &self.binding_parent_modules[&macro_rules];
2234+
let modularized = &self.binding_parent_modules[&modularized];
2235+
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
2236+
&& modularized.is_ancestor_of(*macro_rules)
22392237
}
22402238

22412239
fn extern_prelude_get_item<'r>(

0 commit comments

Comments
 (0)