Skip to content

Commit 395ce41

Browse files
committed
Correct "expected"/"found" on assert! type errors
1 parent 817683c commit 395ce41

File tree

5 files changed

+62
-10
lines changed

5 files changed

+62
-10
lines changed

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,8 +1596,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
15961596
{
15971597
let e = self.tcx.erase_regions(e);
15981598
let f = self.tcx.erase_regions(f);
1599-
let expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
1600-
let found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
1599+
let mut expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
1600+
let mut found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
1601+
if let Some(def_id) = cause.span.ctxt().outer_expn_data().macro_def_id
1602+
&& self.tcx.is_diagnostic_item(sym::assert_macro, def_id)
1603+
{
1604+
// When the type error comes from `assert!()`, the cause and effect are reversed
1605+
// because that macro expands to `match val { false => {panic!()}, _ => {} }`, which
1606+
// would say something like "expected `Type`, found `bool`", confusing the user.
1607+
(found, expected) = (expected, found);
1608+
}
16011609
if expected == found {
16021610
label_or_note(span, terr.to_string(self.tcx));
16031611
} else {
@@ -2114,7 +2122,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21142122
) -> Option<(DiagStyledString, DiagStyledString, Option<PathBuf>)> {
21152123
match values {
21162124
ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
2117-
ValuePairs::Terms(exp_found) => self.expected_found_str_term(exp_found),
2125+
ValuePairs::Terms(exp_found) => self.expected_found_str_term(cause, exp_found),
21182126
ValuePairs::Aliases(exp_found) => self.expected_found_str(exp_found),
21192127
ValuePairs::ExistentialTraitRef(exp_found) => self.expected_found_str(exp_found),
21202128
ValuePairs::ExistentialProjection(exp_found) => self.expected_found_str(exp_found),
@@ -2155,14 +2163,26 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21552163

21562164
fn expected_found_str_term(
21572165
&self,
2166+
cause: &ObligationCause<'tcx>,
21582167
exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
21592168
) -> Option<(DiagStyledString, DiagStyledString, Option<PathBuf>)> {
21602169
let exp_found = self.resolve_vars_if_possible(exp_found);
21612170
if exp_found.references_error() {
21622171
return None;
21632172
}
2173+
let (mut expected, mut found) = (exp_found.expected, exp_found.found);
2174+
if let Some(def_id) = cause.span.ctxt().outer_expn_data().macro_def_id
2175+
&& self.tcx.is_diagnostic_item(sym::assert_macro, def_id)
2176+
{
2177+
// When the type error comes from `assert!()`, the cause and effect are reversed
2178+
// because that macro expands to `match val { false => {panic!()}, _ => {} }`, which
2179+
// would say something like
2180+
// = note: expected `Type`
2181+
// found `bool`"
2182+
(expected, found) = (found, expected);
2183+
}
21642184

2165-
Some(match (exp_found.expected.unpack(), exp_found.found.unpack()) {
2185+
Some(match (expected.unpack(), found.unpack()) {
21662186
(ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
21672187
let (mut exp, mut fnd) = self.cmp(expected, found);
21682188
// Use the terminal width as the basis to determine when to compress the printed
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
fn main() {
2-
assert!("foo");
3-
//~^ ERROR mismatched types
2+
assert!("foo"); //~ ERROR mismatched types
3+
//~^ NOTE expected `bool`, found `str`
4+
//~| NOTE in this expansion of assert!
5+
let x = Some(&1);
6+
assert!(x); //~ ERROR mismatched types
7+
//~^ NOTE expected `bool`, found `Option<&{integer}>`
8+
//~| NOTE expected enum `bool`
9+
//~| NOTE in this expansion of assert!
10+
//~| NOTE in this expansion of assert!
11+
assert!(x, ""); //~ ERROR mismatched types
12+
//~^ NOTE expected `bool`, found `Option<&{integer}>`
13+
//~| NOTE expected enum `bool`
14+
//~| NOTE in this expansion of assert!
15+
//~| NOTE in this expansion of assert!
416
}

tests/ui/codemap_tests/issue-28308.stderr

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@ error[E0308]: mismatched types
22
--> $DIR/issue-28308.rs:2:13
33
|
44
LL | assert!("foo");
5-
| ^^^^^ expected `str`, found `bool`
5+
| ^^^^^ expected `bool`, found `str`
66
|
77
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9-
error: aborting due to 1 previous error
9+
error[E0308]: mismatched types
10+
--> $DIR/issue-28308.rs:6:13
11+
|
12+
LL | assert!(x);
13+
| ^ expected `bool`, found `Option<&{integer}>`
14+
|
15+
= note: expected enum `bool`
16+
found type `Option<&{integer}>`
17+
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/issue-28308.rs:11:13
21+
|
22+
LL | assert!(x, "");
23+
| ^ expected `bool`, found `Option<&{integer}>`
24+
|
25+
= note: expected enum `bool`
26+
found type `Option<&{integer}>`
27+
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
28+
29+
error: aborting due to 3 previous errors
1030

1131
For more information about this error, try `rustc --explain E0308`.

tests/ui/issues/issue-14091-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-14091-2.rs:15:13
33
|
44
LL | assert!(x, x);
5-
| ^ expected `BytePos`, found `bool`
5+
| ^ expected `bool`, found `BytePos`
66
|
77
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
88

tests/ui/issues/issue-14091.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-14091.rs:2:13
33
|
44
LL | assert!(1,1);
5-
| ^ expected integer, found `bool`
5+
| ^ expected `bool`, found integer
66
|
77
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
88

0 commit comments

Comments
 (0)