Skip to content

Commit 2e781dd

Browse files
committedJun 20, 2020
lint: normalize projections using opaque types
This commit normalizes projections which contain opaque types (opaque types are otherwise linted against, which is would have previously made the test cases added in this commit fail). Signed-off-by: David Wood <[email protected]>
1 parent 033013c commit 2e781dd

6 files changed

+127
-8
lines changed
 

‎src/librustc_lint/types.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -895,22 +895,33 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
895895
}
896896

897897
fn check_for_opaque_ty(&mut self, sp: Span, ty: Ty<'tcx>) -> bool {
898-
struct ProhibitOpaqueTypes<'tcx> {
898+
struct ProhibitOpaqueTypes<'a, 'tcx> {
899+
cx: &'a LateContext<'a, 'tcx>,
899900
ty: Option<Ty<'tcx>>,
900901
};
901902

902-
impl<'tcx> ty::fold::TypeVisitor<'tcx> for ProhibitOpaqueTypes<'tcx> {
903+
impl<'a, 'tcx> ty::fold::TypeVisitor<'tcx> for ProhibitOpaqueTypes<'a, 'tcx> {
903904
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
904-
if let ty::Opaque(..) = ty.kind {
905-
self.ty = Some(ty);
906-
true
907-
} else {
908-
ty.super_visit_with(self)
905+
match ty.kind {
906+
ty::Opaque(..) => {
907+
self.ty = Some(ty);
908+
true
909+
}
910+
// Consider opaque types within projections FFI-safe if they do not normalize
911+
// to more opaque types.
912+
ty::Projection(..) => {
913+
let ty = self.cx.tcx.normalize_erasing_regions(self.cx.param_env, ty);
914+
915+
// If `ty` is a opaque type directly then `super_visit_with` won't invoke
916+
// this function again.
917+
if ty.has_opaque_types() { self.visit_ty(ty) } else { false }
918+
}
919+
_ => ty.super_visit_with(self),
909920
}
910921
}
911922
}
912923

913-
let mut visitor = ProhibitOpaqueTypes { ty: None };
924+
let mut visitor = ProhibitOpaqueTypes { cx: self.cx, ty: None };
914925
ty.visit_with(&mut visitor);
915926
if let Some(ty) = visitor.ty {
916927
self.emit_ffi_unsafe_type_lint(ty, sp, "opaque types have no C equivalent", None);
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(type_alias_impl_trait)]
2+
#![deny(improper_ctypes)]
3+
4+
pub trait Baz { }
5+
6+
impl Baz for u32 { }
7+
8+
type Qux = impl Baz;
9+
10+
pub trait Foo {
11+
type Assoc;
12+
}
13+
14+
impl Foo for u32 {
15+
type Assoc = Qux;
16+
}
17+
18+
fn assign() -> Qux { 1 }
19+
20+
extern "C" {
21+
pub fn lint_me() -> <u32 as Foo>::Assoc; //~ ERROR: uses type `impl Baz`
22+
}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: `extern` block uses type `impl Baz`, which is not FFI-safe
2+
--> $DIR/lint-ctypes-73251-1.rs:21:25
3+
|
4+
LL | pub fn lint_me() -> <u32 as Foo>::Assoc;
5+
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-ctypes-73251-1.rs:2:9
9+
|
10+
LL | #![deny(improper_ctypes)]
11+
| ^^^^^^^^^^^^^^^
12+
= note: opaque types have no C equivalent
13+
14+
error: aborting due to previous error
15+
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![feature(type_alias_impl_trait)]
2+
#![deny(improper_ctypes)]
3+
4+
pub trait TraitA {
5+
type Assoc;
6+
}
7+
8+
impl TraitA for u32 {
9+
type Assoc = u32;
10+
}
11+
12+
pub trait TraitB {
13+
type Assoc;
14+
}
15+
16+
impl<T> TraitB for T where T: TraitA {
17+
type Assoc = <T as TraitA>::Assoc;
18+
}
19+
20+
type AliasA = impl TraitA<Assoc = u32>;
21+
22+
type AliasB = impl TraitB<Assoc = AliasA>;
23+
24+
fn use_of_a() -> AliasA { 3 }
25+
26+
fn use_of_b() -> AliasB { 3 }
27+
28+
extern "C" {
29+
pub fn lint_me() -> <AliasB as TraitB>::Assoc; //~ ERROR: uses type `impl TraitA`
30+
}
31+
32+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: `extern` block uses type `impl TraitA`, which is not FFI-safe
2+
--> $DIR/lint-ctypes-73251-2.rs:29:25
3+
|
4+
LL | pub fn lint_me() -> <AliasB as TraitB>::Assoc;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-ctypes-73251-2.rs:2:9
9+
|
10+
LL | #![deny(improper_ctypes)]
11+
| ^^^^^^^^^^^^^^^
12+
= note: opaque types have no C equivalent
13+
14+
error: aborting due to previous error
15+

‎src/test/ui/lint/lint-ctypes-73251.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// check-pass
2+
3+
#![feature(type_alias_impl_trait)]
4+
#![deny(improper_ctypes)]
5+
6+
pub trait Foo {
7+
type Assoc;
8+
}
9+
10+
impl Foo for () {
11+
type Assoc = u32;
12+
}
13+
14+
type Bar = impl Foo<Assoc = u32>;
15+
16+
fn assign() -> Bar {}
17+
18+
extern "C" {
19+
pub fn lint_me() -> <Bar as Foo>::Assoc;
20+
}
21+
22+
fn main() {}

0 commit comments

Comments
 (0)
Please sign in to comment.