-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Add a TaggedQueryKey to identify a query instance
#153492
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,106 +1,18 @@ | ||
| use std::fmt::Debug; | ||
| use std::marker::PhantomData; | ||
| use std::mem::transmute; | ||
| use std::sync::Arc; | ||
|
|
||
| use rustc_data_structures::sync::{DynSend, DynSync}; | ||
| use rustc_hir::def::DefKind; | ||
| use rustc_span::Span; | ||
| use rustc_span::def_id::DefId; | ||
|
|
||
| use crate::dep_graph::DepKind; | ||
| use crate::queries::TaggedQueryKey; | ||
|
|
||
| /// Description of a frame in the query stack. | ||
| /// | ||
| /// This is mostly used in case of cycles for error reporting. | ||
| #[derive(Clone, Debug)] | ||
| pub struct QueryStackFrame<I> { | ||
| /// This field initially stores a `QueryStackDeferred` during collection, | ||
| /// but can later be changed to `QueryStackFrameExtra` containing concrete information | ||
| /// by calling `lift`. This is done so that collecting query does not need to invoke | ||
| /// queries, instead `lift` will call queries in a more appropriate location. | ||
| pub info: I, | ||
|
|
||
| pub struct QueryStackFrame<'tcx> { | ||
| pub tagged_key: TaggedQueryKey<'tcx>, | ||
| pub dep_kind: DepKind, | ||
| pub def_id: Option<DefId>, | ||
| /// A def-id that is extracted from a `Ty` in a query key | ||
| pub def_id_for_ty_in_cycle: Option<DefId>, | ||
| } | ||
|
|
||
| impl<'tcx> QueryStackFrame<QueryStackDeferred<'tcx>> { | ||
| #[inline] | ||
| pub fn new( | ||
| info: QueryStackDeferred<'tcx>, | ||
| dep_kind: DepKind, | ||
| def_id: Option<DefId>, | ||
| def_id_for_ty_in_cycle: Option<DefId>, | ||
| ) -> Self { | ||
| Self { info, def_id, dep_kind, def_id_for_ty_in_cycle } | ||
| } | ||
|
|
||
| pub fn lift(&self) -> QueryStackFrame<QueryStackFrameExtra> { | ||
| QueryStackFrame { | ||
| info: self.info.extract(), | ||
| dep_kind: self.dep_kind, | ||
| def_id: self.def_id, | ||
| def_id_for_ty_in_cycle: self.def_id_for_ty_in_cycle, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct QueryStackFrameExtra { | ||
| pub description: String, | ||
| pub span: Option<Span>, | ||
| pub def_kind: Option<DefKind>, | ||
| } | ||
|
|
||
| impl QueryStackFrameExtra { | ||
| #[inline] | ||
| pub fn new(description: String, span: Option<Span>, def_kind: Option<DefKind>) -> Self { | ||
| Self { description, span, def_kind } | ||
| } | ||
|
|
||
| // FIXME(eddyb) Get more valid `Span`s on queries. | ||
| #[inline] | ||
| pub fn default_span(&self, span: Span) -> Span { | ||
| if !span.is_dummy() { | ||
| return span; | ||
| } | ||
| self.span.unwrap_or(span) | ||
| } | ||
| } | ||
|
|
||
| /// Track a 'side effect' for a particular query. | ||
| /// This is used to hold a closure which can create `QueryStackFrameExtra`. | ||
| #[derive(Clone)] | ||
| pub struct QueryStackDeferred<'tcx> { | ||
| _dummy: PhantomData<&'tcx ()>, | ||
|
|
||
| // `extract` may contain references to 'tcx, but we can't tell drop checking that it won't | ||
| // access it in the destructor. | ||
| extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend>, | ||
| } | ||
|
|
||
| impl<'tcx> QueryStackDeferred<'tcx> { | ||
| pub fn new<C: Copy + DynSync + DynSend + 'tcx>( | ||
| context: C, | ||
| extract: fn(C) -> QueryStackFrameExtra, | ||
| ) -> Self { | ||
| let extract: Arc<dyn Fn() -> QueryStackFrameExtra + DynSync + DynSend + 'tcx> = | ||
| Arc::new(move || extract(context)); | ||
| // SAFETY: The `extract` closure does not access 'tcx in its destructor as the only | ||
| // captured variable is `context` which is Copy and cannot have a destructor. | ||
| Self { _dummy: PhantomData, extract: unsafe { transmute(extract) } } | ||
| } | ||
|
|
||
| pub fn extract(&self) -> QueryStackFrameExtra { | ||
| (self.extract)() | ||
| } | ||
| } | ||
|
|
||
| impl<'tcx> Debug for QueryStackDeferred<'tcx> { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.write_str("QueryStackDeferred") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why have
inner? Why not just callkey.key_as_def_id()...here?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.
It reduces code generation as
innercan be reused for shared key types.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.
Can you add a comment explaining this?