-
-
Notifications
You must be signed in to change notification settings - Fork 15.1k
delegation: split resolution and lowering #157296
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -43,14 +43,14 @@ use rustc_ast::visit::Visitor; | |||||
| use rustc_ast::{self as ast, *}; | ||||||
| use rustc_attr_parsing::{AttributeParser, OmitDoc, Recovery, ShouldEmit}; | ||||||
| use rustc_data_structures::fingerprint::Fingerprint; | ||||||
| use rustc_data_structures::fx::FxIndexSet; | ||||||
| use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; | ||||||
| use rustc_data_structures::sorted_map::SortedMap; | ||||||
| use rustc_data_structures::stable_hash::{StableHash, StableHasher}; | ||||||
| use rustc_data_structures::steal::Steal; | ||||||
| use rustc_data_structures::tagged_ptr::TaggedRef; | ||||||
| use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle}; | ||||||
| use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res}; | ||||||
| use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId}; | ||||||
| use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId}; | ||||||
| use rustc_hir::definitions::PerParentDisambiguatorState; | ||||||
| use rustc_hir::lints::DelayedLint; | ||||||
| use rustc_hir::{ | ||||||
|
|
@@ -69,7 +69,10 @@ use smallvec::SmallVec; | |||||
| use thin_vec::ThinVec; | ||||||
| use tracing::{debug, instrument, trace}; | ||||||
|
|
||||||
| use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait}; | ||||||
| use crate::errors::{ | ||||||
| AssocTyParentheses, AssocTyParenthesesSub, CycleInDelegationSignatureResolution, | ||||||
| MisplacedImplTrait, UnresolvedDelegationCallee, | ||||||
| }; | ||||||
| use crate::item::Owners; | ||||||
|
|
||||||
| macro_rules! arena_vec { | ||||||
|
|
@@ -541,13 +544,11 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> { | |||||
| let mut delayed_ids: FxIndexSet<LocalDefId> = Default::default(); | ||||||
|
|
||||||
| for def_id in ast_index.indices() { | ||||||
| match &ast_index[def_id] { | ||||||
| AstOwner::Item(Item { kind: ItemKind::Delegation { .. }, .. }) | ||||||
| | AstOwner::AssocItem(Item { kind: AssocItemKind::Delegation { .. }, .. }, _) => { | ||||||
| delayed_ids.insert(def_id); | ||||||
| } | ||||||
| _ => lowerer.lower_node(def_id), | ||||||
| }; | ||||||
| if opt_delegation(&ast_index[def_id]).is_some() { | ||||||
| delayed_ids.insert(def_id); | ||||||
| } else { | ||||||
| lowerer.lower_node(def_id); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Don't hash unless necessary, because it's expensive. | ||||||
|
|
@@ -558,6 +559,16 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> mid_hir::Crate<'_> { | |||||
| mid_hir::Crate::new(owners, delayed_ids, delayed_resolver, opt_hir_hash) | ||||||
| } | ||||||
|
|
||||||
| fn opt_delegation<'a>(ast_owner: &'a AstOwner<'a>) -> Option<&'a Delegation> { | ||||||
|
Contributor
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. This could be a method on |
||||||
| match ast_owner { | ||||||
| AstOwner::Item(Item { kind: ItemKind::Delegation(d), .. }) | ||||||
| | AstOwner::AssocItem(Item { kind: AssocItemKind::Delegation(d), .. }, _) => { | ||||||
| Some(d.as_ref()) | ||||||
|
Contributor
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. |
||||||
| } | ||||||
| _ => None, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Lowers an AST owner corresponding to `def_id`, now only delegations are lowered this way. | ||||||
| pub fn lower_delayed_owner(tcx: TyCtxt<'_>, def_id: LocalDefId) { | ||||||
| let krate = tcx.hir_crate(()); | ||||||
|
|
@@ -583,6 +594,65 @@ pub fn lower_delayed_owner(tcx: TyCtxt<'_>, def_id: LocalDefId) { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| pub fn delegations_resolutions(tcx: TyCtxt<'_>, _: ()) -> FxIndexMap<LocalDefId, Option<DefId>> { | ||||||
|
Contributor
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.
Suggested change
Otherwise it's confusing why the
Contributor
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. It's probably better to move this function to its old place in delegation.rs. |
||||||
| let krate = tcx.hir_crate(()); | ||||||
|
|
||||||
| let (resolver, ast_crate) = &*krate.delayed_resolver.borrow(); | ||||||
|
|
||||||
| // FIXME!!!(fn_delegation): make ast index lifetime same as resolver, | ||||||
| // as it is too bad to reindex whole crate on each delegation lowering. | ||||||
|
Contributor
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. Sigh, we are doing it again. |
||||||
| let ast_index = index_crate(resolver, ast_crate); | ||||||
|
|
||||||
| let mut result = FxIndexMap::<LocalDefId, Option<DefId>>::default(); | ||||||
|
|
||||||
| for &def_id in &krate.delayed_ids { | ||||||
| let delegation = opt_delegation(&ast_index[def_id]).expect("processing delegations"); | ||||||
| let span = delegation.span(); | ||||||
|
|
||||||
| let res_id = resolver | ||||||
| .delegation_info(def_id) | ||||||
|
Contributor
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. Same with |
||||||
| .and_then(|info| resolve_delegation(tcx, info.resolution_id, span)); | ||||||
|
|
||||||
| result.insert(def_id, res_id); | ||||||
| } | ||||||
|
|
||||||
| result | ||||||
| } | ||||||
|
|
||||||
| fn resolve_delegation(tcx: TyCtxt<'_>, mut def_id: DefId, span: Span) -> Option<DefId> { | ||||||
|
Contributor
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.
Suggested change
Same as above. |
||||||
| let mut visited: FxHashSet<DefId> = Default::default(); | ||||||
| let mut path: SmallVec<[DefId; 1]> = Default::default(); | ||||||
|
|
||||||
| let (resolver, _) = &*tcx.hir_crate(()).delayed_resolver.borrow(); | ||||||
|
|
||||||
| loop { | ||||||
| visited.insert(def_id); | ||||||
|
|
||||||
| path.push(def_id); | ||||||
|
|
||||||
| // If def_id is in local crate and it corresponds to another delegation | ||||||
| // it means that we refer to another delegation as a callee, so in order to obtain | ||||||
| // a signature DefId we obtain NodeId of the callee delegation and try to get signature from it. | ||||||
| if let Some(local_id) = def_id.as_local() | ||||||
| && let Some(delegation_info) = resolver.delegation_info(local_id) | ||||||
| { | ||||||
| def_id = delegation_info.resolution_id; | ||||||
| if visited.contains(&def_id) { | ||||||
| // We encountered a cycle in the resolution, or delegation callee refers to non-existent | ||||||
| // entity, in this case emit an error. | ||||||
| match visited.len() { | ||||||
| 1 => tcx.dcx().emit_err(UnresolvedDelegationCallee { span }), | ||||||
| _ => tcx.dcx().emit_err(CycleInDelegationSignatureResolution { span }), | ||||||
| }; | ||||||
|
|
||||||
| return None; | ||||||
| } | ||||||
| } else { | ||||||
| return Some(path[0]); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[derive(Copy, Clone, PartialEq, Debug)] | ||||||
| enum ParamMode { | ||||||
| /// Any path in a type context. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1277,6 +1277,8 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> Mod | |
| } | ||
|
|
||
| fn force_delayed_owners_lowering(tcx: TyCtxt<'_>) { | ||
| tcx.ensure_done().delegations_resolutions(()); | ||
|
Contributor
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. Is it necessary to ensure this here? |
||
|
|
||
| let krate = tcx.hir_crate(()); | ||
| for &id in &krate.delayed_ids { | ||
| tcx.ensure_done().lower_delayed_owner(id); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Nit: the name is too generic, this is some very specific span inside the delegation, not the whole delegation span.
View changes since the review