Skip to content

Commit 5880ce3

Browse files
committed
Fix const arguments not displaying in types mismatch diagnostic.
1 parent 7870050 commit 5880ce3

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-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

0 commit comments

Comments
 (0)