From 33370fd9829c5bb843b117d11cc7e948f4b824f6 Mon Sep 17 00:00:00 2001 From: kadmin Date: Mon, 22 Mar 2021 23:26:07 +0000 Subject: [PATCH] Update to not have extra match --- .../src/infer/error_reporting/mod.rs | 38 ++++---------- .../ui/const-generics/defaults/mismatch.rs | 23 ++++++++ .../const-generics/defaults/mismatch.stderr | 52 +++++++++++++++++++ 3 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 src/test/ui/const-generics/defaults/mismatch.rs create mode 100644 src/test/ui/const-generics/defaults/mismatch.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 98904206b215c..c171b11e3ffa7 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -67,7 +67,7 @@ use rustc_hir::{Item, ItemKind, Node}; use rustc_middle::ty::error::TypeError; use rustc_middle::ty::{ self, - subst::{Subst, SubstsRef}, + subst::{GenericArgKind, Subst, SubstsRef}, Region, Ty, TyCtxt, TypeFoldable, }; use rustc_span::{sym, BytePos, DesugaringKind, Pos, Span}; @@ -958,42 +958,24 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { let generics = self.tcx.generics_of(def_id); let mut num_supplied_defaults = 0; - #[derive(PartialEq, Eq, Copy, Clone)] - enum Kind { - Const, - Type, - } let default_params = generics.params.iter().rev().filter_map(|param| match param.kind { - ty::GenericParamDefKind::Type { has_default: true, .. } => { - Some((param.def_id, Kind::Type)) - } - ty::GenericParamDefKind::Const { has_default: true } => { - Some((param.def_id, Kind::Const)) - } + ty::GenericParamDefKind::Type { has_default: true, .. } => Some(param.def_id), + ty::GenericParamDefKind::Const { has_default: true } => Some(param.def_id), _ => None, }); - let mut types = substs.types().rev(); - let mut consts = substs.consts().rev(); - for (def_id, kind) in default_params { - match kind { - Kind::Const => { - if let Some(actual) = consts.next() { - if ty::Const::from_anon_const(self.tcx, def_id.expect_local()) != actual { - break; - } - } else { + for (def_id, actual) in default_params.zip(substs.iter().rev()) { + match actual.unpack() { + GenericArgKind::Const(c) => { + if self.tcx.const_param_default(def_id).subst(self.tcx, substs) != c { break; } } - Kind::Type => { - if let Some(actual) = types.next() { - if self.tcx.type_of(def_id).subst(self.tcx, substs) != actual { - break; - } - } else { + GenericArgKind::Type(ty) => { + if self.tcx.type_of(def_id).subst(self.tcx, substs) != ty { break; } } + _ => break, } num_supplied_defaults += 1; } diff --git a/src/test/ui/const-generics/defaults/mismatch.rs b/src/test/ui/const-generics/defaults/mismatch.rs new file mode 100644 index 0000000000000..bf578468bb617 --- /dev/null +++ b/src/test/ui/const-generics/defaults/mismatch.rs @@ -0,0 +1,23 @@ +#![feature(const_generics)] +#![feature(const_generics_defaults)] +#![allow(incomplete_features)] + +pub struct Example; +pub struct Example2(T); +pub struct Example3(T); +pub struct Example4; + +fn main() { + let e: Example::<13> = (); + //~^ Error: mismatched types + let e: Example2:: = (); + //~^ Error: mismatched types + let e: Example3::<13, u32> = (); + //~^ Error: mismatched types + let e: Example3::<7> = (); + //~^ Error: mismatched types + // FIXME(const_generics_defaults): There should be a note for the error below, but it is + // missing. + let e: Example4::<7> = (); + //~^ Error: mismatched types +} diff --git a/src/test/ui/const-generics/defaults/mismatch.stderr b/src/test/ui/const-generics/defaults/mismatch.stderr new file mode 100644 index 0000000000000..c66eb4cd64594 --- /dev/null +++ b/src/test/ui/const-generics/defaults/mismatch.stderr @@ -0,0 +1,52 @@ +error[E0308]: mismatched types + --> $DIR/mismatch.rs:11:26 + | +LL | let e: Example::<13> = (); + | ------------- ^^ expected struct `Example`, found `()` + | | + | expected due to this + +error[E0308]: mismatched types + --> $DIR/mismatch.rs:13:32 + | +LL | let e: Example2:: = (); + | ------------------- ^^ expected struct `Example2`, found `()` + | | + | expected due to this + | + = note: expected struct `Example2` + found unit type `()` + +error[E0308]: mismatched types + --> $DIR/mismatch.rs:15:32 + | +LL | let e: Example3::<13, u32> = (); + | ------------------- ^^ expected struct `Example3`, found `()` + | | + | expected due to this + | + = note: expected struct `Example3` + found unit type `()` + +error[E0308]: mismatched types + --> $DIR/mismatch.rs:17:26 + | +LL | let e: Example3::<7> = (); + | ------------- ^^ expected struct `Example3`, found `()` + | | + | expected due to this + | + = note: expected struct `Example3<7_usize>` + found unit type `()` + +error[E0308]: mismatched types + --> $DIR/mismatch.rs:21:26 + | +LL | let e: Example4::<7> = (); + | ------------- ^^ expected struct `Example4`, found `()` + | | + | expected due to this + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0308`.