Skip to content

Commit 85dda2c

Browse files
committed
Add better default spans for the Ty impl of QueryKey
1 parent 783062d commit 85dda2c

21 files changed

Lines changed: 191 additions & 38 deletions

compiler/rustc_middle/src/query/keys.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::fmt::Debug;
55
use std::hash::Hash;
66

77
use rustc_ast::tokenstream::TokenStream;
8+
use rustc_data_structures::sso::SsoHashSet;
89
use rustc_data_structures::stable_hasher::StableHash;
910
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
1011
use rustc_hir::hir_id::OwnerId;
@@ -256,8 +257,8 @@ impl<'tcx> QueryKey for GenericArg<'tcx> {
256257
}
257258

258259
impl<'tcx> QueryKey for Ty<'tcx> {
259-
fn default_span(&self, _: TyCtxt<'_>) -> Span {
260-
DUMMY_SP
260+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
261+
def_id_of_type(*self).map(|def_id| tcx.def_span(def_id)).unwrap_or(DUMMY_SP)
261262
}
262263
}
263264

@@ -360,3 +361,58 @@ impl<'tcx> QueryKey for (ty::Instance<'tcx>, CollectionMode) {
360361
self.0.default_span(tcx)
361362
}
362363
}
364+
365+
/// Gets a `DefId` associated with a type
366+
///
367+
/// Visited set is needed to avoid full iteration over
368+
/// deeply nested tuples that have no DefId.
369+
fn def_id_of_type_cached<'a>(ty: Ty<'a>, visited: &mut SsoHashSet<Ty<'a>>) -> Option<DefId> {
370+
match *ty.kind() {
371+
ty::Adt(adt_def, _) => Some(adt_def.did()),
372+
373+
ty::Dynamic(data, ..) => data.principal_def_id(),
374+
375+
ty::Pat(subty, _) | ty::Array(subty, _) | ty::Slice(subty) => {
376+
def_id_of_type_cached(subty, visited)
377+
}
378+
379+
ty::RawPtr(ty, _) => def_id_of_type_cached(ty, visited),
380+
381+
ty::Ref(_, ty, _) => def_id_of_type_cached(ty, visited),
382+
383+
ty::Tuple(tys) => tys.iter().find_map(|ty| {
384+
if visited.insert(ty) {
385+
return def_id_of_type_cached(ty, visited);
386+
}
387+
return None;
388+
}),
389+
390+
ty::FnDef(def_id, _)
391+
| ty::Closure(def_id, _)
392+
| ty::CoroutineClosure(def_id, _)
393+
| ty::Coroutine(def_id, _)
394+
| ty::CoroutineWitness(def_id, _)
395+
| ty::Foreign(def_id) => Some(def_id),
396+
397+
ty::Alias(alias) => Some(alias.kind.def_id()),
398+
399+
ty::Bool
400+
| ty::Char
401+
| ty::Int(_)
402+
| ty::Uint(_)
403+
| ty::Str
404+
| ty::FnPtr(..)
405+
| ty::UnsafeBinder(_)
406+
| ty::Placeholder(..)
407+
| ty::Param(_)
408+
| ty::Infer(_)
409+
| ty::Bound(..)
410+
| ty::Error(_)
411+
| ty::Never
412+
| ty::Float(_) => None,
413+
}
414+
}
415+
416+
fn def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
417+
def_id_of_type_cached(ty, &mut SsoHashSet::new())
418+
}

