Skip to content

Commit fb8d5f1

Browse files
Actually just make can_eq process obligations (almost) everywhere
1 parent fdde66a commit fb8d5f1

File tree

24 files changed

+66
-67
lines changed

24 files changed

+66
-67
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_middle::ty::{
2121
use rustc_middle::ty::{GenericParamDefKind, TyCtxt};
2222
use rustc_middle::{bug, span_bug};
2323
use rustc_span::Span;
24+
use rustc_trait_selection::infer::InferCtxtExt;
2425
use rustc_trait_selection::regions::InferCtxtRegionExt;
2526
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
2627
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_session::parse::feature_err;
2929
use rustc_span::symbol::{sym, Ident};
3030
use rustc_span::{Span, DUMMY_SP};
3131
use rustc_target::spec::abi::Abi;
32+
use rustc_trait_selection::infer::InferCtxtExt;
3233
use rustc_trait_selection::regions::InferCtxtRegionExt;
3334
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
3435
use rustc_trait_selection::traits::misc::{
@@ -1712,15 +1713,7 @@ fn receiver_is_valid<'tcx>(
17121713
let cause =
17131714
ObligationCause::new(span, wfcx.body_def_id, traits::ObligationCauseCode::MethodReceiver);
17141715

1715-
let can_eq_self = |ty| {
1716-
wfcx.infcx.probe(|_| {
1717-
let ocx = ObligationCtxt::new(wfcx.infcx);
1718-
let Ok(()) = ocx.eq(&ObligationCause::dummy(), wfcx.param_env, self_ty, ty) else {
1719-
return false;
1720-
};
1721-
ocx.select_where_possible().is_empty()
1722-
})
1723-
};
1716+
let can_eq_self = |ty| infcx.can_eq(wfcx.param_env, self_ty, ty);
17241717

17251718
// `self: Self` is always valid.
17261719
if can_eq_self(receiver_ty) {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use rustc_span::edit_distance::find_best_match_for_name;
4949
use rustc_span::symbol::{kw, Ident, Symbol};
5050
use rustc_span::{sym, Span, DUMMY_SP};
5151
use rustc_target::spec::abi;
52+
use rustc_trait_selection::infer::InferCtxtExt;
5253
use rustc_trait_selection::traits::wf::object_region_bounds;
5354
use rustc_trait_selection::traits::{self, ObligationCtxt};
5455

compiler/rustc_hir_typeck/src/demand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
1313
use rustc_middle::ty::{self, AssocItem, Ty, TypeFoldable, TypeVisitableExt};
1414
use rustc_span::symbol::sym;
1515
use rustc_span::{Span, DUMMY_SP};
16+
use rustc_trait_selection::infer::InferCtxtExt;
1617
use rustc_trait_selection::traits::ObligationCause;
1718

1819
use super::method::probe;

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use rustc_middle::{bug, span_bug};
4040
use rustc_session::Session;
4141
use rustc_span::symbol::{kw, Ident};
4242
use rustc_span::{sym, BytePos, Span, DUMMY_SP};
43+
use rustc_trait_selection::infer::InferCtxtExt;
4344
use rustc_trait_selection::traits::{self, ObligationCauseCode, SelectionContext};
4445

4546
use std::iter;

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2582,7 +2582,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25822582
}
25832583
}
25842584
(hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, expr), _, &ty::Ref(_, checked, _))
2585-
if self.can_sub(self.param_env, checked, expected) =>
2585+
if self.can_eq(self.param_env, checked, expected) =>
25862586
{
25872587
let make_sugg = |start: Span, end: BytePos| {
25882588
// skip `(` for tuples such as `(c) = (&123)`.

compiler/rustc_hir_typeck/src/method/probe.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rustc_span::edit_distance::{
3333
};
3434
use rustc_span::symbol::sym;
3535
use rustc_span::{symbol::Ident, Span, Symbol, DUMMY_SP};
36+
use rustc_trait_selection::infer::InferCtxtExt as _;
3637
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
3738
use rustc_trait_selection::traits::query::method_autoderef::MethodAutoderefBadTy;
3839
use rustc_trait_selection::traits::query::method_autoderef::{
@@ -857,7 +858,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
857858
let args = self.fresh_args_for_item(self.span, method.def_id);
858859
let fty = self.tcx.fn_sig(method.def_id).instantiate(self.tcx, args);
859860
let fty = self.instantiate_binder_with_fresh_vars(self.span, infer::FnCall, fty);
860-
self.can_sub(self.param_env, fty.output(), expected)
861+
self.can_eq(self.param_env, fty.output(), expected)
861862
}),
862863
_ => false,
863864
}

