Skip to content

Commit 6cf0bc0

Browse files
committed
Print the generic parameter along with the variance in dumps.
1 parent a32d4a0 commit 6cf0bc0

32 files changed

+158
-140
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1+
use std::fmt::Write;
2+
13
use rustc_hir::def::DefKind;
2-
use rustc_hir::def_id::CRATE_DEF_ID;
3-
use rustc_middle::ty::TyCtxt;
4+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
5+
use rustc_middle::ty::{GenericArgs, TyCtxt};
46
use rustc_span::symbol::sym;
57

8+
fn format_variances(tcx: TyCtxt<'_>, def_id: LocalDefId) -> String {
9+
let variances = tcx.variances_of(def_id);
10+
let generics = GenericArgs::identity_for_item(tcx, def_id);
11+
// 7 = 2-letter parameter + ": " + 1-letter variance + ", "
12+
let mut ret = String::with_capacity(2 + 7 * variances.len());
13+
ret.push('[');
14+
for (arg, variance) in generics.iter().zip(variances.iter()) {
15+
write!(ret, "{arg}: {variance:?}, ").unwrap();
16+
}
17+
// Remove trailing `, `.
18+
if !variances.is_empty() {
19+
ret.pop();
20+
ret.pop();
21+
}
22+
ret.push(']');
23+
ret
24+
}
25+
626
pub(crate) fn variances(tcx: TyCtxt<'_>) {
727
if tcx.has_attr(CRATE_DEF_ID, sym::rustc_variance_of_opaques) {
828
for id in tcx.hir().items() {
929
let DefKind::OpaqueTy = tcx.def_kind(id.owner_id) else { continue };
1030

11-
let variances = tcx.variances_of(id.owner_id);
12-
1331
tcx.dcx().emit_err(crate::errors::VariancesOf {
1432
span: tcx.def_span(id.owner_id),
15-
variances: format!("{variances:?}"),
33+
variances: format_variances(tcx, id.owner_id.def_id),
1634
});
1735
}
1836
}
@@ -22,11 +40,9 @@ pub(crate) fn variances(tcx: TyCtxt<'_>) {
2240
continue;
2341
}
2442

25-
let variances = tcx.variances_of(id.owner_id);
26-
2743
tcx.dcx().emit_err(crate::errors::VariancesOf {
2844
span: tcx.def_span(id.owner_id),
29-
variances: format!("{variances:?}"),
45+
variances: format_variances(tcx, id.owner_id.def_id),
3046
});
3147
}
3248
}

tests/ui/error-codes/E0208.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(rustc_attrs)]
22

33
#[rustc_variance]
4-
struct Foo<'a, T> { //~ ERROR [+, o]
4+
struct Foo<'a, T> { //~ ERROR ['a: +, T: o]
55
t: &'a mut T,
66
}
77

tests/ui/error-codes/E0208.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: [+, o]
1+
error: ['a: +, T: o]
22
--> $DIR/E0208.rs:4:1
33
|
44
LL | struct Foo<'a, T> {

tests/ui/impl-trait/capture-lifetime-not-in-hir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ trait Bar<'a> {
66
}
77

88
fn foo<'a, T: Bar<'a>>() -> impl Into<T::Assoc> {
9-
//~^ ERROR [o, o]
9+
//~^ ERROR ['a: o, T: o]
1010
// captures both T and 'a invariantly
1111
()
1212
}
1313

1414
fn foo2<'a, T: Bar<'a>>() -> impl Into<T::Assoc> + 'a {
15-
//~^ ERROR [o, o, o]
15+
//~^ ERROR ['a: o, T: o, 'a: o]
1616
// captures both T and 'a invariantly, and also duplicates `'a`
1717
// i.e. the opaque looks like `impl Into<<T as Bar<'a>>::Assoc> + 'a_duplicated`
1818
()

