Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions compiler/rustc_middle/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,30 +448,6 @@ macro_rules! define_callbacks {
)*
}
}

pub fn def_kind(&self, tcx: TyCtxt<'tcx>) -> Option<DefKind> {
// This is used to reduce code generation as it
// can be reused for queries with the same key type.
fn inner<'tcx>(key: &impl $crate::query::QueryKey, tcx: TyCtxt<'tcx>)
-> Option<DefKind>
{
key
.key_as_def_id()
.and_then(|def_id| def_id.as_local())
.map(|def_id| tcx.def_kind(def_id))
}

if let TaggedQueryKey::def_kind(..) = self {
// Try to avoid infinite recursion.
return None
}

match self {
$(
TaggedQueryKey::$name(key) => inner(key, tcx),
)*
}
}
}

/// Holds a `QueryVTable` for each query.
Expand Down
22 changes: 16 additions & 6 deletions compiler/rustc_query_impl/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,23 @@ pub(crate) fn report_cycle<'tcx>(
usage: usage.tagged_key.description(tcx),
});

let alias = if stack
.iter()
.all(|frame| frame.tagged_key.def_kind(tcx) == Some(DefKind::TyAlias))
{
let is_all_def_kind = |def_kind| {
// Trivial type alias and trait alias cycles consists of `type_of` and
// `explicit_implied_predicates_of` queries, so we just check just these here.
stack.iter().all(|entry| match entry.tagged_key {
TaggedQueryKey::type_of(def_id)
| TaggedQueryKey::explicit_implied_predicates_of(def_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this condition contain the type_of and explicit_implied_predicates_of queries specifically?
The previous logic didn't contain any specific queries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the queries which are involved in simple TyAlias / TraitAlias cycles (in our tests at least).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a comment and probably a panic if we reach this code with any other TaggedQueryKey.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the surrounding function is called to produce a diagnostic for any query cycle, and this particular snippet is doing a best-effort check for certain situations that would let us add more specific information to that diagnostic.

So a panic here seems inappropriate.

if tcx.def_kind(def_id) == def_kind =>
{
true
}
_ => false,
})
};

let alias = if is_all_def_kind(DefKind::TyAlias) {
Some(crate::error::Alias::Ty)
} else if stack.iter().all(|frame| frame.tagged_key.def_kind(tcx) == Some(DefKind::TraitAlias))
{
} else if is_all_def_kind(DefKind::TraitAlias) {
Some(crate::error::Alias::Trait)
} else {
None
Expand Down
Loading