Skip to content

Commit d37cb3f

Browse files
authored
Rollup merge of #106199 - estebank:quiet-type-err-in-binding, r=compiler-errors
Silence knock-down errors on `[type error]` bindings Fix #56036, fix #76589.
2 parents 08e2e4e + 8e039b6 commit d37cb3f

11 files changed

+76
-140
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13071307
// Type check the initializer.
13081308
if let Some(ref init) = decl.init {
13091309
let init_ty = self.check_decl_initializer(decl.hir_id, decl.pat, &init);
1310-
self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, decl_ty, init_ty);
1310+
self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, init_ty);
13111311
}
13121312

13131313
// Does the expected pattern type originate from an expression and what is the span?
@@ -1322,7 +1322,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13221322
// Type check the pattern. Override if necessary to avoid knock-on errors.
13231323
self.check_pat_top(&decl.pat, decl_ty, ty_span, origin_expr);
13241324
let pat_ty = self.node_ty(decl.pat.hir_id);
1325-
self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, decl_ty, pat_ty);
1325+
self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, pat_ty);
13261326

13271327
if let Some(blk) = decl.els {
13281328
let previous_diverges = self.diverges.get();
@@ -1627,14 +1627,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16271627
&self,
16281628
hir_id: hir::HirId,
16291629
pat: &'tcx hir::Pat<'tcx>,
1630-
decl_ty: Ty<'tcx>,
16311630
ty: Ty<'tcx>,
16321631
) {
16331632
if ty.references_error() {
16341633
// Override the types everywhere with `err()` to avoid knock on errors.
1635-
self.write_ty(hir_id, ty);
1636-
self.write_ty(pat.hir_id, ty);
1637-
let local_ty = LocalTy { decl_ty, revealed_ty: ty };
1634+
let err = self.tcx.ty_error();
1635+
self.write_ty(hir_id, err);
1636+
self.write_ty(pat.hir_id, err);
1637+
let local_ty = LocalTy { decl_ty: err, revealed_ty: err };
16381638
self.locals.borrow_mut().insert(hir_id, local_ty);
16391639
self.locals.borrow_mut().insert(pat.hir_id, local_ty);
16401640
}

src/test/ui/error-codes/E0033-teach.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// compile-flags: -Z teach
2-
32
trait SomeTrait {
4-
fn foo(); //~ associated function `foo` has no `self` parameter
3+
fn foo(&self);
4+
}
5+
struct S;
6+
impl SomeTrait for S {
7+
fn foo(&self) {}
58
}
6-
79
fn main() {
8-
let trait_obj: &dyn SomeTrait = SomeTrait;
9-
//~^ ERROR expected value, found trait `SomeTrait`
10-
//~| ERROR E0038
10+
let trait_obj: &dyn SomeTrait = &S;
1111

1212
let &invalid = trait_obj;
1313
//~^ ERROR E0033
+2-31
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
error[E0423]: expected value, found trait `SomeTrait`
2-
--> $DIR/E0033-teach.rs:8:37
3-
|
4-
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
5-
| ^^^^^^^^^ not a value
6-
7-
error[E0038]: the trait `SomeTrait` cannot be made into an object
8-
--> $DIR/E0033-teach.rs:8:20
9-
|
10-
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
11-
| ^^^^^^^^^^^^^^ `SomeTrait` cannot be made into an object
12-
|
13-
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
14-
--> $DIR/E0033-teach.rs:4:8
15-
|
16-
LL | trait SomeTrait {
17-
| --------- this trait cannot be made into an object...
18-
LL | fn foo();
19-
| ^^^ ...because associated function `foo` has no `self` parameter
20-
help: consider turning `foo` into a method by giving it a `&self` argument
21-
|
22-
LL | fn foo(&self);
23-
| +++++
24-
help: alternatively, consider constraining `foo` so it does not apply to trait objects
25-
|
26-
LL | fn foo() where Self: Sized;
27-
| +++++++++++++++++
28-
291
error[E0033]: type `&dyn SomeTrait` cannot be dereferenced
302
--> $DIR/E0033-teach.rs:12:9
313
|
@@ -36,7 +8,6 @@ LL | let &invalid = trait_obj;
368

379
You can read more about trait objects in the Trait Objects section of the Reference: https://doc.rust-lang.org/reference/types.html#trait-objects
3810

39-
error: aborting due to 3 previous errors
11+
error: aborting due to previous error
4012

41-
Some errors have detailed explanations: E0033, E0038, E0423.
42-
For more information about an error, try `rustc --explain E0033`.
13+
For more information about this error, try `rustc --explain E0033`.

src/test/ui/error-codes/E0033.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
trait SomeTrait {
2-
fn foo(); //~ associated function `foo` has no `self` parameter
2+
fn foo(&self);
3+
}
4+
struct S;
5+
impl SomeTrait for S {
6+
fn foo(&self) {}
37
}
4-
58
fn main() {
6-
let trait_obj: &dyn SomeTrait = SomeTrait;
7-
//~^ ERROR expected value, found trait `SomeTrait`
8-
//~| ERROR E0038
9+
let trait_obj: &dyn SomeTrait = &S;
910

1011
let &invalid = trait_obj;
1112
//~^ ERROR E0033

src/test/ui/error-codes/E0033.stderr

+3-32
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,9 @@
1-
error[E0423]: expected value, found trait `SomeTrait`
2-
--> $DIR/E0033.rs:6:37
3-
|
4-
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
5-
| ^^^^^^^^^ not a value
6-
7-
error[E0038]: the trait `SomeTrait` cannot be made into an object
8-
--> $DIR/E0033.rs:6:20
9-
|
10-
LL | let trait_obj: &dyn SomeTrait = SomeTrait;
11-
| ^^^^^^^^^^^^^^ `SomeTrait` cannot be made into an object
12-
|
13-
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
14-
--> $DIR/E0033.rs:2:8
15-
|
16-
LL | trait SomeTrait {
17-
| --------- this trait cannot be made into an object...
18-
LL | fn foo();
19-
| ^^^ ...because associated function `foo` has no `self` parameter
20-
help: consider turning `foo` into a method by giving it a `&self` argument
21-
|
22-
LL | fn foo(&self);
23-
| +++++
24-
help: alternatively, consider constraining `foo` so it does not apply to trait objects
25-
|
26-
LL | fn foo() where Self: Sized;
27-
| +++++++++++++++++
28-
291
error[E0033]: type `&dyn SomeTrait` cannot be dereferenced
30-
--> $DIR/E0033.rs:10:9
2+
--> $DIR/E0033.rs:11:9
313
|
324
LL | let &invalid = trait_obj;
335
| ^^^^^^^^ type `&dyn SomeTrait` cannot be dereferenced
346

35-
error: aborting due to 3 previous errors
7+
error: aborting due to previous error
368

37-
Some errors have detailed explanations: E0033, E0038, E0423.
38-
For more information about an error, try `rustc --explain E0033`.
9+
For more information about this error, try `rustc --explain E0033`.

src/test/ui/lexer/lex-bad-char-literals-6.rs

-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ fn main() {
77
//~^ ERROR: character literal may only contain one codepoint
88

99
if x == y {}
10-
//~^ ERROR: can't compare `&str` with `char`
1110
if y == z {} // no error here
1211
if x == z {}
13-
//~^ ERROR: can't compare `&str` with `char`
1412

1513
let a: usize = "";
1614
//~^ ERROR: mismatched types

src/test/ui/lexer/lex-bad-char-literals-6.stderr

+3-38
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,14 @@ help: if you meant to write a `str` literal, use double quotes
3131
LL | let z = "ef";
3232
| ~~~~
3333

34-
error[E0277]: can't compare `&str` with `char`
35-
--> $DIR/lex-bad-char-literals-6.rs:9:10
36-
|
37-
LL | if x == y {}
38-
| ^^ no implementation for `&str == char`
39-
|
40-
= help: the trait `PartialEq<char>` is not implemented for `&str`
41-
= help: the following other types implement trait `PartialEq<Rhs>`:
42-
<&'a str as PartialEq<OsString>>
43-
<&'a str as PartialEq<String>>
44-
<&'b str as PartialEq<Cow<'a, str>>>
45-
<str as PartialEq<Cow<'a, str>>>
46-
<str as PartialEq<OsStr>>
47-
<str as PartialEq<OsString>>
48-
<str as PartialEq<String>>
49-
<str as PartialEq>
50-
5134
error[E0308]: mismatched types
52-
--> $DIR/lex-bad-char-literals-6.rs:15:20
35+
--> $DIR/lex-bad-char-literals-6.rs:13:20
5336
|
5437
LL | let a: usize = "";
5538
| ----- ^^ expected `usize`, found `&str`
5639
| |
5740
| expected due to this
5841

59-
error[E0277]: can't compare `&str` with `char`
60-
--> $DIR/lex-bad-char-literals-6.rs:12:10
61-
|
62-
LL | if x == z {}
63-
| ^^ no implementation for `&str == char`
64-
|
65-
= help: the trait `PartialEq<char>` is not implemented for `&str`
66-
= help: the following other types implement trait `PartialEq<Rhs>`:
67-
<&'a str as PartialEq<OsString>>
68-
<&'a str as PartialEq<String>>
69-
<&'b str as PartialEq<Cow<'a, str>>>
70-
<str as PartialEq<Cow<'a, str>>>
71-
<str as PartialEq<OsStr>>
72-
<str as PartialEq<OsString>>
73-
<str as PartialEq<String>>
74-
<str as PartialEq>
75-
76-
error: aborting due to 6 previous errors
42+
error: aborting due to 4 previous errors
7743

78-
Some errors have detailed explanations: E0277, E0308.
79-
For more information about an error, try `rustc --explain E0277`.
44+
For more information about this error, try `rustc --explain E0308`.
+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// The purpose of this test is not to validate the output of the compiler.
22
// Instead, it ensures the suggestion is generated without performing an arithmetic overflow.
33

4+
struct S;
5+
impl S {
6+
fn foo(&self) {}
7+
}
48
fn main() {
5-
let x = not_found; //~ ERROR cannot find value `not_found` in this scope
6-
simd_gt::<()>(x);
9+
let x = S;
10+
foo::<()>(x);
711
//~^ ERROR this associated function takes 0 generic arguments but 1 generic argument was supplied
8-
//~| ERROR cannot find function `simd_gt` in this scope
12+
//~| ERROR cannot find function `foo` in this scope
913
}
+17-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
error[E0425]: cannot find value `not_found` in this scope
2-
--> $DIR/issue-104287.rs:5:13
3-
|
4-
LL | let x = not_found;
5-
| ^^^^^^^^^ not found in this scope
6-
71
error[E0107]: this associated function takes 0 generic arguments but 1 generic argument was supplied
8-
--> $DIR/issue-104287.rs:6:5
2+
--> $DIR/issue-104287.rs:10:5
93
|
10-
LL | simd_gt::<()>(x);
11-
| ^^^^^^^------ help: remove these generics
4+
LL | foo::<()>(x);
5+
| ^^^------ help: remove these generics
126
| |
137
| expected 0 generic arguments
8+
|
9+
note: associated function defined here, with 0 generic parameters
10+
--> $DIR/issue-104287.rs:6:8
11+
|
12+
LL | fn foo(&self) {}
13+
| ^^^
1414

15-
error[E0425]: cannot find function `simd_gt` in this scope
16-
--> $DIR/issue-104287.rs:6:5
15+
error[E0425]: cannot find function `foo` in this scope
16+
--> $DIR/issue-104287.rs:10:5
1717
|
18-
LL | simd_gt::<()>(x);
19-
| ^^^^^^^ not found in this scope
18+
LL | foo::<()>(x);
19+
| ^^^ not found in this scope
2020
|
21-
help: use the `.` operator to call the method `SimdPartialOrd::simd_gt` on `[type error]`
21+
help: use the `.` operator to call the method `foo` on `&S`
2222
|
23-
LL - simd_gt::<()>(x);
24-
LL + x.simd_gt();
23+
LL - foo::<()>(x);
24+
LL + x.foo();
2525
|
2626

27-
error: aborting due to 3 previous errors
27+
error: aborting due to 2 previous errors
2828

2929
Some errors have detailed explanations: E0107, E0425.
3030
For more information about an error, try `rustc --explain E0107`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// fn foo() -> String {
2+
// String::new()
3+
// }
4+
5+
fn test(s: &str) {
6+
println!("{}", s);
7+
}
8+
9+
fn test2(s: String) {
10+
println!("{}", s);
11+
}
12+
13+
fn main() {
14+
let x = foo(); //~ERROR cannot find function `foo` in this scope
15+
test(&x);
16+
test2(x); // Does not complain about `x` being a `&str`.
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find function `foo` in this scope
2+
--> $DIR/quiet-type-err-let-binding.rs:14:13
3+
|
4+
LL | let x = foo();
5+
| ^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)