Skip to content

Only prevent field projections into opaque types, not types containing opaque types #116156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
// Opaque types can't have field projections, but we can instead convert
// the current place in-place (heh) to the hidden type, and then apply all
// follow up projections on that.
if node_ty != place_ty && place_ty.has_opaque_types() {
if node_ty != place_ty && matches!(place_ty.kind(), ty::Alias(ty::Opaque, ..)) {
projections.push(Projection { kind: ProjectionKind::OpaqueCast, ty: node_ty });
}
projections.push(Projection { kind, ty });
Expand Down
27 changes: 27 additions & 0 deletions tests/ui/impl-trait/opaque-cast-field-access-in-future.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// edition: 2021

use std::future::Future;

async fn bop() {
fold(run(), |mut foo| async move {
&mut foo.bar;
})
}

fn fold<Fut, F, U>(_: Foo<U>, f: F)
where
F: FnMut(Foo<U>) -> Fut,
{
loop {}
}

struct Foo<F> {
bar: Vec<F>,
}

fn run() -> Foo<impl Future<Output = ()>> {
//~^ ERROR type annotations needed
loop {}
}

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/impl-trait/opaque-cast-field-access-in-future.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0282]: type annotations needed
--> $DIR/opaque-cast-field-access-in-future.rs:22:17
|
LL | fn run() -> Foo<impl Future<Output = ()>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.