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

improper_ctypes incorrectly warns on projections featuring opaque types #73251

Closed
lcnr opened this issue Jun 11, 2020 · 6 comments · Fixed by #73287
Closed

improper_ctypes incorrectly warns on projections featuring opaque types #73251

lcnr opened this issue Jun 11, 2020 · 6 comments · Fixed by #73287
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.

Comments

@lcnr
Copy link
Contributor

lcnr commented Jun 11, 2020

cc @davidtwco

#![feature(type_alias_impl_trait)]

pub trait Foo {
    type Assoc;
}

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

type Bar = impl Foo;

fn assign() -> Bar {}

extern "C" {
    pub fn lint_me() -> <Bar as Foo>::Assoc;
}

Gives the following incorrect warning:

warning: `extern` block uses type `Bar`, which is not FFI-safe
  --> src/lib.rs:16:25
   |
16 |     pub fn lint_me() -> <Bar as Foo>::Assoc;
   |                         ^^^^^^^^^^^^^^^^^^^ not FFI-safe
   |
   = note: `#[warn(improper_ctypes)]` on by default
   = note: opaque types have no C equivalent

warning: 1 warning emitted
@lcnr lcnr added the C-bug Category: This is a bug. label Jun 11, 2020
@davidtwco davidtwco self-assigned this Jun 11, 2020
@lcnr lcnr added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 11, 2020
@davidtwco
Copy link
Member

I'm not sure if this is incorrect - I think this is what I'd expect. In the declaration of lint_me, we don't know what concrete type Bar is - if it were (), then we could normalize to u32 - but we don't know? Maybe I'm missing something.

@lcnr
Copy link
Contributor Author

lcnr commented Jun 11, 2020

Hmm, that might be true. I definitely think that the following should not lint here (with Assoc bound to u32)

#![feature(type_alias_impl_trait)]

pub trait Foo {
    type Assoc;
}

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

type Bar = impl Foo<Assoc = u32>;

fn assign() -> Bar {}

extern "C" {
    pub fn lint_me() -> <Bar as Foo>::Assoc;
}

@davidtwco
Copy link
Member

I agree that seems like it should be fine. So, this happens because of this call:

// We have to check for opaque types before `normalize_erasing_regions`,
// which will replace opaque types with their underlying concrete type.
if self.check_for_opaque_ty(sp, ty) {
// We've already emitted an error due to an opaque type.
return;
}

It makes sense that we want to lint on opaque types on extern declarations, it's unclear whether we would want to allow projections which contain opaque types - this would solve the case you've shown here, but could be used to "sneak" opaque types by the lint (in this playground, the projection normalizes to an opaque type; it's hard to detect because the normalization goes further and resolves it to u32 so we can't tell that it was an opaque type) - I have a build with this locally.

Any thoughts on whether we should change the behavior here?

cc @hanna-kruppe

@lcnr
Copy link
Contributor Author

lcnr commented Jun 12, 2020

Hm, I think it might be worth it to normalize using Reveal::UserFacing here, which should not normalize the first case and your example while fixing the second one if I am not mistaken.

@hanna-kruppe
Copy link
Contributor

Long ago I filed #60855 where we also concluded Reveal::UserFacing is the right behavior for normalization in this lint. The current behavior comes from #64359 cc @varkor

@davidtwco
Copy link
Member

davidtwco commented Jun 12, 2020

I'll put up a PR with my experiments from last night later in that case.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 14, 2020
…in-projections, r=estebank

lint: normalize projections using opaque types

Fixes rust-lang#73251.

This PR normalizes projections which use opaque types (opaque types are otherwise linted against, which is would have previously made the test cases added in this PR fail).
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 15, 2020
…in-projections, r=estebank

lint: normalize projections using opaque types

Fixes rust-lang#73251.

This PR normalizes projections which use opaque types (opaque types are otherwise linted against, which is would have previously made the test cases added in this PR fail).
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 15, 2020
…in-projections, r=estebank

lint: normalize projections using opaque types

Fixes rust-lang#73251.

This PR normalizes projections which use opaque types (opaque types are otherwise linted against, which is would have previously made the test cases added in this PR fail).
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 19, 2020
…in-projections, r=estebank

lint: normalize projections using opaque types

Fixes rust-lang#73251.

This PR normalizes projections which use opaque types (opaque types are otherwise linted against, which is would have previously made the test cases added in this PR fail).
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 20, 2020
…in-projections, r=estebank

lint: normalize projections using opaque types

Fixes rust-lang#73251.

This PR normalizes projections which use opaque types (opaque types are otherwise linted against, which is would have previously made the test cases added in this PR fail).
@bors bors closed this as completed in 9003087 Jun 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants