Skip to content
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

Normalize opaque alias type #116378

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 9 additions & 1 deletion compiler/rustc_const_eval/src/transform/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,15 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {

let kind = match parent_ty.ty.kind() {
&ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
self.tcx.type_of(def_id).instantiate(self.tcx, args).kind()
let ty = self.tcx.type_of(def_id).instantiate(self.tcx, args);
// If the type we get is opaque, we want to normalize it.
if ty.is_impl_trait() {
let inner_ty =
self.tcx.normalize_erasing_regions(self.param_env, ty).kind();
inner_ty
} else {
ty.kind()
}
}
kind => kind,
};
Expand Down
20 changes: 14 additions & 6 deletions compiler/rustc_mir_dataflow/src/impls/initialized.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use rustc_index::bit_set::{BitSet, ChunkedBitSet};
use rustc_index::Idx;
use rustc_middle::mir::{self, Body, CallReturnPlaces, Location, TerminatorEdges};
use rustc_middle::ty::{self, TyCtxt};

use crate::drop_flag_effects_for_function_entry;
use crate::drop_flag_effects_for_location;
use crate::elaborate_drops::DropFlagState;
Expand All @@ -12,6 +7,11 @@ use crate::on_lookup_result_bits;
use crate::MoveDataParamEnv;
use crate::{drop_flag_effects, on_all_children_bits, on_all_drop_children_bits};
use crate::{lattice, AnalysisDomain, GenKill, GenKillAnalysis, MaybeReachable};
use rustc_index::bit_set::{BitSet, ChunkedBitSet};
use rustc_index::Idx;
use rustc_middle::mir::{self, Body, CallReturnPlaces, Location, TerminatorEdges};
use rustc_middle::ty::ParamEnv;
use rustc_middle::ty::{self, TyCtxt};

/// `MaybeInitializedPlaces` tracks all places that might be
/// initialized upon reaching a particular point in the control flow
Expand Down Expand Up @@ -759,7 +759,15 @@ fn switch_on_enum_discriminant<'mir, 'tcx>(
mir::StatementKind::Assign(box (lhs, mir::Rvalue::Discriminant(discriminated)))
if *lhs == switch_on =>
{
match discriminated.ty(body, tcx).ty.kind() {
let mut kind = discriminated.ty(body, tcx).ty.kind();
if discriminated.ty(body, tcx).ty.is_impl_trait() {
let ty = tcx.normalize_erasing_regions(
ParamEnv::reveal_all(),
discriminated.ty(body, tcx).ty,
);
kind = ty.kind();
}
match kind {
ty::Adt(def, _) => return Some((*discriminated, *def)),

// `Rvalue::Discriminant` is also used to get the active yield point for a
Expand Down
32 changes: 32 additions & 0 deletions tests/ui/type-alias-impl-trait/normalize-alias-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// check-pass
// compile-flags: -Z mir-opt-level=3
#![feature(type_alias_impl_trait)]
#![crate_type = "lib"]
pub trait Tr {
fn get(&self) -> u32;
}

impl Tr for (u32,) {
#[inline]
fn get(&self) -> u32 { self.0 }
}

pub fn tr1() -> impl Tr {
(32,)
}

pub fn tr2() -> impl Tr {
struct Inner {
x: X,
}
type X = impl Tr;
impl Tr for Inner {
fn get(&self) -> u32 {
self.x.get()
}
}

Inner {
x: tr1(),
}
}
14 changes: 14 additions & 0 deletions tests/ui/type-alias-impl-trait/tait-normalize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// check-pass

#![feature(type_alias_impl_trait)]

fn enum_upvar() {
type T = impl Copy;
let foo: T = Some((1u32, 2u32));
let x = move || match foo {
None => (),
Some((a, b)) => (),
};
}

fn main(){}
Loading