tests/ui/impl-trait/capture-lifetime-not-in-hir.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: [o, o]
1+
error: ['a: o, T: o]
22
--> $DIR/capture-lifetime-not-in-hir.rs:8:29
33
|
44
LL | fn foo<'a, T: Bar<'a>>() -> impl Into<T::Assoc> {
55
| ^^^^^^^^^^^^^^^^^^^
66

7-
error: [o, o, o]
7+
error: ['a: o, T: o, 'a: o]
88
--> $DIR/capture-lifetime-not-in-hir.rs:14:30
99
|
1010
LL | fn foo2<'a, T: Bar<'a>>() -> impl Into<T::Assoc> + 'a {

tests/ui/impl-trait/implicit-capture-late.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: lifetime declared here
1010
LL | fn foo(x: Vec<i32>) -> Box<dyn for<'a> Deref<Target = impl ?Sized>> {
1111
| ^^
1212

13-
error: [o]
13+
error: ['a: o]
1414
--> $DIR/implicit-capture-late.rs:10:55
1515
|
1616
LL | fn foo(x: Vec<i32>) -> Box<dyn for<'a> Deref<Target = impl ?Sized>> {

tests/ui/impl-trait/in-trait/variance.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ impl<T> Captures<'_> for T {}
77

88
trait Foo<'i> {
99
fn implicit_capture_early<'a: 'a>() -> impl Sized {}
10-
//~^ [o, *, *, o, o]
11-
// Self, 'i, 'a, 'i_duplicated, 'a_duplicated
10+
//~^ [Self: o, 'i: *, 'a: *, 'a: o, 'i: o]
1211

13-
fn explicit_capture_early<'a: 'a>() -> impl Sized + Captures<'a> {} //~ [o, *, *, o, o]
12+
fn explicit_capture_early<'a: 'a>() -> impl Sized + Captures<'a> {}
13+
//~^ [Self: o, 'i: *, 'a: *, 'a: o, 'i: o]
1414

15-
fn implicit_capture_late<'a>(_: &'a ()) -> impl Sized {} //~ [o, *, o, o]
15+
fn implicit_capture_late<'a>(_: &'a ()) -> impl Sized {}
16+
//~^ [Self: o, 'i: *, 'a: o, 'i: o]
1617

17-
fn explicit_capture_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {} //~ [o, *, o, o]
18+
fn explicit_capture_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}
19+
//~^ [Self: o, 'i: *, 'a: o, 'i: o]
1820
}
1921

2022
fn main() {}

tests/ui/impl-trait/in-trait/variance.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
error: [o, *, *, o, o]
1+
error: [Self: o, 'i: *, 'a: *, 'a: o, 'i: o]
22
--> $DIR/variance.rs:9:44
33
|
44
LL | fn implicit_capture_early<'a: 'a>() -> impl Sized {}
55
| ^^^^^^^^^^
66

7-
error: [o, *, *, o, o]
7+
error: [Self: o, 'i: *, 'a: *, 'a: o, 'i: o]
88
--> $DIR/variance.rs:13:44
99
|
1010
LL | fn explicit_capture_early<'a: 'a>() -> impl Sized + Captures<'a> {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: [o, *, o, o]
14-
--> $DIR/variance.rs:15:48
13+
error: [Self: o, 'i: *, 'a: o, 'i: o]
14+
--> $DIR/variance.rs:16:48
1515
|
1616
LL | fn implicit_capture_late<'a>(_: &'a ()) -> impl Sized {}
1717
| ^^^^^^^^^^
1818

