Skip to content

Commit 1f46f77

Browse files
committed
Remove some special code handling TAIT being passed through if and match
This is not necessary for RPIT anymore, since we reverted that to using inference vars.
1 parent 02536fe commit 1f46f77

File tree

6 files changed

+68
-27
lines changed

6 files changed

+68
-27
lines changed

compiler/rustc_typeck/src/check/expectation.rs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
22
use rustc_middle::ty::{self, Ty};
3-
use rustc_span::DUMMY_SP;
43
use rustc_span::{self, Span};
54

65
use super::Expectation::*;
@@ -44,7 +43,7 @@ impl<'a, 'tcx> Expectation<'tcx> {
4443
// when checking the 'then' block which are incompatible with the
4544
// 'else' branch.
4645
pub(super) fn adjust_for_branches(&self, fcx: &FnCtxt<'a, 'tcx>) -> Expectation<'tcx> {
47-
match self.strip_opaque(fcx) {
46+
match *self {
4847
ExpectHasType(ety) => {
4948
let ety = fcx.shallow_resolve(ety);
5049
if !ety.is_ty_var() { ExpectHasType(ety) } else { NoExpectation }
@@ -105,35 +104,14 @@ impl<'a, 'tcx> Expectation<'tcx> {
105104
/// for the program to type-check). `only_has_type` will return
106105
/// such a constraint, if it exists.
107106
pub(super) fn only_has_type(self, fcx: &FnCtxt<'a, 'tcx>) -> Option<Ty<'tcx>> {
108-
match self.strip_opaque(fcx) {
109-
ExpectHasType(ty) => Some(ty),
107+
match self {
108+
ExpectHasType(ty) => Some(fcx.resolve_vars_if_possible(ty)),
110109
NoExpectation | ExpectCastableToType(_) | ExpectRvalueLikeUnsized(_) | IsLast(_) => {
111110
None
112111
}
113112
}
114113
}
115114

116-
/// We must not treat opaque types as expected types in their defining scope, as that
117-
/// will break `fn foo() -> impl Trait { if cond { a } else { b } }` if `a` and `b` are
118-
/// only "equal" if they coerce to a common target, like two different function items
119-
/// coercing to a function pointer if they have the same signature.
120-
fn strip_opaque(self, fcx: &FnCtxt<'a, 'tcx>) -> Self {
121-
match self {
122-
ExpectHasType(ty) => {
123-
let ty = fcx.resolve_vars_if_possible(ty);
124-
match *ty.kind() {
125-
ty::Opaque(def_id, _)
126-
if fcx.infcx.opaque_type_origin(def_id, DUMMY_SP).is_some() =>
127-
{
128-
NoExpectation
129-
}
130-
_ => self,
131-
}
132-
}
133-
_ => self,
134-
}
135-
}
136-
137115
/// Like `only_has_type`, but instead of returning `None` if no
138116
/// hard constraint exists, creates a fresh type variable.
139117
pub(super) fn coercion_target_type(self, fcx: &FnCtxt<'a, 'tcx>, span: Span) -> Ty<'tcx> {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
type Foo = impl std::fmt::Debug;
4+
5+
fn foo(b: bool) -> Foo {
6+
if b {
7+
vec![42_i32]
8+
} else {
9+
std::iter::empty().collect()
10+
//~^ ERROR `Foo` cannot be built from an iterator over elements of type `_`
11+
}
12+
}
13+
14+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0277]: a value of type `Foo` cannot be built from an iterator over elements of type `_`
2+
--> $DIR/branches.rs:9:28
3+
|
4+
LL | std::iter::empty().collect()
5+
| ^^^^^^^ value of type `Foo` cannot be built from `std::iter::Iterator<Item=_>`
6+
|
7+
= help: the trait `FromIterator<_>` is not implemented for `Foo`
8+
note: required by a bound in `collect`
9+
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
10+
|
11+
LL | fn collect<B: FromIterator<Self::Item>>(self) -> B
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `collect`
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
// run-pass
4+
5+
type Foo = impl std::iter::FromIterator<i32> + PartialEq<Vec<i32>> + std::fmt::Debug;
6+
7+
fn foo(b: bool) -> Foo {
8+
if b {
9+
vec![42_i32]
10+
} else {
11+
std::iter::empty().collect()
12+
}
13+
}
14+
15+
fn bar(b: bool) -> impl PartialEq<Vec<i32>> + std::fmt::Debug {
16+
if b {
17+
vec![42_i32]
18+
} else {
19+
std::iter::empty().collect()
20+
}
21+
}
22+
23+
fn main() {
24+
assert_eq!(foo(true), vec![42]);
25+
assert_eq!(foo(false), vec![]);
26+
assert_eq!(bar(true), vec![42]);
27+
assert_eq!(bar(false), vec![]);
28+
}

src/test/ui/type-alias-impl-trait/issue-63279.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ error[E0308]: mismatched types
2121
|
2222
LL | type Closure = impl FnOnce();
2323
| ------------- the expected opaque type
24-
...
24+
LL |
25+
LL | fn c() -> Closure {
26+
| ------- expected `Closure` because of return type
2527
LL | || -> Closure { || () }
2628
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found closure
2729
|

src/test/ui/type-alias-impl-trait/issue-74280.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ error[E0308]: mismatched types
33
|
44
LL | type Test = impl Copy;
55
| --------- the expected opaque type
6-
...
6+
LL |
7+
LL | fn test() -> Test {
8+
| ---- expected `Test` because of return type
9+
LL | let y = || -> Test { () };
710
LL | 7
811
| ^ expected `()`, found integer
912
|

0 commit comments

Comments
 (0)