Skip to content

Commit

Permalink
Move Sized check before first error is created
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jul 1, 2022
1 parent 12ab6bf commit 6711313
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
62 changes: 31 additions & 31 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,37 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
if predicate.references_error() {
return;
}

// This is kind of a hack: it frequently happens that some earlier
// error prevents types from being fully inferred, and then we get
// a bunch of uninteresting errors saying something like "<generic
// #0> doesn't implement Sized". It may even be true that we
// could just skip over all checks where the self-ty is an
// inference variable, but I was afraid that there might be an
// inference variable created, registered as an obligation, and
// then never forced by writeback, and hence by skipping here we'd
// be ignoring the fact that we don't KNOW the type works
// out. Though even that would probably be harmless, given that
// we're only talking about builtin traits, which are known to be
// inhabited. We used to check for `self.tcx.sess.has_errors()` to
// avoid inundating the user with unnecessary errors, but we now
// check upstream for type errors and don't add the obligations to
// begin with in those cases.
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
if !self.is_tainted_by_errors() {
self.emit_inference_failure_err(
body_id,
span,
trait_ref.self_ty().skip_binder().into(),
vec![],
ErrorCode::E0282,
false,
)
.emit();
}
return;
}

// Typically, this ambiguity should only happen if
// there are unresolved type inference variables
// (otherwise it would suggest a coherence
Expand Down Expand Up @@ -1997,37 +2028,6 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
)
};

// This is kind of a hack: it frequently happens that some earlier
// error prevents types from being fully inferred, and then we get
// a bunch of uninteresting errors saying something like "<generic
// #0> doesn't implement Sized". It may even be true that we
// could just skip over all checks where the self-ty is an
// inference variable, but I was afraid that there might be an
// inference variable created, registered as an obligation, and
// then never forced by writeback, and hence by skipping here we'd
// be ignoring the fact that we don't KNOW the type works
// out. Though even that would probably be harmless, given that
// we're only talking about builtin traits, which are known to be
// inhabited. We used to check for `self.tcx.sess.has_errors()` to
// avoid inundating the user with unnecessary errors, but we now
// check upstream for type errors and don't add the obligations to
// begin with in those cases.
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
if !self.is_tainted_by_errors() {
self.emit_inference_failure_err(
body_id,
span,
trait_ref.self_ty().skip_binder().into(),
vec![],
ErrorCode::E0282,
false,
)
.emit();
}
err.cancel();
return;
}

let obligation = Obligation::new(
obligation.cause.clone(),
obligation.param_env,
Expand Down
12 changes: 4 additions & 8 deletions src/test/ui/lazy-type-alias-impl-trait/branches3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ error[E0282]: type annotations needed
--> $DIR/branches3.rs:8:10
|
LL | |s| s.len()
| ^
| ^ - type must be known at this point
|
= note: type must be known at this point
help: consider giving this closure parameter an explicit type
|
LL | |s: _| s.len()
Expand All @@ -14,9 +13,8 @@ error[E0282]: type annotations needed
--> $DIR/branches3.rs:15:10
|
LL | |s| s.len()
| ^
| ^ - type must be known at this point
|
= note: type must be known at this point
help: consider giving this closure parameter an explicit type
|
LL | |s: _| s.len()
Expand All @@ -26,9 +24,8 @@ error[E0282]: type annotations needed
--> $DIR/branches3.rs:23:10
|
LL | |s| s.len()
| ^
| ^ - type must be known at this point
|
= note: type must be known at this point
help: consider giving this closure parameter an explicit type
|
LL | |s: _| s.len()
Expand All @@ -38,9 +35,8 @@ error[E0282]: type annotations needed
--> $DIR/branches3.rs:30:10
|
LL | |s| s.len()
| ^
| ^ - type must be known at this point
|
= note: type must be known at this point
help: consider giving this closure parameter an explicit type
|
LL | |s: _| s.len()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ error[E0282]: type annotations needed
--> $DIR/closures_in_branches.rs:7:10
|
LL | |x| x.len()
| ^
| ^ - type must be known at this point
|
= note: type must be known at this point
help: consider giving this closure parameter an explicit type
|
LL | |x: _| x.len()
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/type-alias-impl-trait/fallback.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0283]: type annotations needed
--> $DIR/fallback.rs:24:5
|
LL | fn unconstrained_foo() -> Wrapper<Foo> {
| ------------ type must be known at this point
LL | Wrapper::Second
| ^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the enum `Wrapper`
|
Expand Down

0 comments on commit 6711313

Please sign in to comment.