Skip to content

Commit 2ac850b

Browse files
committed
Convert impossible cases in macro resolution into assertions
1 parent 8111a2d commit 2ac850b

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
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: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,28 +1198,28 @@ pub struct Resolver<'ra, 'tcx> {
11981198
// FIXME: Remove interior mutability when speculative resolution produces these as outputs.
11991199
single_segment_macro_resolutions:
12001200
RefCell<Vec<(Ident, MacroKind, ParentScope<'ra>, Option<NameBinding<'ra>>, Option<Span>)>>,
1201-
multi_segment_macro_resolutions:
1202-
RefCell<Vec<(Vec<Segment>, Span, MacroKind, ParentScope<'ra>, Option<Res>, Namespace)>>,
1203-
builtin_attrs: Vec<(Ident, ParentScope<'ra>)>,
1204-
/// `derive(Copy)` marks items they are applied to so they are treated specially later.
1205-
/// Derive macros cannot modify the item themselves and have to store the markers in the global
1206-
/// context, so they attach the markers to derive container IDs using this resolver table.
1207-
containers_deriving_copy: FxHashSet<LocalExpnId>,
1208-
/// Parent scopes in which the macros were invoked.
1209-
/// FIXME: `derives` are missing in these parent scopes and need to be taken from elsewhere.
1210-
invocation_parent_scopes: FxHashMap<LocalExpnId, ParentScope<'ra>>,
1211-
/// `macro_rules` scopes *produced* by expanding the macro invocations,
1212-
/// include all the `macro_rules` items and other invocations generated by them.
1213-
output_macro_rules_scopes: FxHashMap<LocalExpnId, MacroRulesScopeRef<'ra>>,
1214-
/// `macro_rules` scopes produced by `macro_rules` item definitions.
1215-
macro_rules_scopes: FxHashMap<LocalDefId, MacroRulesScopeRef<'ra>>,
1216-
/// Helper attributes that are in scope for the given expansion.
1217-
helper_attrs: FxHashMap<LocalExpnId, Vec<(Ident, NameBinding<'ra>)>>,
1218-
/// Ready or in-progress results of resolving paths inside the `#[derive(...)]` attribute
1219-
/// with the given `ExpnId`.
1220-
derive_data: FxHashMap<LocalExpnId, DeriveData>,
1221-
1222-
/// Avoid duplicated errors for "name already defined".
1201+
multi_segment_macro_resolutions:
1202+
RefCell<Vec<(Vec<Segment>, Span, MacroKind, ParentScope<'ra>, Option<Res>, Namespace)>>,
1203+
builtin_attrs: Vec<(Ident, ParentScope<'ra>)>,
1204+
/// `derive(Copy)` marks items they are applied to so they are treated specially later.
1205+
/// Derive macros cannot modify the item themselves and have to store the markers in the global
1206+
/// context, so they attach the markers to derive container IDs using this resolver table.
1207+
containers_deriving_copy: FxHashSet<LocalExpnId>,
1208+
/// Parent scopes in which the macros were invoked.
1209+
/// FIXME: `derives` are missing in these parent scopes and need to be taken from elsewhere.
1210+
invocation_parent_scopes: FxHashMap<LocalExpnId, ParentScope<'ra>>,
1211+
/// `macro_rules` scopes *produced* by expanding the macro invocations,
1212+
/// include all the `macro_rules` items and other invocations generated by them.
1213+
output_macro_rules_scopes: FxHashMap<LocalExpnId, MacroRulesScopeRef<'ra>>,
1214+
/// `macro_rules` scopes produced by `macro_rules` item definitions.
1215+
macro_rules_scopes: FxHashMap<LocalDefId, MacroRulesScopeRef<'ra>>,
1216+
/// Helper attributes that are in scope for the given expansion.
1217+
helper_attrs: FxHashMap<LocalExpnId, Vec<(Ident, NameBinding<'ra>)>>,
1218+
/// Ready or in-progress results of resolving paths inside the `#[derive(...)]` attribute
1219+
/// with the given `ExpnId`.
1220+
derive_data: FxHashMap<LocalExpnId, DeriveData>,
1221+
1222+
/// Avoid duplicated errors for "name already defined".
12231223
name_already_seen: FxHashMap<Symbol, Span>,
12241224

12251225
potentially_unused_imports: Vec<Import<'ra>> = Vec::new(),
@@ -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)