Skip to content

Commit 72940c7

Browse files
Manually register some bounds for a better span
1 parent e4c1a00 commit 72940c7

11 files changed

+84
-8
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::codes::*;
88
use rustc_hir::def::{CtorKind, DefKind};
99
use rustc_hir::{Node, intravisit};
1010
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
11-
use rustc_infer::traits::Obligation;
11+
use rustc_infer::traits::{Obligation, ObligationCauseCode};
1212
use rustc_lint_defs::builtin::{
1313
REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS, UNSUPPORTED_FN_PTR_CALLING_CONVENTIONS,
1414
};
@@ -305,8 +305,32 @@ fn check_opaque_meets_bounds<'tcx>(
305305
_ => re,
306306
});
307307

308-
let misc_cause = traits::ObligationCause::misc(span, def_id);
308+
// HACK: We eagerly instantiate some bounds to report better errors for them...
309+
// This isn't necessary for correctness, since we register these bounds when
310+
// equating the opaque below, but we should clean this up in the new solver.
311+
for (predicate, pred_span) in
312+
tcx.explicit_item_bounds(def_id).iter_instantiated_copied(tcx, args)
313+
{
314+
let predicate = predicate.fold_with(&mut BottomUpFolder {
315+
tcx,
316+
ty_op: |ty| if ty == opaque_ty { hidden_ty } else { ty },
317+
lt_op: |lt| lt,
318+
ct_op: |ct| ct,
319+
});
320+
321+
ocx.register_obligation(Obligation::new(
322+
tcx,
323+
ObligationCause::new(
324+
span,
325+
def_id,
326+
ObligationCauseCode::WhereClause(def_id.to_def_id(), pred_span),
327+
),
328+
param_env,
329+
predicate,
330+
));
331+
}
309332

333+
let misc_cause = ObligationCause::misc(span, def_id);
310334
// FIXME: We should just register the item bounds here, rather than equating.
311335
match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
312336
Ok(()) => {}

tests/ui/async-await/issue-70935-complex-spans.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ note: required because it's used within this `async` block
3535
|
3636
LL | async move {
3737
| ^^^^^^^^^^
38+
note: required by a bound in `foo::{opaque#0}`
39+
--> $DIR/issue-70935-complex-spans.rs:15:37
40+
|
41+
LL | fn foo(x: NotSync) -> impl Future + Send {
42+
| ^^^^ required by this bound in `foo::{opaque#0}`
3843

3944
error[E0277]: `*mut ()` cannot be shared between threads safely
4045
--> $DIR/issue-70935-complex-spans.rs:15:23

tests/ui/type-alias-impl-trait/bounds-are-checked-2.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0277]: the trait bound `T: Clone` is not satisfied
44
LL | t
55
| ^ the trait `Clone` is not implemented for `T`
66
|
7+
note: required by a bound in `X::{opaque#0}`
8+
--> $DIR/bounds-are-checked-2.rs:7:26
9+
|
10+
LL | pub type X<T> = impl Clone;
11+
| ^^^^^ required by this bound in `X::{opaque#0}`
712
help: consider restricting type parameter `T`
813
|
914
LL | pub type X<T: std::clone::Clone> = impl Clone;

tests/ui/type-alias-impl-trait/generic_duplicate_param_use2.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0277]: `T` doesn't implement `Debug`
44
LL | t
55
| ^ `T` cannot be formatted using `{:?}` because it doesn't implement `Debug`
66
|
7+
note: required by a bound in `Two::{opaque#0}`
8+
--> $DIR/generic_duplicate_param_use2.rs:8:23
9+
|
10+
LL | type Two<T, U> = impl Debug;
11+
| ^^^^^ required by this bound in `Two::{opaque#0}`
712
help: consider restricting type parameter `T`
813
|
914
LL | type Two<T: std::fmt::Debug, U> = impl Debug;

tests/ui/type-alias-impl-trait/generic_duplicate_param_use4.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0277]: `U` doesn't implement `Debug`
44
LL | u
55
| ^ `U` cannot be formatted using `{:?}` because it doesn't implement `Debug`
66
|
7+
note: required by a bound in `Two::{opaque#0}`
8+
--> $DIR/generic_duplicate_param_use4.rs:8:23
9+
|
10+
LL | type Two<T, U> = impl Debug;
11+
| ^^^^^ required by this bound in `Two::{opaque#0}`
712
help: consider restricting type parameter `U`
813
|
914
LL | type Two<T, U: std::fmt::Debug> = impl Debug;

