-
Notifications
You must be signed in to change notification settings - Fork 14.1k
resolve: Preserve ambiguous glob reexports in crate metadata #147984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ use rustc_hir::def::{self, *}; | |
| use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId}; | ||
| use rustc_index::bit_set::DenseBitSet; | ||
| use rustc_metadata::creader::LoadedMacro; | ||
| use rustc_middle::metadata::ModChild; | ||
| use rustc_middle::metadata::{AmbigModChildKind, ModChild, Reexport}; | ||
| use rustc_middle::ty::{Feed, Visibility}; | ||
| use rustc_middle::{bug, span_bug}; | ||
| use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind}; | ||
|
|
@@ -36,9 +36,9 @@ use crate::imports::{ImportData, ImportKind}; | |
| use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef}; | ||
| use crate::ref_mut::CmCell; | ||
| use crate::{ | ||
| BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind, ModuleOrUniformRoot, | ||
| NameBinding, ParentScope, PathResult, ResolutionError, Resolver, Segment, Used, | ||
| VisResolutionError, errors, | ||
| AmbiguityKind, BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind, | ||
| ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope, PathResult, | ||
| ResolutionError, Resolver, Segment, Used, VisResolutionError, errors, | ||
| }; | ||
|
|
||
| type Res = def::Res<NodeId>; | ||
|
|
@@ -81,9 +81,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| res: Res, | ||
| vis: Visibility<DefId>, | ||
| span: Span, | ||
| expn_id: LocalExpnId, | ||
| expansion: LocalExpnId, | ||
| ambiguity: Option<(NameBinding<'ra>, AmbiguityKind)>, | ||
| ) { | ||
| let binding = self.arenas.new_res_binding(res, vis, span, expn_id); | ||
| let binding = self.arenas.alloc_name_binding(NameBindingData { | ||
| kind: NameBindingKind::Res(res), | ||
| ambiguity, | ||
| // External ambiguities always report the `AMBIGUOUS_GLOB_IMPORTS` lint at the moment. | ||
| warn_ambiguity: true, | ||
| vis, | ||
| span, | ||
| expansion, | ||
| }); | ||
| // Even if underscore names cannot be looked up, we still need to add them to modules, | ||
| // because they can be fetched by glob imports from those modules, and bring traits | ||
| // into scope both directly and through glob imports. | ||
|
|
@@ -232,9 +241,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| } | ||
|
|
||
| pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) { | ||
| for (i, child) in self.tcx.module_children(module.def_id()).into_iter().enumerate() { | ||
| let parent_scope = ParentScope::module(module, self.arenas); | ||
| self.build_reduced_graph_for_external_crate_res(child, parent_scope, i) | ||
| let def_id = module.def_id(); | ||
| let children = self.tcx.module_children(def_id); | ||
| let parent_scope = ParentScope::module(module, self.arenas); | ||
| for (i, child) in children.iter().enumerate() { | ||
| self.build_reduced_graph_for_external_crate_res(child, parent_scope, i, None) | ||
| } | ||
| for (i, child) in | ||
| self.cstore().ambig_module_children_untracked(def_id, self.tcx.sess).enumerate() | ||
yaahc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| self.build_reduced_graph_for_external_crate_res( | ||
| &child.main, | ||
| parent_scope, | ||
| children.len() + i, | ||
| Some((&child.second, child.kind)), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -244,18 +265,36 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| child: &ModChild, | ||
| parent_scope: ParentScope<'ra>, | ||
| child_index: usize, | ||
| ambig_child: Option<(&ModChild, AmbigModChildKind)>, | ||
| ) { | ||
| let parent = parent_scope.module; | ||
| let child_span = |this: &Self, reexport_chain: &[Reexport], res: def::Res<_>| { | ||
| this.def_span( | ||
| reexport_chain | ||
| .first() | ||
| .and_then(|reexport| reexport.id()) | ||
| .unwrap_or_else(|| res.def_id()), | ||
| ) | ||
| }; | ||
| let ModChild { ident, res, vis, ref reexport_chain } = *child; | ||
| let span = self.def_span( | ||
| reexport_chain | ||
| .first() | ||
| .and_then(|reexport| reexport.id()) | ||
| .unwrap_or_else(|| res.def_id()), | ||
| ); | ||
| let span = child_span(self, reexport_chain, res); | ||
| let res = res.expect_non_local(); | ||
| let expansion = parent_scope.expansion; | ||
| let ambig = ambig_child.map(|(ambig_child, ambig_kind)| { | ||
| let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child; | ||
| let span = child_span(self, reexport_chain, res); | ||
| let res = res.expect_non_local(); | ||
| let ambig_kind = match ambig_kind { | ||
| AmbigModChildKind::GlobVsGlob => AmbiguityKind::GlobVsGlob, | ||
| AmbigModChildKind::GlobVsExpanded => AmbiguityKind::GlobVsExpanded, | ||
| }; | ||
| (self.arenas.new_res_binding(res, vis, span, expansion), ambig_kind) | ||
| }); | ||
|
|
||
| // Record primary definitions. | ||
| let define_extern = |ns| { | ||
| self.define_extern(parent, ident, ns, child_index, res, vis, span, expansion, ambig) | ||
| }; | ||
| match res { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: When I was reviewing this section I felt like there was a high signal to noise ratio with the way this match is structured. As far as I can tell the only difference between the different Especially with the new field changing the formatting to be one line per argument, its even more difficult now to visually tell what is different and what isn't between those invocations. Feel free to ignore this one, just thought I'd share my experience.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can factor the |
||
| Res::Def( | ||
| DefKind::Mod | ||
|
|
@@ -272,9 +311,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| _, | ||
| ) | ||
| | Res::PrimTy(..) | ||
| | Res::ToolMod => { | ||
| self.define_extern(parent, ident, TypeNS, child_index, res, vis, span, expansion) | ||
| } | ||
| | Res::ToolMod => define_extern(TypeNS), | ||
| Res::Def( | ||
| DefKind::Fn | ||
| | DefKind::AssocFn | ||
|
|
@@ -283,10 +320,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| | DefKind::AssocConst | ||
| | DefKind::Ctor(..), | ||
| _, | ||
| ) => self.define_extern(parent, ident, ValueNS, child_index, res, vis, span, expansion), | ||
| Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => { | ||
| self.define_extern(parent, ident, MacroNS, child_index, res, vis, span, expansion) | ||
| } | ||
| ) => define_extern(ValueNS), | ||
| Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => define_extern(MacroNS), | ||
| Res::Def( | ||
| DefKind::TyParam | ||
| | DefKind::ConstParam | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming this means logic should be able to preserve globvsexpanded ambiguities across crate boundaries but I don't see any tests exercising this logic. Can you add one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a test.
In any case, cross-crate
GlobVsExpandedis unnecessary and will be removed in #149681.