Skip to content

Commit 8e50987

Browse files
committed
address review comments
1 parent 7ca135b commit 8e50987

File tree

10 files changed

+67
-38
lines changed

10 files changed

+67
-38
lines changed

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
698698
) {
699699
match (self.tcx.parent_hir_node(expr.hir_id), error) {
700700
(hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), init: Some(init), .. }), _)
701-
if init.hir_id == expr.hir_id && !ty.span.source_equal(init.span) =>
701+
if init.hir_id == expr.hir_id =>
702702
{
703703
// Point at `let` assignment type.
704704
err.span_label(ty.span, "expected due to this");

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
459459
span,
460460
format!("this is an iterator with items of type `{}`", args.type_at(0)),
461461
);
462-
} else if !span.overlaps(cause.span) {
462+
} else {
463463
let expected_ty = self.tcx.short_string(expected_ty, err.long_ty_path());
464464
err.span_label(span, format!("this expression has type `{expected_ty}`"));
465465
}
@@ -1622,12 +1622,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16221622
let f = self.tcx.erase_regions(f);
16231623
let mut expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
16241624
let mut found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
1625-
if let Some(def_id) = cause.span.ctxt().outer_expn_data().macro_def_id
1626-
&& self.tcx.is_diagnostic_item(sym::assert_macro, def_id)
1625+
if let ObligationCauseCode::Pattern { span, .. } = cause.code()
1626+
&& let Some(span) = span
1627+
&& !span.from_expansion()
1628+
&& cause.span.from_expansion()
16271629
{
1628-
// When the type error comes from `assert!()`, the cause and effect are reversed
1629-
// because that macro expands to `match val { false => {panic!()}, _ => {} }`, which
1630-
// would say something like "expected `Type`, found `bool`", confusing the user.
1630+
// When the type error comes from a macro like `assert!()`, and we are pointing at
1631+
// code the user wrote the cause and effect are reversed as the expected value is
1632+
// what the macro expanded to.
16311633
(found, expected) = (expected, found);
16321634
}
16331635
if expected == found {
@@ -2202,14 +2204,22 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
22022204
return None;
22032205
}
22042206
let (mut expected, mut found) = (exp_found.expected, exp_found.found);
2205-
if let Some(def_id) = cause.span.ctxt().outer_expn_data().macro_def_id
2206-
&& self.tcx.is_diagnostic_item(sym::assert_macro, def_id)
2207+
2208+
if let ObligationCauseCode::Pattern { span, .. } = cause.code()
2209+
&& let Some(span) = span
2210+
&& !span.from_expansion()
2211+
&& cause.span.from_expansion()
22072212
{
2208-
// When the type error comes from `assert!()`, the cause and effect are reversed
2209-
// because that macro expands to `match val { false => {panic!()}, _ => {} }`, which
2210-
// would say something like
2213+
// When the type error comes from a macro like `assert!()`, and we are pointing at
2214+
// code the user wrote, the cause and effect are reversed as the expected value is
2215+
// what the macro expanded to. So if the user provided a `Type` when the macro is
2216+
// written in such a way that a `bool` was expected, we want to print:
2217+
// = note: expected `bool`
2218+
// found `Type`"
2219+
// but as far as the compiler is concerned, after expansion what was expected was `Type`
22112220
// = note: expected `Type`
22122221
// found `bool`"
2222+
// so we reverse them here to match user expectation.
22132223
(expected, found) = (found, expected);
22142224
}
22152225

src/tools/clippy/tests/ui/incompatible_msrv.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![warn(clippy::incompatible_msrv)]
2-
#![allow(clippy::diverging_sub_expression)]
32
#![feature(custom_inner_attributes)]
4-
#![allow(stable_features)]
3+
#![allow(stable_features, clippy::diverging_sub_expression)]
54
#![feature(strict_provenance)] // For use in test
65
#![clippy::msrv = "1.3.0"]
76

src/tools/clippy/tests/ui/incompatible_msrv.stderr

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.10.0`
2-
--> tests/ui/incompatible_msrv.rs:17:39
2+
--> tests/ui/incompatible_msrv.rs:16:39
33
|
44
LL | assert_eq!(map.entry("poneyland").key(), &"poneyland");
55
| ^^^^^
@@ -8,37 +8,37 @@ LL | assert_eq!(map.entry("poneyland").key(), &"poneyland");
88
= help: to override `-D warnings` add `#[allow(clippy::incompatible_msrv)]`
99