tests/ui/type-alias-impl-trait/hidden_type_mismatch.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ LL | impl<T: Proj<Assoc = i32> + Copy> Copy for Bar<T> {}
2121
| ----------- ^^^^ ^^^^^^
2222
| |
2323
| unsatisfied trait bound introduced here
24+
note: required by a bound in `Tait::{opaque#0}`
25+
--> $DIR/hidden_type_mismatch.rs:36:26
26+
|
27+
LL | pub type Tait = impl Copy + From<Bar<()>> + Into<Bar<()>>;
28+
| ^^^^ required by this bound in `Tait::{opaque#0}`
2429

2530
error: aborting due to 1 previous error
2631

tests/ui/type-alias-impl-trait/implied_lifetime_wf_check4_static.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ LL | s
55
| ^
66
| |
77
| the parameter type `A` must be valid for the static lifetime...
8-
| ...so that the type `A` will meet its required lifetime bounds
8+
| ...so that the type `A` will meet its required lifetime bounds...
99
|
10+
note: ...that is required by this bound
11+
--> $DIR/implied_lifetime_wf_check4_static.rs:4:35
12+
|
13+
LL | pub type Ty<A> = impl Sized + 'static;
14+
| ^^^^^^^
1015
help: consider adding an explicit lifetime bound
1116
|
1217
LL | pub type Ty<A: 'static> = impl Sized + 'static;

tests/ui/type-alias-impl-trait/issue-52843.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0277]: the trait bound `T: Default` is not satisfied
44
LL | t
55
| ^ the trait `Default` is not implemented for `T`
66
|
7+
note: required by a bound in `Foo::{opaque#0}`
8+
--> $DIR/issue-52843.rs:3:20
9+
|
10+
LL | type Foo<T> = impl Default;
11+
| ^^^^^^^ required by this bound in `Foo::{opaque#0}`
712
help: consider restricting type parameter `T`
813
|
914
LL | type Foo<T: std::default::Default> = impl Default;

tests/ui/type-alias-impl-trait/issue-90400-2.stderr

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ error[E0277]: the trait bound `B: Bar` is not satisfied
44
LL | MyBaz(bar)
55
| ^^^^^^^^^^ the trait `Bar` is not implemented for `B`
66
|
7-
note: required by a bound in `MyBaz`
8-
--> $DIR/issue-90400-2.rs:29:17
7+
note: required for `MyBaz<B>` to implement `Baz`
8+
--> $DIR/issue-90400-2.rs:30:14
99
|
10-
LL | struct MyBaz<B: Bar>(B);
11-
| ^^^ required by this bound in `MyBaz`
10+
LL | impl<B: Bar> Baz for MyBaz<B> {
11+
| --- ^^^ ^^^^^^^^
12+
| |
13+
| unsatisfied trait bound introduced here
14+
note: required by a bound in `<MyFoo as Foo>::FooFn::{opaque#0}`
15+
--> $DIR/issue-90400-2.rs:22:26
16+
|
17+
LL | type FooFn<B> = impl Baz;
18+
| ^^^ required by this bound in `<MyFoo as Foo>::FooFn::{opaque#0}`
1219
help: consider restricting type parameter `B`
1320
|
1421
LL | type FooFn<B: Bar> = impl Baz;

tests/ui/type-alias-impl-trait/underconstrained_generic.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ LL | impl<X: Trait> ProofForConversion<X> for () {
1111
| ----- ^^^^^^^^^^^^^^^^^^^^^ ^^
1212
| |
1313
| unsatisfied trait bound introduced here
14+
note: required by a bound in `Converter::{opaque#0}`
15+
--> $DIR/underconstrained_generic.rs:19:26
16+
|
17+
LL | type Converter<T> = impl ProofForConversion<T>;
18+
| ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Converter::{opaque#0}`
1419
help: consider restricting type parameter `T`
1520
|
1621
LL | type Converter<T: Trait> = impl ProofForConversion<T>;

tests/ui/type-alias-impl-trait/wf-in-associated-type.fail.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ LL | impl<'a, T> Trait<'a, T> for () {
55
| -- the parameter type `T` must be valid for the lifetime `'a` as defined here...
66
...
77
LL | req
8-
| ^^^ ...so that the type `&'a T` will meet its required lifetime bounds
8+
| ^^^ ...so that the type `&'a T` will meet its required lifetime bounds...
99
|
10+
note: ...that is required by this bound
11+
--> $DIR/wf-in-associated-type.rs:38:36
12+
|
13+
LL | type Opaque = impl Sized + 'a;
14+
| ^^
1015
help: consider adding an explicit lifetime bound
1116
|
1217
LL | impl<'a, T: 'a> Trait<'a, T> for () {

0 commit comments

Comments
 (0)