Skip to content

Commit c686f43

Browse files
Rollup merge of rust-lang#105846 - compiler-errors:issue-105838, r=jackh726
Account for return-position `impl Trait` in trait in `opt_suggest_box_span` RPITITs are the only types where their opaque bounds might normalize to some other self type than the opaque type itself. To avoid needing to do normalization, let's just match on either alias kind. Ideally, we'd just get rid of `opt_suggest_box_span`. It's kind of a wart on type-checking `if`/`match`. I've recently refactored this expression for being confusing/wrong, but moving it into the error path is pretty hard. Fixes rust-lang#105838
2 parents d26242d + 7df33a0 commit c686f43

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

compiler/rustc_hir_typeck/src/_match.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
526526
None
527527
}
528528
})?;
529-
let opaque_ty = self.tcx.mk_opaque(rpit_def_id, substs);
530529

531530
if !self.can_coerce(first_ty, expected) || !self.can_coerce(second_ty, expected) {
532531
return None;
@@ -540,13 +539,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
540539
{
541540
let pred = pred.kind().rebind(match pred.kind().skip_binder() {
542541
ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) => {
543-
assert_eq!(trait_pred.trait_ref.self_ty(), opaque_ty);
542+
// FIXME(rpitit): This will need to be fixed when we move to associated types
543+
assert!(matches!(
544+
*trait_pred.trait_ref.self_ty().kind(),
545+
ty::Alias(_, ty::AliasTy { def_id, substs, .. })
546+
if def_id == rpit_def_id && substs == substs
547+
));
544548
ty::PredicateKind::Clause(ty::Clause::Trait(
545549
trait_pred.with_self_ty(self.tcx, ty),
546550
))
547551
}
548552
ty::PredicateKind::Clause(ty::Clause::Projection(mut proj_pred)) => {
549-
assert_eq!(proj_pred.projection_ty.self_ty(), opaque_ty);
553+
assert!(matches!(
554+
*proj_pred.projection_ty.self_ty().kind(),
555+
ty::Alias(_, ty::AliasTy { def_id, substs, .. })
556+
if def_id == rpit_def_id && substs == substs
557+
));
550558
proj_pred = proj_pred.with_self_ty(self.tcx, ty);
551559
ty::PredicateKind::Clause(ty::Clause::Projection(proj_pred))
552560
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// check-pass
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
//~^ WARN the feature `return_position_impl_trait_in_trait` is incomplete
5+
6+
struct TestA {}
7+
struct TestB {}
8+
9+
impl TestTrait for TestA {
10+
type Output = ();
11+
}
12+
impl TestTrait for TestB {
13+
type Output = ();
14+
}
15+
16+
trait TestTrait {
17+
type Output;
18+
}
19+
20+
impl<A, B> TestTrait for GreeterOutput<A, B>
21+
where
22+
A: TestTrait<Output = ()>,
23+
B: TestTrait<Output = ()>,
24+
{
25+
type Output = ();
26+
}
27+
28+
enum GreeterOutput<A, B>
29+
where
30+
A: TestTrait<Output = ()>,
31+
B: TestTrait<Output = ()>,
32+
{
33+
SayHello(A),
34+
SayGoodbye(B),
35+
}
36+
37+
trait Greeter {
38+
fn test_func(&self, func: &str) -> impl TestTrait<Output = ()> {
39+
match func {
40+
"SayHello" => GreeterOutput::SayHello(TestA {}),
41+
"SayGoodbye" => GreeterOutput::SayGoodbye(TestB {}),
42+
_ => GreeterOutput::SayHello(TestA {}),
43+
}
44+
}
45+
}
46+
47+
fn main() {
48+
println!("Hello, world!");
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `return_position_impl_trait_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/box-coerce-span-in-default.rs:3:12
3+
|
4+
LL | #![feature(return_position_impl_trait_in_trait)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+

0 commit comments

Comments
 (0)