Skip to content

Commit 6711313

Browse files
Move Sized check before first error is created
1 parent 12ab6bf commit 6711313

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

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

+31-31
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,37 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
19581958
if predicate.references_error() {
19591959
return;
19601960
}
1961+
1962+
// This is kind of a hack: it frequently happens that some earlier
1963+
// error prevents types from being fully inferred, and then we get
1964+
// a bunch of uninteresting errors saying something like "<generic
1965+
// #0> doesn't implement Sized". It may even be true that we
1966+
// could just skip over all checks where the self-ty is an
1967+
// inference variable, but I was afraid that there might be an
1968+
// inference variable created, registered as an obligation, and
1969+
// then never forced by writeback, and hence by skipping here we'd
1970+
// be ignoring the fact that we don't KNOW the type works
1971+
// out. Though even that would probably be harmless, given that
1972+
// we're only talking about builtin traits, which are known to be
1973+
// inhabited. We used to check for `self.tcx.sess.has_errors()` to
1974+
// avoid inundating the user with unnecessary errors, but we now
1975+
// check upstream for type errors and don't add the obligations to
1976+
// begin with in those cases.
1977+
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
1978+
if !self.is_tainted_by_errors() {
1979+
self.emit_inference_failure_err(
1980+
body_id,
1981+
span,
1982+
trait_ref.self_ty().skip_binder().into(),
1983+
vec![],
1984+
ErrorCode::E0282,
1985+
false,
1986+
)
1987+
.emit();
1988+
}
1989+
return;
1990+
}
1991+
19611992
// Typically, this ambiguity should only happen if
19621993
// there are unresolved type inference variables
19631994
// (otherwise it would suggest a coherence
@@ -1997,37 +2028,6 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
19972028
)
19982029
};
19992030

2000-
// This is kind of a hack: it frequently happens that some earlier
2001-
// error prevents types from being fully inferred, and then we get
2002-
// a bunch of uninteresting errors saying something like "<generic
2003-
// #0> doesn't implement Sized". It may even be true that we
2004-
// could just skip over all checks where the self-ty is an
2005-
// inference variable, but I was afraid that there might be an
2006-
// inference variable created, registered as an obligation, and
2007-
// then never forced by writeback, and hence by skipping here we'd
2008-
// be ignoring the fact that we don't KNOW the type works
2009-
// out. Though even that would probably be harmless, given that
2010-
// we're only talking about builtin traits, which are known to be
2011-
// inhabited. We used to check for `self.tcx.sess.has_errors()` to
2012-
// avoid inundating the user with unnecessary errors, but we now
2013-
// check upstream for type errors and don't add the obligations to
2014-
// begin with in those cases.
2015-
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
2016-
if !self.is_tainted_by_errors() {
2017-
self.emit_inference_failure_err(
2018-
body_id,
2019-
span,
2020-
trait_ref.self_ty().skip_binder().into(),
2021-
vec![],
2022-
ErrorCode::E0282,
2023-
false,
2024-
)
2025-
.emit();
2026-
}
2027-
err.cancel();
2028-
return;
2029-
}
2030-
20312031
let obligation = Obligation::new(
20322032
obligation.cause.clone(),
20332033
obligation.param_env,

src/test/ui/lazy-type-alias-impl-trait/branches3.stderr

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error[E0282]: type annotations needed
22
--> $DIR/branches3.rs:8:10
33
|
44
LL | |s| s.len()
5-
| ^
5+
| ^ - type must be known at this point
66
|
7-
= note: type must be known at this point
87
help: consider giving this closure parameter an explicit type
98
|
109
LL | |s: _| s.len()
@@ -14,9 +13,8 @@ error[E0282]: type annotations needed
1413
--> $DIR/branches3.rs:15:10
1514
|
1615
LL | |s| s.len()
17-
| ^
16+
| ^ - type must be known at this point
1817
|
19-
= note: type must be known at this point
2018
help: consider giving this closure parameter an explicit type
2119
|
2220
LL | |s: _| s.len()
@@ -26,9 +24,8 @@ error[E0282]: type annotations needed
2624
--> $DIR/branches3.rs:23:10
2725
|
2826
LL | |s| s.len()
29-
| ^
27+
| ^ - type must be known at this point
3028
|
31-
= note: type must be known at this point
3229
help: consider giving this closure parameter an explicit type
3330
|
3431
LL | |s: _| s.len()
@@ -38,9 +35,8 @@ error[E0282]: type annotations needed
3835
--> $DIR/branches3.rs:30:10
3936
|
4037
LL | |s| s.len()
41-
| ^
38+
| ^ - type must be known at this point
4239
|
43-
= note: type must be known at this point
4440
help: consider giving this closure parameter an explicit type
4541
|
4642
LL | |s: _| s.len()

src/test/ui/type-alias-impl-trait/closures_in_branches.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error[E0282]: type annotations needed
22
--> $DIR/closures_in_branches.rs:7:10
33
|
44
LL | |x| x.len()
5-
| ^
5+
| ^ - type must be known at this point
66
|
7-
= note: type must be known at this point
87
help: consider giving this closure parameter an explicit type
98
|
109
LL | |x: _| x.len()

src/test/ui/type-alias-impl-trait/fallback.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0283]: type annotations needed
22
--> $DIR/fallback.rs:24:5
33
|
4+
LL | fn unconstrained_foo() -> Wrapper<Foo> {
5+
| ------------ type must be known at this point
46
LL | Wrapper::Second
57
| ^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the enum `Wrapper`
68
|

0 commit comments

Comments
 (0)