src/tools/miri/tests/fail/layout_cycle.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
error[E0391]: cycle detected when computing layout of `S<S<()>>`
2+
--> tests/fail/layout_cycle.rs:LL:CC
23
|
3-
= note: ...which requires computing layout of `<S<()> as Tr>::I`...
4+
LL | pub struct S<T: Tr> {
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: ...which requires computing layout of `<S<()> as Tr>::I`...
8+
--> tests/fail/layout_cycle.rs:LL:CC
9+
|
10+
LL | type I: Tr;
11+
| ^^^^^^^^^^
412
= note: ...which again requires computing layout of `S<S<()>>`, completing the cycle
513
note: cycle used when const-evaluating + checking `core::mem::SizedTypeProperties::SIZE`
614
--> RUSTLIB/core/src/mem/mod.rs:LL:CC

tests/ui/consts/const-size_of-cycle.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ note: ...which requires simplifying constant for the type system `core::mem::Siz
1010
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
1111
note: ...which requires const-evaluating + checking `core::mem::SizedTypeProperties::SIZE`...
1212
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
13-
= note: ...which requires computing layout of `Foo`...
13+
note: ...which requires computing layout of `Foo`...
14+
--> $DIR/const-size_of-cycle.rs:1:1
15+
|
16+
LL | struct Foo {
17+
| ^^^^^^^^^^
1418
= note: ...which requires computing layout of `[u8; std::mem::size_of::<Foo>()]`...
1519
note: ...which requires normalizing `[u8; std::mem::size_of::<Foo>()]`...
1620
--> $DIR/const-size_of-cycle.rs:2:17

tests/ui/consts/issue-44415.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`..
99
|
1010
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
= note: ...which requires computing layout of `Foo`...
12+
note: ...which requires computing layout of `Foo`...
13+
--> $DIR/issue-44415.rs:5:1
14+
|
15+
LL | struct Foo {
16+
| ^^^^^^^^^^
1317
= note: ...which requires computing layout of `[u8; unsafe { intrinsics::size_of::<Foo>() }]`...
1418
note: ...which requires normalizing `[u8; unsafe { intrinsics::size_of::<Foo>() }]`...
1519
--> $DIR/issue-44415.rs:6:17

tests/ui/layout/layout-cycle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//@ build-fail
2-
//~^ ERROR: cycle detected when computing layout of
32

43
// Issue #111176 -- ensure that we do not emit ICE on layout cycles
54

65
use std::mem;
76

87
pub struct S<T: Tr> {
8+
//~^ ERROR: cycle detected when computing layout of
99
pub f: <T as Tr>::I,
1010
}
1111

tests/ui/layout/layout-cycle.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
error[E0391]: cycle detected when computing layout of `S<S<()>>`
2+
--> $DIR/layout-cycle.rs:7:1
23
|
3-
= note: ...which requires computing layout of `<S<()> as Tr>::I`...
4+
LL | pub struct S<T: Tr> {
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: ...which requires computing layout of `<S<()> as Tr>::I`...
8+
--> $DIR/layout-cycle.rs:13:5
9+
|
10+
LL | type I: Tr;
11+
| ^^^^^^^^^^
412
= note: ...which again requires computing layout of `S<S<()>>`, completing the cycle
513
note: cycle used when const-evaluating + checking `core::mem::SizedTypeProperties::SIZE`
614
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL

tests/ui/layout/post-mono-layout-cycle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ build-fail
2-
//~^ ERROR cycle detected when computing layout of `Wrapper<()>`
32

43
trait Trait {
54
type Assoc;
@@ -10,6 +9,7 @@ impl Trait for () {
109
}
1110

1211
struct Wrapper<T: Trait> {
12+
//~^ ERROR cycle detected when computing layout of `Wrapper<()>`
1313
_x: <T as Trait>::Assoc,
1414
}
1515

tests/ui/layout/post-mono-layout-cycle.stderr

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
error[E0391]: cycle detected when computing layout of `Wrapper<()>`
2+
--> $DIR/post-mono-layout-cycle.rs:11:1
23
|
3-
= note: ...which requires computing layout of `<() as Trait>::Assoc`...
4+
LL | struct Wrapper<T: Trait> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: ...which requires computing layout of `<() as Trait>::Assoc`...
8+
--> $DIR/post-mono-layout-cycle.rs:4:5
9+
|
10+
LL | type Assoc;
11+
| ^^^^^^^^^^
412
= note: ...which again requires computing layout of `Wrapper<()>`, completing the cycle
5-
= note: cycle used when computing layout of `core::option::Option<Wrapper<()>>`
13+
note: cycle used when computing layout of `core::option::Option<Wrapper<()>>`
14+
--> $SRC_DIR/core/src/option.rs:LL:COL
615
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
716

817
error: aborting due to 1 previous error

tests/ui/pattern/non-structural-match-types-cycle-err.stderr

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ note: ...which requires const-evaluating + checking `<impl at $DIR/non-structura
1414
|
1515
LL | const NONE: Option<T> = None;
1616
| ^^^^^^^^^^^^^^^^^^^^^
17-
= note: ...which requires computing layout of `core::option::Option<{async block@$DIR/non-structural-match-types-cycle-err.rs:18:16: 18:21}>`...
18-
= note: ...which requires computing layout of `{async block@$DIR/non-structural-match-types-cycle-err.rs:18:16: 18:21}`...
17+
note: ...which requires computing layout of `core::option::Option<{async block@$DIR/non-structural-match-types-cycle-err.rs:18:16: 18:21}>`...
18+
--> $SRC_DIR/core/src/option.rs:LL:COL
19+
note: ...which requires computing layout of `{async block@$DIR/non-structural-match-types-cycle-err.rs:18:16: 18:21}`...
20+
--> $DIR/non-structural-match-types-cycle-err.rs:18:16
21+
|
22+
LL | match Some(async {}) {
23+
| ^^^^^
1924
note: ...which requires optimizing MIR for `defines::{closure#0}`...
2025
--> $DIR/non-structural-match-types-cycle-err.rs:18:16
2126
|

tests/ui/recursion/issue-26548-recursion-via-normalize.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
//~ ERROR cycle detected when computing layout of `core::option::Option<S>`
2-
//~| NOTE see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
3-
//~| NOTE ...which requires computing layout of `S`...
4-
//~| NOTE ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
5-
//~| NOTE ...which again requires computing layout of `core::option::Option<S>`, completing the cycle
6-
//~| NOTE cycle used when computing layout of `core::option::Option<<S as Mirror>::It>`
1+
//~? ERROR cycle detected when computing layout of `core::option::Option<S>`
2+
//~? NOTE see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
3+
//~? NOTE ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
4+
//~? NOTE ...which again requires computing layout of `core::option::Option<S>`, completing the cycle
5+
//~? NOTE cycle used when computing layout of `core::option::Option<<S as Mirror>::It>`
76

87
trait Mirror {
98
type It: ?Sized;
@@ -12,6 +11,7 @@ impl<T: ?Sized> Mirror for T {
1211
type It = Self;
1312
}
1413
struct S(Option<<S as Mirror>::It>);
14+
//~^ NOTE ...which requires computing layout of `S`...
1515

1616
fn main() {
1717
let _s = S(None);

0 commit comments

Comments
 (0)