Skip to content

closures accept ill-formed inputs #104478

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

Open
aliemjay opened this issue Nov 16, 2022 · 1 comment
Open

closures accept ill-formed inputs #104478

aliemjay opened this issue Nov 16, 2022 · 1 comment
Labels
A-borrow-checker Area: The borrow checker A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. NLL-sound Working towards the "invalid code does not compile" goal T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@aliemjay
Copy link
Member

aliemjay commented Nov 16, 2022

Both of these closures should fail borrowck: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=e9855ecda2f9427454cebce7738e598d

struct MyTy<T: Trait>(T);
trait Trait {}
impl Trait for &'static str {}
fn wf<T>(_: T) {}

fn test() {
    let _: for<'x> fn(MyTy<&'x str>) = |_| {}; // PASS!
    
    // but it should fail for the same reason this fails:
    let _: for<'x> fn(MyTy<&'x str>) = |x| wf(x); // FAIL
}

I think we should simply do WF-checking of closure input/output types in the closure environment, but doing so would cause accidental breakages due to #104477. So #104477 should be resolved first.

@rustbot label A-NLL NLL-sound C-bug T-types A-borrow-checker

@rustbot rustbot added A-borrow-checker Area: The borrow checker A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. NLL-sound Working towards the "invalid code does not compile" goal T-types Relevant to the types team, which will review and decide on the PR/issue. labels Nov 16, 2022
@aliemjay
Copy link
Member Author

aliemjay commented Nov 28, 2023

This is not exclusive to borrowck - typeck can also ignore the well-formedness of cloure args:

struct Iter<T: Iterator>(T);

fn foo(_: impl Fn(&Iter<u8>)) {}

fn main() {
    foo(|_| {});
}

edit: this has been fixed in #123531

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. NLL-sound Working towards the "invalid code does not compile" goal T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants