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

Borrow checking can be effected by seemingly irrelevant statements #111298

Open
matthewjasper opened this issue May 6, 2023 · 4 comments
Open
Assignees
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. NLL-polonius Issues related for using Polonius in the borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthewjasper
Copy link
Contributor

I tried this code:

trait Trait { type Item; }

impl<'a, X> Trait for &'a Vec<X> {
    type Item = &'a X;
}

impl<X> Trait for Box<dyn Trait<Item = X>> {
    type Item = X;
}

fn make_dyn_trait(_: &()) -> Box<dyn Trait<Item = &()>> {
    todo!()
}

struct diff<'a, M, N, S>(N, S)
where
    M: 'a,
    N: Trait<Item = &'a M>,
    S: Trait<Item = &'a M>;

fn may_panic<X>(_: X) { }

struct Droppable;

impl Drop for Droppable {
    fn drop(&mut self) {}
}


fn main() {
    let dyn_trait = make_dyn_trait(&());
    let storage = vec![()];
    let _x = may_panic(());
    let storage_ref = &storage;
    // Comment this line and compilation fails
    let a = Droppable;
    diff(dyn_trait, storage_ref);
}

I expected to see this happen: Commenting let statement has no effect on borrowck

Instead, this happened: borrowck succeed with the additional statement

See #80949 (comment) for an explanation of a similar bug that occurred after a change in mir generation.

@matthewjasper matthewjasper added the C-bug Category: This is a bug. label May 6, 2023
@matthewjasper matthewjasper self-assigned this May 6, 2023
@TechHara

This comment was marked as off-topic.

@TechHara

This comment was marked as off-topic.

@aliemjay aliemjay self-assigned this May 7, 2023
@matthewjasper
Copy link
Contributor Author

@TechHara I believe that's #69367

@jyn514 jyn514 added A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 26, 2023
@matthewjasper
Copy link
Contributor Author

matthewjasper commented Jun 28, 2023

This should probably be left until flow sensitive outlives (Polonius) is stabilized. In this example the inconsistent behaviour with the let x line can be fixed by adding more basic blocks to the MIR, but adding the second line results in MIR that needs more than basic block differences.

trait Trait {
    type Item;
}

impl<'a, X> Trait for &'a Vec<X> {
    type Item = &'a X;
}

impl<'a> Trait for &'a Droppable {
    type Item = &'a ();
}

impl<X> Trait for Box<dyn Trait<Item = X>> {
    type Item = X;
}

fn make_dyn_trait(_: &()) -> Box<dyn Trait<Item = &()>> {
    todo!()
}

struct diff<'a, 'b, M, N, S>(&'b N, &'b S)
where
    M: 'a,
    N: Trait<Item = &'a M>,
    S: Trait<Item = &'a M>;

fn may_panic<X>(_: X) {}

struct Droppable;

impl Drop for Droppable {
    fn drop(&mut self) {}
}

fn main2(c: bool) {
    let dyn_trait = make_dyn_trait(&());
    let (storage, mut optional_diff) = (Droppable, None);
    // uncomment this line only and this compiles
    // let x = String::new();
    let storage_ref = &storage;
    // uncomment this line and the line above doesn't change things
    // let y = String::new();
    optional_diff = Some(diff(&{ dyn_trait }, &storage_ref));
}

@matthewjasper matthewjasper added the NLL-polonius Issues related for using Polonius in the borrow checker label Jun 28, 2023
@matthewjasper matthewjasper changed the title Borrow checking can be effected by seemingly irrelavent statements Borrow checking can be effected by seemingly irrelevant statements Nov 3, 2023
@aliemjay aliemjay removed their assignment Nov 13, 2023
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 C-bug Category: This is a bug. NLL-polonius Issues related for using Polonius in the borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants