Skip to content

Commit 33370fd

Browse files
committedMar 23, 2021
Update to not have extra match
1 parent 7116bb5 commit 33370fd

File tree

3 files changed

+85
-28
lines changed

3 files changed

+85
-28
lines changed
 

‎compiler/rustc_infer/src/infer/error_reporting/mod.rs

+10-28
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc_hir::{Item, ItemKind, Node};
6767
use rustc_middle::ty::error::TypeError;
6868
use rustc_middle::ty::{
6969
self,
70-
subst::{Subst, SubstsRef},
70+
subst::{GenericArgKind, Subst, SubstsRef},
7171
Region, Ty, TyCtxt, TypeFoldable,
7272
};
7373
use rustc_span::{sym, BytePos, DesugaringKind, Pos, Span};
@@ -958,42 +958,24 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
958958
let generics = self.tcx.generics_of(def_id);
959959
let mut num_supplied_defaults = 0;
960960

961-
#[derive(PartialEq, Eq, Copy, Clone)]
962-
enum Kind {
963-
Const,
964-
Type,
965-
}
966961
let default_params = generics.params.iter().rev().filter_map(|param| match param.kind {
967-
ty::GenericParamDefKind::Type { has_default: true, .. } => {
968-
Some((param.def_id, Kind::Type))
969-
}
970-
ty::GenericParamDefKind::Const { has_default: true } => {
971-
Some((param.def_id, Kind::Const))
972-
}
962+
ty::GenericParamDefKind::Type { has_default: true, .. } => Some(param.def_id),
963+
ty::GenericParamDefKind::Const { has_default: true } => Some(param.def_id),
973964
_ => None,
974965
});
975-
let mut types = substs.types().rev();
976-
let mut consts = substs.consts().rev();
977-
for (def_id, kind) in default_params {
978-
match kind {
979-
Kind::Const => {
980-
if let Some(actual) = consts.next() {
981-
if ty::Const::from_anon_const(self.tcx, def_id.expect_local()) != actual {
982-
break;
983-
}
984-
} else {
966+
for (def_id, actual) in default_params.zip(substs.iter().rev()) {
967+
match actual.unpack() {
968+
GenericArgKind::Const(c) => {
969+
if self.tcx.const_param_default(def_id).subst(self.tcx, substs) != c {
985970
break;
986971
}
987972
}
988-
Kind::Type => {
989-
if let Some(actual) = types.next() {
990-
if self.tcx.type_of(def_id).subst(self.tcx, substs) != actual {
991-
break;
992-
}
993-
} else {
973+
GenericArgKind::Type(ty) => {
974+
if self.tcx.type_of(def_id).subst(self.tcx, substs) != ty {
994975
break;
995976
}
996977
}
978+
_ => break,
997979
}
998980
num_supplied_defaults += 1;
999981
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(const_generics)]
2+
#![feature(const_generics_defaults)]
3+
#![allow(incomplete_features)]
4+
5+
pub struct Example<const N: usize=13>;
6+
pub struct Example2<T=u32, const N: usize=13>(T);
7+
pub struct Example3<const N: usize=13, T=u32>(T);
8+
pub struct Example4<const N: usize=13, const M: usize=4>;
9+
10+
fn main() {
11+
let e: Example::<13> = ();
12+
//~^ Error: mismatched types
13+
let e: Example2::<u32, 13> = ();
14+
//~^ Error: mismatched types
15+
let e: Example3::<13, u32> = ();
16+
//~^ Error: mismatched types
17+
let e: Example3::<7> = ();
18+
//~^ Error: mismatched types
19+
// FIXME(const_generics_defaults): There should be a note for the error below, but it is
20+
// missing.
21+
let e: Example4::<7> = ();
22+
//~^ Error: mismatched types
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/mismatch.rs:11:26
3+
|
4+
LL | let e: Example::<13> = ();
5+
| ------------- ^^ expected struct `Example`, found `()`
6+
| |
7+
| expected due to this
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/mismatch.rs:13:32
11+
|
12+
LL | let e: Example2::<u32, 13> = ();
13+
| ------------------- ^^ expected struct `Example2`, found `()`
14+
| |
15+
| expected due to this
16+
|
17+
= note: expected struct `Example2`
18+
found unit type `()`
19+
20+
error[E0308]: mismatched types
21+
--> $DIR/mismatch.rs:15:32
22+
|
23+
LL | let e: Example3::<13, u32> = ();
24+
| ------------------- ^^ expected struct `Example3`, found `()`
25+
| |
26+
| expected due to this
27+
|
28+
= note: expected struct `Example3`
29+
found unit type `()`
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/mismatch.rs:17:26
33+
|
34+
LL | let e: Example3::<7> = ();
35+
| ------------- ^^ expected struct `Example3`, found `()`
36+
| |
37+
| expected due to this
38+
|
39+
= note: expected struct `Example3<7_usize>`
40+
found unit type `()`
41+
42+
error[E0308]: mismatched types
43+
--> $DIR/mismatch.rs:21:26
44+
|
45+
LL | let e: Example4::<7> = ();
46+
| ------------- ^^ expected struct `Example4`, found `()`
47+
| |
48+
| expected due to this
49+
50+
error: aborting due to 5 previous errors
51+
52+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)