1010
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.12.0`
11-
--> tests/ui/incompatible_msrv.rs:23:11
11+
--> tests/ui/incompatible_msrv.rs:22:11
1212
|
1313
LL | v.into_key();
1414
| ^^^^^^^^^^
1515

1616
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.4.0`
17-
--> tests/ui/incompatible_msrv.rs:27:5
17+
--> tests/ui/incompatible_msrv.rs:26:5
1818
|
1919
LL | sleep(Duration::new(1, 0));
2020
| ^^^^^
2121

2222
error: current MSRV (Minimum Supported Rust Version) is `1.2.0` but this item is stable since `1.3.0`
23-
--> tests/ui/incompatible_msrv.rs:32:33
23+
--> tests/ui/incompatible_msrv.rs:31:33
2424
|
2525
LL | static NO_BODY_BAD_MSRV: Option<Duration> = None;
2626
| ^^^^^^^^
2727

2828
error: current MSRV (Minimum Supported Rust Version) is `1.2.0` but this item is stable since `1.3.0`
29-
--> tests/ui/incompatible_msrv.rs:39:19
29+
--> tests/ui/incompatible_msrv.rs:38:19
3030
|
3131
LL | let _: Option<Duration> = None;
3232
| ^^^^^^^^
3333

3434
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.43.0`
35-
--> tests/ui/incompatible_msrv.rs:63:17
35+
--> tests/ui/incompatible_msrv.rs:62:17
3636
|
3737
LL | let _ = core::iter::once_with(|| 0);
3838
| ^^^^^^^^^^^^^^^^^^^^^
3939

4040
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.43.0`
41-
--> tests/ui/incompatible_msrv.rs:70:21
41+
--> tests/ui/incompatible_msrv.rs:69:21
4242
|
4343
LL | let _ = core::iter::once_with(|| $msg);
4444
| ^^^^^^^^^^^^^^^^^^^^^
@@ -49,63 +49,63 @@ LL | my_panic!("foo");
4949
= note: this error originates in the macro `my_panic` (in Nightly builds, run with -Z macro-backtrace for more info)
5050

5151
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.43.0`
52-
--> tests/ui/incompatible_msrv.rs:77:13
52+
--> tests/ui/incompatible_msrv.rs:76:13
5353
|
5454
LL | assert!(core::iter::once_with(|| 0).next().is_some());
5555
| ^^^^^^^^^^^^^^^^^^^^^
5656

5757
error: current MSRV (Minimum Supported Rust Version) is `1.80.0` but this item is stable since `1.82.0`
58-
--> tests/ui/incompatible_msrv.rs:90:13
58+
--> tests/ui/incompatible_msrv.rs:89:13
5959
|
6060
LL | let _ = std::iter::repeat_n((), 5);
6161
| ^^^^^^^^^^^^^^^^^^^
6262

6363
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.82.0`
64-
--> tests/ui/incompatible_msrv.rs:101:13
64+
--> tests/ui/incompatible_msrv.rs:100:13
6565
|
6666
LL | let _ = std::iter::repeat_n((), 5);
6767
| ^^^^^^^^^^^^^^^^^^^
6868

6969
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.82.0`
70-
--> tests/ui/incompatible_msrv.rs:106:17
70+
--> tests/ui/incompatible_msrv.rs:105:17
7171
|
7272
LL | let _ = std::iter::repeat_n((), 5);
7373
| ^^^^^^^^^^^^^^^^^^^
7474
|
7575
= note: you may want to conditionally increase the MSRV considered by Clippy using the `clippy::msrv` attribute
7676

7777
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.82.0`
78-
--> tests/ui/incompatible_msrv.rs:111:17
78+
--> tests/ui/incompatible_msrv.rs:110:17
7979
|
8080
LL | let _ = std::iter::repeat_n((), 5);
8181
| ^^^^^^^^^^^^^^^^^^^
8282

8383
error: current MSRV (Minimum Supported Rust Version) is `1.78.0` but this item is stable since `1.84.0`
84-
--> tests/ui/incompatible_msrv.rs:124:7
84+
--> tests/ui/incompatible_msrv.rs:123:7
8585
|
8686
LL | r.isqrt()
8787
| ^^^^^^^
8888

