Skip to content

Commit 5da3a2f

Browse files
committed
enhance check_pat_lit with TopInfo
1 parent 9b6e0e8 commit 5da3a2f

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

src/librustc_typeck/check/demand.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4343
expected: Ty<'tcx>,
4444
actual: Ty<'tcx>,
4545
) -> Option<DiagnosticBuilder<'tcx>> {
46-
let cause = &self.misc(sp);
46+
self.demand_suptype_with_origin(&self.misc(sp), expected, actual)
47+
}
48+
49+
pub fn demand_suptype_with_origin(
50+
&self,
51+
cause: &ObligationCause<'tcx>,
52+
expected: Ty<'tcx>,
53+
actual: Ty<'tcx>,
54+
) -> Option<DiagnosticBuilder<'tcx>> {
4755
match self.at(cause, self.param_env).sup(expected, actual) {
4856
Ok(InferOk { obligations, value: () }) => {
4957
self.register_predicates(obligations);

src/librustc_typeck/check/pat.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::pat_util::EnumerateAndAdjustIterator;
99
use rustc_hir::{HirId, Pat, PatKind};
1010
use rustc_infer::infer;
1111
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
12-
use rustc_infer::traits::Pattern;
12+
use rustc_infer::traits::{ObligationCause, Pattern};
1313
use rustc_span::hygiene::DesugaringKind;
1414
use rustc_span::source_map::{Span, Spanned};
1515
use syntax::ast;
@@ -66,16 +66,19 @@ struct TopInfo<'tcx> {
6666
}
6767

6868
impl<'tcx> FnCtxt<'_, 'tcx> {
69+
fn pattern_cause(&self, ti: TopInfo<'tcx>, cause_span: Span) -> ObligationCause<'tcx> {
70+
let code = Pattern { span: ti.span, root_ty: ti.expected, origin_expr: ti.origin_expr };
71+
self.cause(cause_span, code)
72+
}
73+
6974
fn demand_eqtype_pat_diag(
7075
&self,
7176
cause_span: Span,
7277
expected: Ty<'tcx>,
7378
actual: Ty<'tcx>,
7479
ti: TopInfo<'tcx>,
7580
) -> Option<DiagnosticBuilder<'tcx>> {
76-
let code = Pattern { span: ti.span, root_ty: ti.expected, origin_expr: ti.origin_expr };
77-
let cause = self.cause(cause_span, code);
78-
self.demand_eqtype_with_origin(&cause, expected, actual)
81+
self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual)
7982
}
8083

8184
fn demand_eqtype_pat(
@@ -379,7 +382,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
379382
// &'static str <: expected
380383
//
381384
// then that's equivalent to there existing a LUB.
382-
if let Some(mut err) = self.demand_suptype_diag(span, expected, pat_ty) {
385+
let cause = self.pattern_cause(ti, span);
386+
if let Some(mut err) = self.demand_suptype_with_origin(&cause, expected, pat_ty) {
383387
err.emit_unless(
384388
ti.span
385389
.filter(|&s| {

src/test/ui/match/match-ill-type2.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0308]: mismatched types
22
--> $DIR/match-ill-type2.rs:4:9
33
|
4+
LL | match 1i32 {
5+
| ---- this expression has type `i32`
6+
LL | 1i32 => 1,
47
LL | 2u32 => 1,
58
| ^^^^ expected `i32`, found `u32`
69

src/test/ui/rfc-2005-default-binding-mode/lit.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0308]: mismatched types
22
--> $DIR/lit.rs:7:13
33
|
4+
LL | match &s {
5+
| -- this expression has type `&&str`
46
LL | "abc" => true,
57
| ^^^^^ expected `&str`, found `str`
68
|
@@ -10,6 +12,8 @@ LL | "abc" => true,
1012
error[E0308]: mismatched types
1113
--> $DIR/lit.rs:16:9
1214
|
15+
LL | match &s {
16+
| -- this expression has type `&&[u8]`
1317
LL | b"abc" => true,
1418
| ^^^^^^ expected `&[u8]`, found array `[u8; 3]`
1519
|

src/test/ui/slightly-nice-generic-literal-messages.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0308]: mismatched types
22
--> $DIR/slightly-nice-generic-literal-messages.rs:7:9
33
|
4+
LL | match Foo(1.1, marker::PhantomData) {
5+
| ----------------------------- this expression has type `Foo<{float}, _>`
46
LL | 1 => {}
57
| ^ expected struct `Foo`, found integer
68
|

0 commit comments

Comments
 (0)