Skip to content

Borrow checking can be effected by seemingly irrelevant statements #111298

Open
@matthewjasper

Description

@matthewjasper

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.

Metadata

Metadata

Assignees

Labels

A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.NLL-poloniusIssues related for using Polonius in the borrow checkerT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions