Skip to content

Commit ecdb5e9

Browse files
authoredOct 8, 2019
Rollup merge of rust-lang#65154 - skinny121:const-arg-diagnostic, r=varkor
Fix const generic arguments not displaying in types mismatch diagnostic Fixes rust-lang#61395
2 parents 5422ed7 + 74eac92 commit ecdb5e9

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed
 

‎src/librustc/infer/error_reporting/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
935935
.filter(|(a, b)| a == b)
936936
.count();
937937
let len = sub1.len() - common_default_params;
938+
let consts_offset = len - sub1.consts().count();
938939

939940
// Only draw `<...>` if there're lifetime/type arguments.
940941
if len > 0 {
@@ -981,7 +982,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
981982
// ^ elided type as this type argument was the same in both sides
982983
let type_arguments = sub1.types().zip(sub2.types());
983984
let regions_len = sub1.regions().count();
984-
for (i, (ta1, ta2)) in type_arguments.take(len).enumerate() {
985+
let num_display_types = consts_offset - regions_len;
986+
for (i, (ta1, ta2)) in type_arguments.take(num_display_types).enumerate() {
985987
let i = i + regions_len;
986988
if ta1 == ta2 {
987989
values.0.push_normal("_");
@@ -994,6 +996,21 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
994996
self.push_comma(&mut values.0, &mut values.1, len, i);
995997
}
996998

999+
// Do the same for const arguments, if they are equal, do not highlight and
1000+
// elide them from the output.
1001+
let const_arguments = sub1.consts().zip(sub2.consts());
1002+
for (i, (ca1, ca2)) in const_arguments.enumerate() {
1003+
let i = i + consts_offset;
1004+
if ca1 == ca2 {
1005+
values.0.push_normal("_");
1006+
values.1.push_normal("_");
1007+
} else {
1008+
values.0.push_highlighted(ca1.to_string());
1009+
values.1.push_highlighted(ca2.to_string());
1010+
}
1011+
self.push_comma(&mut values.0, &mut values.1, len, i);
1012+
}
1013+
9971014
// Close the type argument bracket.
9981015
// Only draw `<...>` if there're lifetime/type arguments.
9991016
if len > 0 {

‎src/test/ui/const-generics/slice-const-param-mismatch.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@ error[E0308]: mismatched types
1212
LL | let _: ConstString<"Hello"> = ConstString::<"World">;
1313
| ^^^^^^^^^^^^^^^^^^^^^^ expected `"Hello"`, found `"World"`
1414
|
15-
= note: expected type `ConstString<>`
16-
found type `ConstString<>`
15+
= note: expected type `ConstString<"Hello">`
16+
found type `ConstString<"World">`
1717

1818
error[E0308]: mismatched types
1919
--> $DIR/slice-const-param-mismatch.rs:11:33
2020
|
2121
LL | let _: ConstString<"ℇ㇈↦"> = ConstString::<"ℇ㇈↥">;
2222
| ^^^^^^^^^^^^^^^^^^^^^ expected `"ℇ㇈↦"`, found `"ℇ㇈↥"`
2323
|
24-
= note: expected type `ConstString<>`
25-
found type `ConstString<>`
24+
= note: expected type `ConstString<"ℇ㇈↦">`
25+
found type `ConstString<"ℇ㇈↥">`
2626

2727
error[E0308]: mismatched types
2828
--> $DIR/slice-const-param-mismatch.rs:13:33
2929
|
3030
LL | let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
3131
| ^^^^^^^^^^^^^^^^^^^^ expected `b"AAA"`, found `b"BBB"`
3232
|
33-
= note: expected type `ConstBytes<>`
34-
found type `ConstBytes<>`
33+
= note: expected type `ConstBytes<b"AAA">`
34+
found type `ConstBytes<b"BBB">`
3535

3636
error: aborting due to 3 previous errors
3737

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(const_generics)]
2+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
3+
4+
// tests the diagnostic output of type mismatches for types that have const generics arguments.
5+
6+
use std::marker::PhantomData;
7+
8+
struct A<'a, T, const X: u32, const Y: u32> {
9+
data: PhantomData<&'a T>
10+
}
11+
12+
fn a<'a, 'b>() {
13+
let _: A<'a, u32, {2u32}, {3u32}> = A::<'a, u32, {4u32}, {3u32}> { data: PhantomData };
14+
//~^ ERROR mismatched types
15+
let _: A<'a, u16, {2u32}, {3u32}> = A::<'b, u32, {2u32}, {3u32}> { data: PhantomData };
16+
//~^ ERROR mismatched types
17+
}
18+
19+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/types-mismatch-const-args.rs:1:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/types-mismatch-const-args.rs:13:41
11+
|
12+
LL | let _: A<'a, u32, {2u32}, {3u32}> = A::<'a, u32, {4u32}, {3u32}> { data: PhantomData };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `2u32`, found `4u32`
14+
|
15+
= note: expected type `A<'_, _, 2u32, _>`
16+
found type `A<'_, _, 4u32, _>`
17+
18+
error[E0308]: mismatched types
19+
--> $DIR/types-mismatch-const-args.rs:15:41
20+
|
21+
LL | let _: A<'a, u16, {2u32}, {3u32}> = A::<'b, u32, {2u32}, {3u32}> { data: PhantomData };
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u16, found u32
23+
|
24+
= note: expected type `A<'a, u16, _, _>`
25+
found type `A<'b, u32, _, _>`
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)
Please sign in to comment.