19-
error: [o, *, o, o]
20-
--> $DIR/variance.rs:17:48
19+
error: [Self: o, 'i: *, 'a: o, 'i: o]
20+
--> $DIR/variance.rs:19:48
2121
|
2222
LL | fn explicit_capture_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/impl-trait/variance.e2024.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
error: [*, o]
1+
error: ['a: *, 'a: o]
22
--> $DIR/variance.rs:14:36
33
|
44
LL | fn not_captured_early<'a: 'a>() -> impl Sized {}
55
| ^^^^^^^^^^
66

7-
error: [*, o]
7+
error: ['a: *, 'a: o]
88
--> $DIR/variance.rs:19:32
99
|
1010
LL | fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: [o]
13+
error: ['a: o]
1414
--> $DIR/variance.rs:21:40
1515
|
1616
LL | fn not_captured_late<'a>(_: &'a ()) -> impl Sized {}
1717
| ^^^^^^^^^^
1818

19-
error: [o]
19+
error: ['a: o]
2020
--> $DIR/variance.rs:26:36
2121
|
2222
LL | fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}

tests/ui/impl-trait/variance.new.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
error: [*, o]
1+
error: ['a: *, 'a: o]
22
--> $DIR/variance.rs:14:36
33
|
44
LL | fn not_captured_early<'a: 'a>() -> impl Sized {}
55
| ^^^^^^^^^^
66

7-
error: [*, o]
7+
error: ['a: *, 'a: o]
88
--> $DIR/variance.rs:19:32
99
|
1010
LL | fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: [o]
13+
error: ['a: o]
1414
--> $DIR/variance.rs:21:40
1515
|
1616
LL | fn not_captured_late<'a>(_: &'a ()) -> impl Sized {}
1717
| ^^^^^^^^^^
1818

19-
error: [o]
19+
error: ['a: o]
2020
--> $DIR/variance.rs:26:36
2121
|
2222
LL | fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}

tests/ui/impl-trait/variance.old.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: [*]
1+
error: ['a: *]
22
--> $DIR/variance.rs:14:36
33
|
44
LL | fn not_captured_early<'a: 'a>() -> impl Sized {}
55
| ^^^^^^^^^^
66

7-
error: [*, o]
7+
error: ['a: *, 'a: o]
88
--> $DIR/variance.rs:19:32
99
|
1010
LL | fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {}
@@ -16,7 +16,7 @@ error: []
1616
LL | fn not_captured_late<'a>(_: &'a ()) -> impl Sized {}
1717
| ^^^^^^^^^^
1818

19-
error: [o]
19+
error: ['a: o]
2020
--> $DIR/variance.rs:26:36
2121
|
2222
LL | fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}

tests/ui/impl-trait/variance.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ trait Captures<'a> {}
1212
impl<T> Captures<'_> for T {}
1313

1414
fn not_captured_early<'a: 'a>() -> impl Sized {}
15-
//[old]~^ [*]
16-
//[new]~^^ [*, o]
17-
//[e2024]~^^^ [*, o]
15+
//[old]~^ ['a: *]
16+
//[new]~^^ ['a: *, 'a: o]
17+
//[e2024]~^^^ ['a: *, 'a: o]
1818

19-
fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {} //~ [*, o]
19+
fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {} //~ ['a: *, 'a: o]
2020

2121
fn not_captured_late<'a>(_: &'a ()) -> impl Sized {}
2222
//[old]~^ []
23-
//[new]~^^ [o]
24-
//[e2024]~^^^ [o]
23+
//[new]~^^ ['a: o]
24+
//[e2024]~^^^ ['a: o]
2525

26-
fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {} //~ [o]
26+
fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {} //~ ['a: o]
2727

2828
fn main() {}

tests/ui/type-alias-impl-trait/variance.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
trait Captures<'a> {}
66
impl<T> Captures<'_> for T {}
77

8-
type NotCapturedEarly<'a> = impl Sized; //~ [*, o]
8+
type NotCapturedEarly<'a> = impl Sized; //~ ['a: *, 'a: o]
99
//~^ ERROR: unconstrained opaque type
1010

11-
type CapturedEarly<'a> = impl Sized + Captures<'a>; //~ [*, o]
11+
type CapturedEarly<'a> = impl Sized + Captures<'a>; //~ ['a: *, 'a: o]
1212
//~^ ERROR: unconstrained opaque type
1313

14-
type NotCapturedLate<'a> = dyn for<'b> Iterator<Item = impl Sized>; //~ [*, o, o]
14+
type NotCapturedLate<'a> = dyn for<'b> Iterator<Item = impl Sized>; //~ ['a: *, 'b: o, 'a: o]
1515
//~^ ERROR `impl Trait` cannot capture higher-ranked lifetime from `dyn` type
1616
//~| ERROR: unconstrained opaque type
1717

