Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7a52206

Browse files
committedJun 17, 2024
Use subtyping instead of equality, since method resolution also uses subtyping
1 parent fd7eefc commit 7a52206

13 files changed

+101
-22
lines changed
 

‎compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -750,16 +750,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
750750

751751
/// Resolves an associated value path into a base type and associated constant, or method
752752
/// resolution. The newly resolved definition is written into `type_dependent_defs`.
753+
#[instrument(level = "trace", skip(self), ret)]
753754
pub fn resolve_ty_and_res_fully_qualified_call(
754755
&self,
755756
qpath: &'tcx QPath<'tcx>,
756757
hir_id: HirId,
757758
span: Span,
758759
) -> (Res, Option<LoweredTy<'tcx>>, &'tcx [hir::PathSegment<'tcx>]) {
759-
debug!(
760-
"resolve_ty_and_res_fully_qualified_call: qpath={:?} hir_id={:?} span={:?}",
761-
qpath, hir_id, span
762-
);
763760
let (ty, qself, item_segment) = match *qpath {
764761
QPath::Resolved(ref opt_qself, path) => {
765762
return (
@@ -1417,10 +1414,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14171414
// This also occurs for an enum variant on a type alias.
14181415
let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).instantiate(tcx, args));
14191416
let self_ty = self.normalize(span, self_ty);
1420-
match self.at(&self.misc(span), self.param_env).eq(
1417+
match self.at(&self.misc(span), self.param_env).sub(
14211418
DefineOpaqueTypes::Yes,
1422-
impl_ty,
14231419
self_ty,
1420+
impl_ty,
14241421
) {
14251422
Ok(ok) => self.register_infer_ok_obligations(ok),
14261423
Err(_) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct Fail<T>;
2+
//~^ ERROR: type parameter `T` is never used
3+
4+
impl Fail<i32> {
5+
const C: () = ();
6+
}
7+
8+
fn main() {
9+
Fail::<()>::C
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0392]: type parameter `T` is never used
2+
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg.rs:1:13
3+
|
4+
LL | struct Fail<T>;
5+
| ^ unused type parameter
6+
|
7+
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
8+
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0392`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait Proj {
2+
type Assoc;
3+
}
4+
impl<T> Proj for T {
5+
type Assoc = T;
6+
}
7+
8+
struct Fail<T: Proj<Assoc = U>, U>(T);
9+
10+
impl Fail<i32, i32> {
11+
const C: () = ();
12+
}
13+
14+
fn main() {
15+
Fail::<i32, u32>::C
16+
//~^ ERROR: type mismatch
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0271]: type mismatch resolving `<i32 as Proj>::Assoc == u32`
2+
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:15:5
3+
|
4+
LL | Fail::<i32, u32>::C
5+
| ^^^^^^^^^^^^^^^^ type mismatch resolving `<i32 as Proj>::Assoc == u32`
6+
|
7+
note: expected this to be `u32`
8+
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:5:18
9+
|
10+
LL | type Assoc = T;
11+
| ^
12+
note: required by a bound in `Fail`
13+
--> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:8:21
14+
|
15+
LL | struct Fail<T: Proj<Assoc = U>, U>(T);
16+
| ^^^^^^^^^ required by this bound in `Fail`
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0271`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
2+
--> $DIR/coerce-issue-49593-box-never.rs:19:5
3+
|
4+
LL | Box::<_ /* ! */>::new(x)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
6+
|
7+
= note: required for the cast from `Box<()>` to `Box<(dyn std::error::Error + 'static)>`
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0277`.

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
2-
--> $DIR/coerce-issue-49593-box-never.rs:18:53
2+
--> $DIR/coerce-issue-49593-box-never.rs:19:5
33
|
4-
LL | /* *mut $0 is coerced to Box<dyn Error> here */ Box::<_ /* ! */>::new(x)
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
4+
LL | Box::<_ /* ! */>::new(x)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
66
|
77
= note: required for the cast from `Box<()>` to `Box<(dyn std::error::Error + 'static)>`
88

99
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
10-
--> $DIR/coerce-issue-49593-box-never.rs:23:49
10+
--> $DIR/coerce-issue-49593-box-never.rs:24:49
1111
|
1212
LL | /* *mut $0 is coerced to *mut Error here */ raw_ptr_box::<_ /* ! */>(x)
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ revisions: nofallback fallback
22
//@ ignore-windows - the number of `Error` impls is platform-dependent
3-
//@[fallback] check-pass
4-
//@[nofallback] check-fail
3+
//@check-fail
54

65
#![feature(never_type)]
76
#![cfg_attr(fallback, feature(never_type_fallback))]
@@ -15,8 +14,10 @@ fn raw_ptr_box<T>(t: T) -> *mut T {
1514
}
1615

1716
fn foo(x: !) -> Box<dyn Error> {
18-
/* *mut $0 is coerced to Box<dyn Error> here */ Box::<_ /* ! */>::new(x)
19-
//[nofallback]~^ ERROR trait bound `(): std::error::Error` is not satisfied
17+
// Subtyping during method resolution will generate new inference vars and
18+
// subtype them. Thus fallback will not fall back to `!`, but `()` instead.
19+
Box::<_ /* ! */>::new(x)
20+
//~^ ERROR trait bound `(): std::error::Error` is not satisfied
2021
}
2122

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ note: required by a bound in `Mask::<T, N>::splat`
1515
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/masks.rs:LL:COL
1616
help: consider giving `y` an explicit type, where the type for type parameter `T` is specified
1717
|
18-
LL | let y: Mask<_, N> = Mask::<_, _>::splat(false);
18+
LL | let y: Mask<T, N> = Mask::<_, _>::splat(false);
1919
| ++++++++++++
2020

2121
error: aborting due to 1 previous error

‎tests/ui/inference/need_type_info/type-alias-indirect.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/type-alias-indirect.rs:14:5
33
|
44
LL | IndirectAlias::new();
5-
| ^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the type alias `IndirectAlias`
5+
| ^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the type alias `IndirectAlias`
66

77
error: aborting due to 1 previous error
88

‎tests/ui/inference/need_type_info/type-alias.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ error[E0282]: type annotations needed
22
--> $DIR/type-alias.rs:12:5
33
|
44
LL | DirectAlias::new()
5-
| ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
5+
| ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the type alias `DirectAlias`
66

77
error[E0282]: type annotations needed
88
--> $DIR/type-alias.rs:18:5
99
|
1010
LL | IndirectAlias::new();
11-
| ^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the type alias `IndirectAlias`
11+
| ^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the type alias `IndirectAlias`
1212

1313
error[E0282]: type annotations needed
1414
--> $DIR/type-alias.rs:32:5
1515
|
1616
LL | DirectButWithDefaultAlias::new();
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the type alias `DirectButWithDefaultAlias`
1818

1919
error: aborting due to 3 previous errors
2020

‎tests/ui/suggestions/mut-borrow-needed-by-trait.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fn main() {
1717
let fp = BufWriter::new(fp);
1818
//~^ ERROR the trait bound `&dyn std::io::Write: std::io::Write` is not satisfied
1919
//~| ERROR the trait bound `&dyn std::io::Write: std::io::Write` is not satisfied
20+
//~| ERROR the trait bound `&dyn std::io::Write: std::io::Write` is not satisfied
2021

2122
writeln!(fp, "hello world").unwrap(); //~ ERROR the method
2223
}

‎tests/ui/suggestions/mut-borrow-needed-by-trait.stderr

+13-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ LL | let fp = BufWriter::new(fp);
1010
note: required by a bound in `BufWriter::<W>::new`
1111
--> $SRC_DIR/std/src/io/buffered/bufwriter.rs:LL:COL
1212

13+
error[E0277]: the trait bound `&dyn std::io::Write: std::io::Write` is not satisfied
14+
--> $DIR/mut-borrow-needed-by-trait.rs:17:14
15+
|
16+
LL | let fp = BufWriter::new(fp);
17+
| ^^^^^^^^^ the trait `std::io::Write` is not implemented for `&dyn std::io::Write`
18+
|
19+
= note: `std::io::Write` is implemented for `&mut dyn std::io::Write`, but not for `&dyn std::io::Write`
20+
note: required by a bound in `BufWriter`
21+
--> $SRC_DIR/std/src/io/buffered/bufwriter.rs:LL:COL
22+
1323
error[E0277]: the trait bound `&dyn std::io::Write: std::io::Write` is not satisfied
1424
--> $DIR/mut-borrow-needed-by-trait.rs:17:14
1525
|
@@ -21,21 +31,21 @@ note: required by a bound in `BufWriter`
2131
--> $SRC_DIR/std/src/io/buffered/bufwriter.rs:LL:COL
2232

2333
error[E0599]: the method `write_fmt` exists for struct `BufWriter<&dyn Write>`, but its trait bounds were not satisfied
24-
--> $DIR/mut-borrow-needed-by-trait.rs:21:14
34+
--> $DIR/mut-borrow-needed-by-trait.rs:22:14
2535
|
2636
LL | writeln!(fp, "hello world").unwrap();
2737
| ---------^^---------------- method cannot be called on `BufWriter<&dyn Write>` due to unsatisfied trait bounds
2838
|
2939
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
30-
--> $DIR/mut-borrow-needed-by-trait.rs:21:14
40+
--> $DIR/mut-borrow-needed-by-trait.rs:22:14
3141
|
3242
LL | writeln!(fp, "hello world").unwrap();
3343
| ^^
3444
= note: the following trait bounds were not satisfied:
3545
`&dyn std::io::Write: std::io::Write`
3646
which is required by `BufWriter<&dyn std::io::Write>: std::io::Write`
3747

38-
error: aborting due to 3 previous errors
48+
error: aborting due to 4 previous errors
3949

4050
Some errors have detailed explanations: E0277, E0599.
4151
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)
Failed to load comments.