Skip to content

Commit 2269381

Browse files
authored
Rollup merge of rust-lang#129429 - cjgillot:named-variance, r=compiler-errors
Print the generic parameter along with the variance in dumps. This allows to make sure we are testing what we think we are testing. While the tests are correct, I discovered that opaque duplicated args are in the reverse declaration order.
2 parents 198a68d + 5cef88c commit 2269381

32 files changed

+158
-140
lines changed
Lines changed: 24 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 6 additions & 6 deletions
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]
8-
--> $DIR/variance.rs:13:44
7+
error: [Self: o, 'i: *, 'a: *, 'a: o, 'i: o]
8+
--> $DIR/variance.rs:12:44
99
|
1010
LL | fn explicit_capture_early<'a: 'a>() -> impl Sized + Captures<'a> {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: [o, *, o, o]
13+
error: [Self: o, 'i: *, 'a: o, 'i: o]
1414
--> $DIR/variance.rs:15: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:18:48
2121
|
2222
LL | fn explicit_capture_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^

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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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> {}

0 commit comments

Comments
 (0)