compiler/rustc_hir_typeck/src/pat.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_span::source_map::Spanned;
1919
use rustc_span::symbol::{kw, sym, Ident};
2020
use rustc_span::{BytePos, Span, DUMMY_SP};
2121
use rustc_target::abi::FieldIdx;
22+
use rustc_trait_selection::infer::InferCtxtExt;
2223
use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode};
2324
use ty::VariantDef;
2425

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ fn foo(&self) -> Self::T { String::new() }
820820
tcx.defaultness(item.id.owner_id)
821821
{
822822
let assoc_ty = tcx.type_of(item.id.owner_id).instantiate_identity();
823-
if self.infcx.can_eq(param_env, assoc_ty, found) {
823+
if self.infcx.can_eq_shallow(param_env, assoc_ty, found) {
824824
diag.span_label(
825825
item.span,
826826
"associated type defaults can't be assumed inside the \
@@ -843,7 +843,7 @@ fn foo(&self) -> Self::T { String::new() }
843843
let assoc_ty = tcx.type_of(item.id.owner_id).instantiate_identity();
844844
if let hir::Defaultness::Default { has_value: true } =
845845
tcx.defaultness(item.id.owner_id)
846-
&& self.infcx.can_eq(param_env, assoc_ty, found)
846+
&& self.infcx.can_eq_shallow(param_env, assoc_ty, found)
847847
{
848848
diag.span_label(
849849
item.span,

compiler/rustc_infer/src/infer/mod.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -769,21 +769,7 @@ impl<'tcx> InferCtxt<'tcx> {
769769

770770
// FIXME(-Znext-solver): Get rid of this method, it's never correct. Either that,
771771
// or we need to process the obligations.
772-
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, expected: T, actual: T) -> bool
773-
where
774-
T: at::ToTrace<'tcx>,
775-
{
776-
let origin = &ObligationCause::dummy();
777-
self.probe(|_| {
778-
// We're only answering whether there could be a subtyping relation, and with
779-
// opaque types, "there could be one", via registering a hidden type.
780-
self.at(origin, param_env).sub(DefineOpaqueTypes::Yes, expected, actual).is_ok()
781-
})
782-
}
783-
784-
// FIXME(-Znext-solver): Get rid of this method, it's never correct. Either that,
785-
// or we need to process the obligations.
786-
pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
772+
pub fn can_eq_shallow<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
787773
where
788774
T: at::ToTrace<'tcx>,
789775
{

compiler/rustc_trait_selection/src/infer.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::infer::at::ToTrace;
12
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
23
use crate::traits::{self, Obligation, ObligationCause, ObligationCtxt, SelectionContext};
34

@@ -17,6 +18,16 @@ pub use rustc_infer::infer::*;
1718

1819
#[extension(pub trait InferCtxtExt<'tcx>)]
1920
impl<'tcx> InferCtxt<'tcx> {
21+
fn can_eq<T: ToTrace<'tcx>>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool {
22+
self.probe(|_| {
23+
let ocx = ObligationCtxt::new(self);
24+
let Ok(()) = ocx.eq(&ObligationCause::dummy(), param_env, a, b) else {
25+
return false;
26+
};
27+
ocx.select_where_possible().is_empty()
28+
})
29+
}
30+
2031
fn type_is_copy_modulo_regions(&self, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> bool {
2132
let ty = self.resolve_vars_if_possible(ty);
2233

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use super::{ObligationCauseCode, PredicateObligation};
2+
use crate::errors::{
3+
EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented,
4+
};
25
use crate::infer::error_reporting::TypeErrCtxt;
6+
use crate::infer::InferCtxtExt;
7+
use crate::traits::error_reporting::type_err_ctxt_ext::InferCtxtPrivExt;
38
use rustc_ast::AttrArgs;
49
use rustc_ast::AttrArgsEq;
510
use rustc_ast::AttrKind;
@@ -21,12 +26,6 @@ use rustc_span::Span;
2126
use std::iter;
2227
use std::path::PathBuf;
2328

24-
use crate::errors::{
25-
EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented,
26-
};
27-
28-
use crate::traits::error_reporting::type_err_ctxt_ext::InferCtxtPrivExt;
29-
3029
/// The symbols which are always allowed in a format string
3130
static ALLOWED_FORMAT_SYMBOLS: &[Symbol] = &[
3231
kw::SelfUpper,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
10731073
// mismatched, then we have a totally different error to report.
10741074
if self.enter_forall(found_args, |found_args| {
10751075
self.enter_forall(expected_args, |expected_args| {
1076-
!self.can_sub(obligation.param_env, expected_args, found_args)
1076+
!self.can_eq(obligation.param_env, expected_args, found_args)
10771077
})
10781078
}) {
10791079
return None;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use super::{
1818
TraitQueryMode,
1919
};
2020

21-
use crate::infer::{InferCtxt, InferOk, TypeFreshener};
21+
use crate::infer::{InferCtxt, InferCtxtExt, InferOk, TypeFreshener};
2222
use crate::solve::InferCtxtSelectExt as _;
2323
use crate::traits::error_reporting::TypeErrCtxtExt;
2424
use crate::traits::normalize::normalize_with_depth;

tests/ui/generic-associated-types/assume-gat-normalization-for-nested-goals.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: the trait bound `i32: Baz<Self>` is not satisfied
1+
error[E0277]: the trait bound `<Self as Foo>::Bar<()>: Eq<i32>` is not satisfied
22
--> $DIR/assume-gat-normalization-for-nested-goals.rs:9:30
33
|
44
LL | type Bar<T>: Baz<Self> = i32;

tests/ui/impl-trait/nested_impl_trait.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn fine(x: impl Into<u32>) -> impl Into<u32> { x }
55

66
fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
77
//~^ ERROR nested `impl Trait` is not allowed
8-
//~| ERROR the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
8+
//~| ERROR the trait bound `impl Debug: From<impl Into<u32>>` is not satisfied
99

1010
fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
1111
//~^ ERROR nested `impl Trait` is not allowed
@@ -18,7 +18,7 @@ struct X;
1818
impl X {
1919
fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
2020
//~^ ERROR nested `impl Trait` is not allowed
21-
//~| ERROR the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
21+
//~| ERROR the trait bound `impl Debug: From<impl Into<u32>>` is not satisfied
2222
}
2323

2424
fn allowed_in_assoc_type() -> impl Iterator<Item=impl Fn()> {

tests/ui/impl-trait/nested_impl_trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
4242
|
4343
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
4444

45-
error[E0277]: the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
45+
error[E0277]: the trait bound `impl Debug: From<impl Into<u32>>` is not satisfied
4646
--> $DIR/nested_impl_trait.rs:6:46
4747
|
4848
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
@@ -51,7 +51,7 @@ LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
5151
= help: the trait `Into<U>` is implemented for `T`
5252
= note: required for `impl Into<u32>` to implement `Into<impl Debug>`
5353

54-
error[E0277]: the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
54+
error[E0277]: the trait bound `impl Debug: From<impl Into<u32>>` is not satisfied
5555
--> $DIR/nested_impl_trait.rs:19:34
5656
|
5757
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }

tests/ui/traits/next-solver/auto-with-drop_tracking_mir.fail.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ note: required by a bound in `is_send`
1717
|
1818
LL | fn is_send(_: impl Send) {}
1919
| ^^^^ required by this bound in `is_send`
20-
help: consider dereferencing here
21-
|
22-
LL | is_send(*foo());
23-
| +
2420

2521
error: aborting due to 1 previous error
2622

tests/ui/type-alias-impl-trait/method_resolution3.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | fn bar(self: Bar<u32>) {
88
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
99

1010
error[E0307]: invalid `self` parameter type: `&Bar<u32>`
11-
--> $DIR/method_resolution3.rs:21:18
11+
--> $DIR/method_resolution3.rs:20:18
1212
|
1313
LL | fn baz(self: &Bar<u32>) {
1414
| ^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
error[E0271]: type mismatch resolving `Foo == u32`
1+
error[E0307]: invalid `self` parameter type: `Bar<u32>`
22
--> $DIR/method_resolution3.rs:16:18
33
|
44
LL | fn bar(self: Bar<u32>) {
5-
| ^^^^^^^^ types differ
5+
| ^^^^^^^^
6+
|
7+
= note: type of `self` must be `Self` or a type that dereferences to it
8+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
69

7-
error[E0271]: type mismatch resolving `Foo == u32`
8-
--> $DIR/method_resolution3.rs:21:18
10+
error[E0307]: invalid `self` parameter type: `&Bar<u32>`
11+
--> $DIR/method_resolution3.rs:20:18
912
|
1013
LL | fn baz(self: &Bar<u32>) {
11-
| ^^^^^^^^^ types differ
14+
| ^^^^^^^^^
15+
|
16+
= note: type of `self` must be `Self` or a type that dereferences to it
17+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
1218

1319
error: aborting due to 2 previous errors
1420

15-
For more information about this error, try `rustc --explain E0271`.
21+
For more information about this error, try `rustc --explain E0307`.

tests/ui/type-alias-impl-trait/method_resolution3.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ struct Bar<T>(T);
1414

1515
impl Bar<Foo> {
1616
fn bar(self: Bar<u32>) {
17-
//[current]~^ ERROR: invalid `self` parameter
18-
//[next]~^^ ERROR: type mismatch resolving `Foo == u32`
17+
//~^ ERROR: invalid `self` parameter
1918
self.foo()
2019
}
2120
fn baz(self: &Bar<u32>) {
22-
//[current]~^ ERROR: invalid `self` parameter
23-
//[next]~^^ ERROR: type mismatch resolving `Foo == u32`
21+
//~^ ERROR: invalid `self` parameter
2422
self.foo()
2523
}
2624
}

tests/ui/type-alias-impl-trait/method_resolution4.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | fn foo(self: Bar<Foo>) {
88
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
99

1010
error[E0307]: invalid `self` parameter type: `&Bar<Foo>`
11-
--> $DIR/method_resolution4.rs:32:20
11+
--> $DIR/method_resolution4.rs:31:20
1212
|
1313
LL | fn foomp(self: &Bar<Foo>) {
1414
| ^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
error[E0271]: type mismatch resolving `u32 == Foo`
1+
error[E0307]: invalid `self` parameter type: `Bar<Foo>`
22
--> $DIR/method_resolution4.rs:27:18
33
|
44
LL | fn foo(self: Bar<Foo>) {
5-
| ^^^^^^^^ types differ
5+
| ^^^^^^^^
6+
|
7+
= note: type of `self` must be `Self` or a type that dereferences to it
8+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
69

7-
error[E0271]: type mismatch resolving `u32 == Foo`
8-
--> $DIR/method_resolution4.rs:32:20
10+
error[E0307]: invalid `self` parameter type: `&Bar<Foo>`
11+
--> $DIR/method_resolution4.rs:31:20
912
|
1013
LL | fn foomp(self: &Bar<Foo>) {
11-
| ^^^^^^^^^ types differ
14+
| ^^^^^^^^^
15+
|
16+
= note: type of `self` must be `Self` or a type that dereferences to it
17+
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
1218

1319
error: aborting due to 2 previous errors
1420

15-
For more information about this error, try `rustc --explain E0271`.
21+
For more information about this error, try `rustc --explain E0307`.

tests/ui/type-alias-impl-trait/method_resolution4.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ impl Bar<Foo> {
2525

2626
impl Bar<u32> {
2727
fn foo(self: Bar<Foo>) {
28-
//[current]~^ ERROR: invalid `self` parameter
29-
//[next]~^^ ERROR: type mismatch resolving `u32 == Foo`
28+
//~^ ERROR: invalid `self` parameter
3029
self.bar()
3130
}
3231
fn foomp(self: &Bar<Foo>) {
33-
//[current]~^ ERROR: invalid `self` parameter
34-
//[next]~^^ ERROR: type mismatch resolving `u32 == Foo`
32+
//~^ ERROR: invalid `self` parameter
3533
self.bar()
3634
}
3735
}

0 commit comments

Comments
 (0)