Skip to content

Commit

Permalink
ty: projections in transparent_newtype_field
Browse files Browse the repository at this point in the history
This commit modifies `transparent_newtype_field` so that it handles
projections with generic parameters, where `normalize_erasing_regions`
would ICE.

Signed-off-by: David Wood <david@davidtw.co>
  • Loading branch information
davidtwco committed Jun 19, 2020
1 parent a39c778 commit d5b0737
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/librustc_middle/ty/mod.rs
Expand Up @@ -2387,11 +2387,15 @@ impl<'tcx> AdtDef {
assert!(self.is_struct() && self.repr.transparent());

for field in &self.non_enum_variant().fields {
let field_ty = tcx.normalize_erasing_regions(
param_env,
field.ty(tcx, InternalSubsts::identity_for_item(tcx, self.did)),
);
let field_ty = field.ty(tcx, InternalSubsts::identity_for_item(tcx, self.did));

// `normalize_erasing_regions` will fail for projections that contain generic
// parameters, so check these before normalizing.
if field_ty.has_projections() && field_ty.needs_subst() {
return Some(field);
}

let field_ty = tcx.normalize_erasing_regions(param_env, field_ty);
if !field_ty.is_zst(tcx, self.did) {
return Some(field);
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/lint/lint-ctypes-73249-1.rs
@@ -0,0 +1,21 @@
// check-pass
#![deny(improper_ctypes)]

pub trait Foo {
type Assoc: 'static;
}

impl Foo for () {
type Assoc = u32;
}

extern "C" {
pub fn lint_me(x: Bar<()>);
}

#[repr(transparent)]
pub struct Bar<T: Foo> {
value: &'static <T as Foo>::Assoc,
}

fn main() {}
21 changes: 21 additions & 0 deletions src/test/ui/lint/lint-ctypes-73249.rs
@@ -0,0 +1,21 @@
// check-pass
#![deny(improper_ctypes)]

pub trait Foo {
type Assoc;
}

impl Foo for () {
type Assoc = u32;
}

extern "C" {
pub fn lint_me(x: Bar<()>);
}

#[repr(transparent)]
pub struct Bar<T: Foo> {
value: <T as Foo>::Assoc,
}

fn main() {}

0 comments on commit d5b0737

Please sign in to comment.