diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index c289176c3038..7ed6cbf93dab 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -43,7 +43,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expected: Ty<'tcx>, actual: Ty<'tcx>, ) -> Option> { - let cause = &self.misc(sp); + self.demand_suptype_with_origin(&self.misc(sp), expected, actual) + } + + pub fn demand_suptype_with_origin( + &self, + cause: &ObligationCause<'tcx>, + expected: Ty<'tcx>, + actual: Ty<'tcx>, + ) -> Option> { match self.at(cause, self.param_env).sup(expected, actual) { Ok(InferOk { obligations, value: () }) => { self.register_predicates(obligations); diff --git a/src/librustc_typeck/check/pat.rs b/src/librustc_typeck/check/pat.rs index 3fdae81c766c..a4312e41ee87 100644 --- a/src/librustc_typeck/check/pat.rs +++ b/src/librustc_typeck/check/pat.rs @@ -9,7 +9,7 @@ use rustc_hir::pat_util::EnumerateAndAdjustIterator; use rustc_hir::{HirId, Pat, PatKind}; use rustc_infer::infer; use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; -use rustc_infer::traits::Pattern; +use rustc_infer::traits::{ObligationCause, Pattern}; use rustc_span::hygiene::DesugaringKind; use rustc_span::source_map::{Span, Spanned}; use syntax::ast; @@ -66,6 +66,11 @@ struct TopInfo<'tcx> { } impl<'tcx> FnCtxt<'_, 'tcx> { + fn pattern_cause(&self, ti: TopInfo<'tcx>, cause_span: Span) -> ObligationCause<'tcx> { + let code = Pattern { span: ti.span, root_ty: ti.expected, origin_expr: ti.origin_expr }; + self.cause(cause_span, code) + } + fn demand_eqtype_pat_diag( &self, cause_span: Span, @@ -73,9 +78,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> { actual: Ty<'tcx>, ti: TopInfo<'tcx>, ) -> Option> { - let code = Pattern { span: ti.span, root_ty: ti.expected, origin_expr: ti.origin_expr }; - let cause = self.cause(cause_span, code); - self.demand_eqtype_with_origin(&cause, expected, actual) + self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual) } fn demand_eqtype_pat( @@ -379,7 +382,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // &'static str <: expected // // then that's equivalent to there existing a LUB. - if let Some(mut err) = self.demand_suptype_diag(span, expected, pat_ty) { + let cause = self.pattern_cause(ti, span); + if let Some(mut err) = self.demand_suptype_with_origin(&cause, expected, pat_ty) { err.emit_unless( ti.span .filter(|&s| { diff --git a/src/test/ui/match/match-ill-type2.stderr b/src/test/ui/match/match-ill-type2.stderr index 1cf61ebad435..5078f03d6017 100644 --- a/src/test/ui/match/match-ill-type2.stderr +++ b/src/test/ui/match/match-ill-type2.stderr @@ -1,6 +1,9 @@ error[E0308]: mismatched types --> $DIR/match-ill-type2.rs:4:9 | +LL | match 1i32 { + | ---- this expression has type `i32` +LL | 1i32 => 1, LL | 2u32 => 1, | ^^^^ expected `i32`, found `u32` diff --git a/src/test/ui/rfc-2005-default-binding-mode/lit.stderr b/src/test/ui/rfc-2005-default-binding-mode/lit.stderr index b0d60c7a4c83..6d18a39606cc 100644 --- a/src/test/ui/rfc-2005-default-binding-mode/lit.stderr +++ b/src/test/ui/rfc-2005-default-binding-mode/lit.stderr @@ -1,6 +1,8 @@ error[E0308]: mismatched types --> $DIR/lit.rs:7:13 | +LL | match &s { + | -- this expression has type `&&str` LL | "abc" => true, | ^^^^^ expected `&str`, found `str` | @@ -10,6 +12,8 @@ LL | "abc" => true, error[E0308]: mismatched types --> $DIR/lit.rs:16:9 | +LL | match &s { + | -- this expression has type `&&[u8]` LL | b"abc" => true, | ^^^^^^ expected `&[u8]`, found array `[u8; 3]` | diff --git a/src/test/ui/slightly-nice-generic-literal-messages.stderr b/src/test/ui/slightly-nice-generic-literal-messages.stderr index 61eabed95042..14f01f0ebdf7 100644 --- a/src/test/ui/slightly-nice-generic-literal-messages.stderr +++ b/src/test/ui/slightly-nice-generic-literal-messages.stderr @@ -1,6 +1,8 @@ error[E0308]: mismatched types --> $DIR/slightly-nice-generic-literal-messages.rs:7:9 | +LL | match Foo(1.1, marker::PhantomData) { + | ----------------------------- this expression has type `Foo<{float}, _>` LL | 1 => {} | ^ expected struct `Foo`, found integer |