Skip to content

Commit 9c0379c

Browse files
committed
Auto merge of #85511 - Mark-Simulacrum:eq-not-sup, r=nikomatsakis
Adjust self-type check to require equality When we encounter `SomeType::<X>::foo`, `self_ty` is `SomeType<X>` and the method is defined in an impl on `SomeType<A>`. Previously, we required simply that `self_ty <: impl_ty`, but this is too lax: we should require equality in order to use the method. This was found as part of unrelated work on never type stabilization, but also fixes one of the wf test cases.
2 parents af2ed1b + 4b4382d commit 9c0379c

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15221522
let ty = tcx.type_of(impl_def_id);
15231523

15241524
let impl_ty = self.instantiate_type_scheme(span, &substs, ty);
1525-
match self.at(&self.misc(span), self.param_env).sup(impl_ty, self_ty) {
1525+
match self.at(&self.misc(span), self.param_env).eq(impl_ty, self_ty) {
15261526
Ok(ok) => self.register_infer_ok_obligations(ok),
15271527
Err(_) => {
15281528
self.tcx.sess.delay_span_bug(

src/test/ui/suggestions/mut-borrow-needed-by-trait.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ 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
2120

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

src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr

+2-15
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ LL | let fp = BufWriter::new(fp);
77
= note: `std::io::Write` is implemented for `&mut dyn std::io::Write`, but not for `&dyn std::io::Write`
88
= note: required by `BufWriter::<W>::new`
99

10-
error[E0277]: the trait bound `&dyn std::io::Write: std::io::Write` is not satisfied
11-
--> $DIR/mut-borrow-needed-by-trait.rs:17:14
12-
|
13-
LL | let fp = BufWriter::new(fp);
14-
| ^^^^^^^^^ the trait `std::io::Write` is not implemented for `&dyn std::io::Write`
15-
|
16-
::: $SRC_DIR/std/src/io/buffered/bufwriter.rs:LL:COL
17-
|
18-
LL | pub struct BufWriter<W: Write> {
19-
| ----- required by this bound in `BufWriter`
20-
|
21-
= note: `std::io::Write` is implemented for `&mut dyn std::io::Write`, but not for `&dyn std::io::Write`
22-
2310
error[E0277]: the trait bound `&dyn std::io::Write: std::io::Write` is not satisfied
2411
--> $DIR/mut-borrow-needed-by-trait.rs:17:14
2512
|
@@ -34,7 +21,7 @@ LL | pub struct BufWriter<W: Write> {
3421
= note: `std::io::Write` is implemented for `&mut dyn std::io::Write`, but not for `&dyn std::io::Write`
3522

3623
error[E0599]: the method `write_fmt` exists for struct `BufWriter<&dyn std::io::Write>`, but its trait bounds were not satisfied
37-
--> $DIR/mut-borrow-needed-by-trait.rs:22:5
24+
--> $DIR/mut-borrow-needed-by-trait.rs:21:5
3825
|
3926
LL | writeln!(fp, "hello world").unwrap();
4027
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `BufWriter<&dyn std::io::Write>` due to unsatisfied trait bounds
@@ -49,7 +36,7 @@ LL | pub struct BufWriter<W: Write> {
4936
which is required by `BufWriter<&dyn std::io::Write>: std::io::Write`
5037
= note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)
5138

52-
error: aborting due to 4 previous errors
39+
error: aborting due to 3 previous errors
5340

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

src/test/ui/wf/wf-static-method.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ LL | fn inherent_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
6868
| -- -- lifetime `'b` defined here
6969
| |
7070
| lifetime `'a` defined here
71-
LL | <Evil>::inherent_evil(b) // bug? shouldn't this be an error
71+
LL | <Evil>::inherent_evil(b)
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'b` must outlive `'a`
7373
|
7474
= help: consider adding the following bound: `'b: 'a`

src/test/ui/wf/wf-static-method.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ fn indirect_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
4747
}
4848

4949
fn inherent_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
50-
<Evil>::inherent_evil(b) // bug? shouldn't this be an error
50+
<Evil>::inherent_evil(b)
51+
//~^ ERROR cannot infer an appropriate lifetime
5152
}
5253

5354

src/test/ui/wf/wf-static-method.stderr

+28-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,34 @@ note: ...so that reference does not outlive borrowed content
103103
LL | <IndirectEvil>::static_evil(b)
104104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
105105

106-
error: aborting due to 5 previous errors
106+
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements
107+
--> $DIR/wf-static-method.rs:50:5
108+
|
109+
LL | <Evil>::inherent_evil(b)
110+
| ^^^^^^^^^^^^^^^^^^^^^
111+
|
112+
note: first, the lifetime cannot outlive the lifetime `'b` as defined on the function body at 49:22...
113+
--> $DIR/wf-static-method.rs:49:22
114+
|
115+
LL | fn inherent_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
116+
| ^^
117+
note: ...so that reference does not outlive borrowed content
118+
--> $DIR/wf-static-method.rs:50:27
119+
|
120+
LL | <Evil>::inherent_evil(b)
121+
| ^
122+
note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 49:18...
123+
--> $DIR/wf-static-method.rs:49:18
124+
|
125+
LL | fn inherent_evil<'a, 'b>(b: &'b u32) -> &'a u32 {
126+
| ^^
127+
note: ...so that reference does not outlive borrowed content
128+
--> $DIR/wf-static-method.rs:50:5
129+
|
130+
LL | <Evil>::inherent_evil(b)
131+
| ^^^^^^^^^^^^^^^^^^^^^^^^
132+
133+
error: aborting due to 6 previous errors
107134

108135
Some errors have detailed explanations: E0312, E0478, E0495.
109136
For more information about an error, try `rustc --explain E0312`.

0 commit comments

Comments
 (0)