18-
type Captured<'a> = dyn for<'b> Iterator<Item = impl Sized + Captures<'a>>; //~ [*, o, o]
18+
type Captured<'a> = dyn for<'b> Iterator<Item = impl Sized + Captures<'a>>; //~ ['a: *, 'b: o, 'a: o]
1919
//~^ ERROR `impl Trait` cannot capture higher-ranked lifetime from `dyn` type
2020
//~| ERROR: unconstrained opaque type
2121

22-
type Bar<'a, 'b: 'b, T> = impl Sized; //~ ERROR [*, *, o, o, o]
22+
type Bar<'a, 'b: 'b, T> = impl Sized; //~ ERROR ['a: *, 'b: *, T: o, 'a: o, 'b: o]
2323
//~^ ERROR: unconstrained opaque type
2424

2525
trait Foo<'i> {
@@ -31,24 +31,24 @@ trait Foo<'i> {
3131
}
3232

3333
impl<'i> Foo<'i> for &'i () {
34-
type ImplicitCapture<'a> = impl Sized; //~ [*, *, o, o]
34+
type ImplicitCapture<'a> = impl Sized; //~ ['i: *, 'a: *, 'a: o, 'i: o]
3535
//~^ ERROR: unconstrained opaque type
3636

37-
type ExplicitCaptureFromHeader<'a> = impl Sized + Captures<'i>; //~ [*, *, o, o]
37+
type ExplicitCaptureFromHeader<'a> = impl Sized + Captures<'i>; //~ ['i: *, 'a: *, 'a: o, 'i: o]
3838
//~^ ERROR: unconstrained opaque type
3939

40-
type ExplicitCaptureFromGat<'a> = impl Sized + Captures<'a>; //~ [*, *, o, o]
40+
type ExplicitCaptureFromGat<'a> = impl Sized + Captures<'a>; //~ ['i: *, 'a: *, 'a: o, 'i: o]
4141
//~^ ERROR: unconstrained opaque type
4242
}
4343

4444
impl<'i> Foo<'i> for () {
45-
type ImplicitCapture<'a> = impl Sized; //~ [*, *, o, o]
45+
type ImplicitCapture<'a> = impl Sized; //~ ['i: *, 'a: *, 'a: o, 'i: o]
4646
//~^ ERROR: unconstrained opaque type
4747

48-
type ExplicitCaptureFromHeader<'a> = impl Sized + Captures<'i>; //~ [*, *, o, o]
48+
type ExplicitCaptureFromHeader<'a> = impl Sized + Captures<'i>; //~ ['i: *, 'a: *, 'a: o, 'i: o]
4949
//~^ ERROR: unconstrained opaque type
5050

51-
type ExplicitCaptureFromGat<'a> = impl Sized + Captures<'a>; //~ [*, *, o, o]
51+
type ExplicitCaptureFromGat<'a> = impl Sized + Captures<'a>; //~ ['i: *, 'a: *, 'a: o, 'i: o]
5252
//~^ ERROR: unconstrained opaque type
5353
}
5454

@@ -59,15 +59,15 @@ impl<'a> Nesting<'a> for &'a () {
5959
type Output = &'a ();
6060
}
6161
type NestedDeeply<'a> =
62-
impl Nesting< //~ [*, o]
62+
impl Nesting< //~ ['a: *, 'a: o]
6363
'a,
64-
Output = impl Nesting< //~ [*, o]
64+
Output = impl Nesting< //~ ['a: *, 'a: o]
6565
'a,
66-
Output = impl Nesting< //~ [*, o]
66+
Output = impl Nesting< //~ ['a: *, 'a: o]
6767
'a,
68-
Output = impl Nesting< //~ [*, o]
68+
Output = impl Nesting< //~ ['a: *, 'a: o]
6969
'a,
70-
Output = impl Nesting<'a> //~ [*, o]
70+
Output = impl Nesting<'a> //~ ['a: *, 'a: o]
7171
>
7272
>,
7373
>,

0 commit comments

Comments
 (0)