8989
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.85.0`
90-
--> tests/ui/incompatible_msrv.rs:129:13
90+
--> tests/ui/incompatible_msrv.rs:128:13
9191
|
9292
LL | let _ = std::io::ErrorKind::CrossesDevices;
9393
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9494

9595
error: current MSRV (Minimum Supported Rust Version) is `1.87.0` but this item is stable in a `const` context since `1.88.0`
96-
--> tests/ui/incompatible_msrv.rs:141:15
96+
--> tests/ui/incompatible_msrv.rs:140:15
9797
|
9898
LL | _ = c.get();
9999
| ^^^^^
100100

101101
error: current MSRV (Minimum Supported Rust Version) is `1.86.0` but this item is stable since `1.87.0`
102-
--> tests/ui/incompatible_msrv.rs:160:13
102+
--> tests/ui/incompatible_msrv.rs:159:13
103103
|
104104
LL | let _ = std::io::ErrorKind::InvalidFilename;
105105
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106106

107107
error: current MSRV (Minimum Supported Rust Version) is `1.86.0` but this item is stable since `1.87.0`
108-
--> tests/ui/incompatible_msrv.rs:162:21
108+
--> tests/ui/incompatible_msrv.rs:161:21
109109
|
110110
LL | let _ = const { std::io::ErrorKind::InvalidFilename };
111111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/codemap_tests/issue-28308.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
fn main() {
22
assert!("foo"); //~ ERROR mismatched types
33
//~^ NOTE expected `bool`, found `str`
4+
//~| NOTE this expression has type `&str`
45
//~| NOTE in this expansion of assert!
56
let x = Some(&1);
67
assert!(x); //~ ERROR mismatched types
78
//~^ NOTE expected `bool`, found `Option<&{integer}>`
89
//~| NOTE expected enum `bool`
10+
//~| NOTE this expression has type `Option<&{integer}>`
911
//~| NOTE in this expansion of assert!
1012
//~| NOTE in this expansion of assert!
1113
assert!(x, ""); //~ ERROR mismatched types
1214
//~^ NOTE expected `bool`, found `Option<&{integer}>`
1315
//~| NOTE expected enum `bool`
16+
//~| NOTE this expression has type `Option<&{integer}>`
1417
//~| NOTE in this expansion of assert!
1518
//~| NOTE in this expansion of assert!
1619
}

tests/ui/codemap_tests/issue-28308.stderr

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@ error[E0308]: mismatched types
22
--> $DIR/issue-28308.rs:2:13
33
|
44
LL | assert!("foo");
5-
| ^^^^^ expected `bool`, found `str`
5+
| ^^^^^
6+
| |
7+
| expected `bool`, found `str`
8+
| this expression has type `&str`
69

710
error[E0308]: mismatched types
8-
--> $DIR/issue-28308.rs:6:13
11+
--> $DIR/issue-28308.rs:7:13
912
|
1013
LL | assert!(x);
11-
| ^ expected `bool`, found `Option<&{integer}>`
14+
| ^
15+
| |
16+
| expected `bool`, found `Option<&{integer}>`
17+
| this expression has type `Option<&{integer}>`
1218
|
1319
= note: expected enum `bool`
1420
found type `Option<&{integer}>`
1521

1622
error[E0308]: mismatched types
17-
--> $DIR/issue-28308.rs:11:13
23+
--> $DIR/issue-28308.rs:13:13
1824
|
1925
LL | assert!(x, "");
20-
| ^ expected `bool`, found `Option<&{integer}>`
26+
| ^
27+
| |
28+
| expected `bool`, found `Option<&{integer}>`
29+
| this expression has type `Option<&{integer}>`
2130
|
2231
= note: expected enum `bool`
2332
found type `Option<&{integer}>`

tests/ui/proc-macro/quote/does-not-have-iter-interpolated-dup.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | quote!($($nonrep $nonrep)*);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
| |
77
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
8+
| expected due to this
89
| here the type of `has_iter` is inferred to be `ThereIsNoIteratorInRepetition`
910

1011
error: aborting due to 1 previous error

tests/ui/proc-macro/quote/does-not-have-iter-interpolated.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | quote!($($nonrep)*);
55
| ^^^^^^^^^^^^^^^^^^^
66
| |
77
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
8+
| expected due to this
89
| here the type of `has_iter` is inferred to be `ThereIsNoIteratorInRepetition`
910

1011
error: aborting due to 1 previous error

tests/ui/proc-macro/quote/does-not-have-iter-separated.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0308]: mismatched types
22
--> $DIR/does-not-have-iter-separated.rs:8:5
33
|
44
LL | quote!($(a b),*);
5-
| ^^^^^^^^^^^^^^^^ expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
5+
| ^^^^^^^^^^^^^^^^
6+
| |
7+
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
8+
| expected due to this
69

710
error: aborting due to 1 previous error
811

tests/ui/proc-macro/quote/does-not-have-iter.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0308]: mismatched types
22
--> $DIR/does-not-have-iter.rs:8:5
33
|
44
LL | quote!($(a b)*);
5-
| ^^^^^^^^^^^^^^^ expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
5+
| ^^^^^^^^^^^^^^^
6+
| |
7+
| expected `HasIterator`, found `ThereIsNoIteratorInRepetition`
8+
| expected due to this
69

710
error: aborting due to 1 previous error
811

0 commit comments

Comments
 (0)