Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f4e027c

Browse files
Relate receiver invariantly in method probe for Mode::Path
1 parent e08b80c commit f4e027c

15 files changed

+109
-54
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,10 +1437,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14371437
// This also occurs for an enum variant on a type alias.
14381438
let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).instantiate(tcx, args));
14391439
let self_ty = self.normalize(span, self_ty);
1440-
match self.at(&self.misc(span), self.param_env).sub(
1440+
match self.at(&self.misc(span), self.param_env).eq(
14411441
DefineOpaqueTypes::Yes,
1442-
self_ty,
14431442
impl_ty,
1443+
self_ty,
14441444
) {
14451445
Ok(ok) => self.register_infer_ok_obligations(ok),
14461446
Err(_) => {

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
620620
self.unsatisfied_predicates.borrow_mut().clear();
621621
}
622622

623+
/// When we're looking up a method by path (UFCS), we relate the receiver types invariantly. When we are looking up a method by
624+
fn variance(&self) -> ty::Variance {
625+
match self.mode {
626+
Mode::MethodCall => ty::Contravariant,
627+
Mode::Path => ty::Invariant,
628+
}
629+
}
630+
623631
///////////////////////////////////////////////////////////////////////////
624632
// CANDIDATE ASSEMBLY
625633

@@ -1442,7 +1450,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14421450
(xform_self_ty, xform_ret_ty) =
14431451
self.xform_self_ty(probe.item, impl_ty, impl_args);
14441452
xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1445-
match ocx.sup(cause, self.param_env, xform_self_ty, self_ty) {
1453+
match ocx.relate(cause, self.param_env, self.variance(), xform_self_ty, self_ty)
1454+
{
14461455
Ok(()) => {}
14471456
Err(err) => {
14481457
debug!("--> cannot relate self-types {:?}", err);
@@ -1514,7 +1523,13 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
15141523
{
15151524
return ProbeResult::NoMatch;
15161525
}
1517-
_ => match ocx.sup(cause, self.param_env, xform_self_ty, self_ty) {
1526+
_ => match ocx.relate(
1527+
cause,
1528+
self.param_env,
1529+
self.variance(),
1530+
xform_self_ty,
1531+
self_ty,
1532+
) {
15181533
Ok(()) => {}
15191534
Err(err) => {
15201535
debug!("--> cannot relate self-types {:?}", err);
@@ -1560,7 +1575,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
15601575
(xform_self_ty, xform_ret_ty) =
15611576
self.xform_self_ty(probe.item, trait_ref.self_ty(), trait_ref.args);
15621577
xform_self_ty = ocx.normalize(cause, self.param_env, xform_self_ty);
1563-
match ocx.sup(cause, self.param_env, xform_self_ty, self_ty) {
1578+
match ocx.relate(cause, self.param_env, self.variance(), xform_self_ty, self_ty)
1579+
{
15641580
Ok(()) => {}
15651581
Err(err) => {
15661582
debug!("--> cannot relate self-types {:?}", err);
@@ -1603,7 +1619,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16031619
}
16041620

16051621
debug!("comparing return_ty {:?} with xform ret ty {:?}", return_ty, xform_ret_ty);
1606-
match ocx.sup(cause, self.param_env, return_ty, xform_ret_ty) {
1622+
match ocx.relate(cause, self.param_env, self.variance(), return_ty, xform_ret_ty) {
16071623
Ok(()) => {}
16081624
Err(_) => {
16091625
result = ProbeResult::BadReturnType;

tests/ui/associated-consts/wrong-projection-self-ty-invalid-bivariant-arg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ impl Fail<i32> {
77

88
fn main() {
99
Fail::<()>::C
10+
//~^ ERROR no associated item named `C` found for struct `Fail<()>` in the current scope
1011
}

tests/ui/associated-consts/wrong-projection-self-ty-invalid-bivariant-arg.stderr

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ LL | struct Fail<T>;
77
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
88
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
99

10-
error: aborting due to 1 previous error
10+
error[E0599]: no associated item named `C` found for struct `Fail<()>` in the current scope
11+
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg.rs:9:17
12+
|
13+
LL | struct Fail<T>;
14+
| -------------- associated item `C` not found for this struct
15+
...
16+
LL | Fail::<()>::C
17+
| ^ associated item not found in `Fail<()>`
18+
|
19+
= note: the associated item was found for
20+
- `Fail<i32>`
21+
22+
error: aborting due to 2 previous errors
1123

12-
For more information about this error, try `rustc --explain E0392`.
24+
Some errors have detailed explanations: E0392, E0599.
25+
For more information about an error, try `rustc --explain E0392`.

tests/ui/associated-consts/wrong-projection-self-ty-invalid-bivariant-arg2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ impl Fail<i32, i32> {
1414
fn main() {
1515
Fail::<i32, u32>::C
1616
//~^ ERROR: type mismatch
17+
//~| ERROR no associated item named `C` found for struct `Fail<i32, u32>` in the current scope
1718
}

tests/ui/associated-consts/wrong-projection-self-ty-invalid-bivariant-arg2.stderr

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
error[E0599]: no associated item named `C` found for struct `Fail<i32, u32>` in the current scope
2+
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:15:23
3+
|
4+
LL | struct Fail<T: Proj<Assoc = U>, U>(T);
5+
| ---------------------------------- associated item `C` not found for this struct
6+
...
7+
LL | Fail::<i32, u32>::C
8+
| ^ associated item not found in `Fail<i32, u32>`
9+
|
10+
= note: the associated item was found for
11+
- `Fail<i32, i32>`
12+
113
error[E0271]: type mismatch resolving `<i32 as Proj>::Assoc == u32`
214
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:15:5
315
|
@@ -15,6 +27,7 @@ note: required by a bound in `Fail`
1527
LL | struct Fail<T: Proj<Assoc = U>, U>(T);
1628
| ^^^^^^^^^ required by this bound in `Fail`
1729

18-
error: aborting due to 1 previous error
30+
error: aborting due to 2 previous errors
1931

20-
For more information about this error, try `rustc --explain E0271`.
32+
Some errors have detailed explanations: E0271, E0599.
33+
For more information about an error, try `rustc --explain E0271`.

tests/ui/coercion/coerce-issue-49593-box-never.fallback.stderr

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/ui/coercion/coerce-issue-49593-box-never.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ revisions: nofallback fallback
2-
//@check-fail
2+
//@[fallback] check-pass
33

44
#![feature(never_type)]
55
#![cfg_attr(fallback, feature(never_type_fallback))]
@@ -16,7 +16,7 @@ fn foo(x: !) -> Box<dyn Error> {
1616
// Subtyping during method resolution will generate new inference vars and
1717
// subtype them. Thus fallback will not fall back to `!`, but `()` instead.
1818
Box::<_ /* ! */>::new(x)
19-
//~^ ERROR trait bound `(): std::error::Error` is not satisfied
19+
//[nofallback]~^ ERROR trait bound `(): std::error::Error` is not satisfied
2020
}
2121

2222
fn foo_raw_ptr(x: !) -> *mut dyn Error {

tests/ui/const-generics/generic_arg_infer/issue-91614.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ note: required by a const generic parameter in `Mask::<T, N>::splat`
88
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/masks.rs:LL:COL
99
help: consider giving `y` an explicit type, where the value of const parameter `N` is specified
1010
|
11-
LL | let y: Mask<T, N> = Mask::<_, _>::splat(false);
11+
LL | let y: Mask<_, N> = Mask::<_, _>::splat(false);
1212
| ++++++++++++
1313

1414
error[E0284]: type annotations needed for `Mask<_, _>`
@@ -21,7 +21,7 @@ note: required by a const generic parameter in `Mask`
2121
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/masks.rs:LL:COL
2222
help: consider giving `y` an explicit type, where the value of const parameter `N` is specified
2323
|
24-
LL | let y: Mask<T, N> = Mask::<_, _>::splat(false);
24+
LL | let y: Mask<_, N> = Mask::<_, _>::splat(false);
2525
| ++++++++++++
2626

2727
error: aborting due to 2 previous errors

tests/ui/impl-trait/issues/issue-62742.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::marker::PhantomData;
22

33
fn a() {
44
WrongImpl::foo(0i32);
5-
//~^ ERROR overflow assigning `_` to `[_]`
5+
//~^ ERROR the function or associated item `foo` exists for struct `SafeImpl<_, RawImpl<_>>`, but its trait bounds were not satisfied
6+
//~| ERROR the trait bound `RawImpl<_>: Raw<_>` is not satisfied
67
}
78

89
fn b() {

0 commit comments

Comments
 (0)