Commit 4e510da
authored
Rollup merge of rust-lang#130866 - compiler-errors:dyn-instantiate-binder, r=lcnr
Allow instantiating object trait binder when upcasting
This PR fixes two bugs (that probably need an FCP).
### We use equality rather than subtyping for upcasting dyn conversions
This code should be valid:
```rust
#![feature(trait_upcasting)]
trait Foo: for<'h> Bar<'h> {}
trait Bar<'a> {}
fn foo(x: &dyn Foo) {
let y: &dyn Bar<'static> = x;
}
```
But instead:
```
error[E0308]: mismatched types
--> src/lib.rs:7:32
|
7 | let y: &dyn Bar<'static> = x;
| ^ one type is more general than the other
|
= note: expected existential trait ref `for<'h> Bar<'h>`
found existential trait ref `Bar<'_>`
```
And so should this:
```rust
#![feature(trait_upcasting)]
fn foo(x: &dyn for<'h> Fn(&'h ())) {
let y: &dyn FnOnce(&'static ()) = x;
}
```
But instead:
```
error[E0308]: mismatched types
--> src/lib.rs:4:39
|
4 | let y: &dyn FnOnce(&'static ()) = x;
| ^ one type is more general than the other
|
= note: expected existential trait ref `for<'h> FnOnce<(&'h (),)>`
found existential trait ref `FnOnce<(&(),)>`
```
Specifically, both of these fail because we use *equality* when comparing the supertrait to the *target* of the unsize goal. For the first example, since our supertrait is `for<'h> Bar<'h>` but our target is `Bar<'static>`, there's a higher-ranked type mismatch even though we *should* be able to instantiate that supertrait binder when upcasting. Similarly for the second example.
### New solver uses equality rather than subtyping for no-op (i.e. non-upcasting) dyn conversions
This code should be valid in the new solver, like it is with the old solver:
```rust
// -Znext-solver
fn foo<'a>(x: &mut for<'h> dyn Fn(&'h ())) {
let _: &mut dyn Fn(&'a ()) = x;
}
```
But instead:
```
error: lifetime may not live long enough
--> <source>:2:11
|
1 | fn foo<'a>(x: &mut dyn for<'h> Fn(&'h ())) {
| -- lifetime `'a` defined here
2 | let _: &mut dyn Fn(&'a ()) = x;
| ^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
|
= note: requirement occurs because of a mutable reference to `dyn Fn(&())`
```
Specifically, this fails because we try to coerce `&mut dyn for<'h> Fn(&'h ())` to `&mut dyn Fn(&'a ())`, which registers an `dyn for<'h> Fn(&'h ()): dyn Fn(&'a ())` goal. This fails because the new solver uses *equating* rather than *subtyping* in `Unsize` goals.
This is *mostly* not a problem... You may wonder why the same code passes on the new solver for immutable references:
```
// -Znext-solver
fn foo<'a>(x: &dyn Fn(&())) {
let _: &dyn Fn(&'a ()) = x; // works
}
```
That's because in this case, we first try to coerce via `Unsize`, but due to the leak check the goal fails. Then, later in coercion, we fall back to a simple subtyping operation, which *does* work.
Since `&T` is covariant over `T`, but `&mut T` is invariant, that's where the discrepancy between these two examples crops up.
---
r? lcnr or reassign :DFile tree
10 files changed
+228
-201
lines changed- compiler
- rustc_infer/src/infer
- rustc_next_trait_solver/src/solve
- eval_ctxt
- rustc_trait_selection/src/traits/select
- tests/ui
- coercion
- traits/trait-upcasting
10 files changed
+228
-201
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
| 95 | + | |
101 | 96 | | |
102 | 97 | | |
103 | 98 | | |
| |||
116 | 111 | | |
117 | 112 | | |
118 | 113 | | |
119 | | - | |
| 114 | + | |
120 | 115 | | |
121 | 116 | | |
122 | 117 | | |
| |||
136 | 131 | | |
137 | 132 | | |
138 | 133 | | |
139 | | - | |
| 134 | + | |
140 | 135 | | |
141 | 136 | | |
142 | 137 | | |
| |||
154 | 149 | | |
155 | 150 | | |
156 | 151 | | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
| 152 | + | |
161 | 153 | | |
162 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
163 | 172 | | |
164 | 173 | | |
165 | 174 | | |
| |||
192 | 201 | | |
193 | 202 | | |
194 | 203 | | |
195 | | - | |
| 204 | + | |
196 | 205 | | |
197 | 206 | | |
198 | 207 | | |
| |||
284 | 293 | | |
285 | 294 | | |
286 | 295 | | |
287 | | - | |
| 296 | + | |
288 | 297 | | |
289 | 298 | | |
290 | 299 | | |
| |||
306 | 315 | | |
307 | 316 | | |
308 | 317 | | |
309 | | - | |
| 318 | + | |
310 | 319 | | |
311 | 320 | | |
312 | 321 | | |
| |||
316 | 325 | | |
317 | 326 | | |
318 | 327 | | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
| 328 | + | |
325 | 329 | | |
326 | 330 | | |
327 | | - | |
| 331 | + | |
328 | 332 | | |
329 | 333 | | |
330 | | - | |
| 334 | + | |
331 | 335 | | |
332 | 336 | | |
333 | 337 | | |
| |||
338 | 342 | | |
339 | 343 | | |
340 | 344 | | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
| 345 | + | |
347 | 346 | | |
348 | 347 | | |
349 | | - | |
| 348 | + | |
350 | 349 | | |
351 | 350 | | |
352 | 351 | | |
353 | 352 | | |
354 | 353 | | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
| 354 | + | |
361 | 355 | | |
362 | 356 | | |
363 | | - | |
| 357 | + | |
364 | 358 | | |
365 | 359 | | |
366 | 360 | | |
367 | 361 | | |
368 | 362 | | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
| 363 | + | |
375 | 364 | | |
376 | 365 | | |
377 | | - | |
| 366 | + | |
378 | 367 | | |
379 | 368 | | |
380 | 369 | | |
381 | 370 | | |
382 | 371 | | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
| 372 | + | |
389 | 373 | | |
390 | 374 | | |
391 | 375 | | |
392 | 376 | | |
393 | | - | |
| 377 | + | |
394 | 378 | | |
395 | 379 | | |
396 | | - | |
| 380 | + | |
397 | 381 | | |
398 | 382 | | |
399 | | - | |
| 383 | + | |
400 | 384 | | |
401 | 385 | | |
402 | 386 | | |
| |||
419 | 403 | | |
420 | 404 | | |
421 | 405 | | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
| 406 | + | |
428 | 407 | | |
429 | 408 | | |
430 | | - | |
| 409 | + | |
431 | 410 | | |
432 | 411 | | |
433 | 412 | | |
434 | 413 | | |
435 | 414 | | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
| 415 | + | |
442 | 416 | | |
443 | 417 | | |
444 | | - | |
| 418 | + | |
445 | 419 | | |
446 | 420 | | |
447 | 421 | | |
448 | 422 | | |
449 | 423 | | |
450 | | - | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | | - | |
455 | | - | |
| 424 | + | |
456 | 425 | | |
457 | 426 | | |
458 | | - | |
| 427 | + | |
459 | 428 | | |
460 | 429 | | |
461 | 430 | | |
462 | 431 | | |
463 | 432 | | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
| 433 | + | |
470 | 434 | | |
471 | 435 | | |
472 | | - | |
| 436 | + | |
473 | 437 | | |
474 | 438 | | |
475 | 439 | | |
476 | 440 | | |
477 | 441 | | |
478 | | - | |
479 | | - | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
| 442 | + | |
484 | 443 | | |
485 | 444 | | |
486 | 445 | | |
487 | | - | |
| 446 | + | |
488 | 447 | | |
489 | 448 | | |
490 | 449 | | |
| |||
493 | 452 | | |
494 | 453 | | |
495 | 454 | | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
| 455 | + | |
502 | 456 | | |
503 | 457 | | |
504 | | - | |
| 458 | + | |
505 | 459 | | |
506 | 460 | | |
507 | 461 | | |
508 | 462 | | |
509 | 463 | | |
510 | | - | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
| 464 | + | |
516 | 465 | | |
517 | 466 | | |
518 | | - | |
| 467 | + | |
519 | 468 | | |
520 | 469 | | |
521 | 470 | | |
522 | 471 | | |
523 | 472 | | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
| 473 | + | |
530 | 474 | | |
531 | 475 | | |
532 | | - | |
| 476 | + | |
533 | 477 | | |
534 | 478 | | |
535 | 479 | | |
Lines changed: 9 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
448 | 448 | | |
449 | 449 | | |
450 | 450 | | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | | - | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
455 | 455 | | |
456 | 456 | | |
457 | 457 | | |
| |||
840 | 840 | | |
841 | 841 | | |
842 | 842 | | |
| 843 | + | |
| 844 | + | |
843 | 845 | | |
844 | | - | |
| 846 | + | |
845 | 847 | | |
846 | | - | |
| 848 | + | |
847 | 849 | | |
848 | | - | |
| 850 | + | |
849 | 851 | | |
850 | 852 | | |
851 | 853 | | |
| |||
0 commit comments