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

Incorrect / Inconsistent behavior of deref coercion with {} #23014

Open
theemathas opened this issue Mar 4, 2015 · 7 comments
Open

Incorrect / Inconsistent behavior of deref coercion with {} #23014

theemathas opened this issue Mar 4, 2015 · 7 comments
Labels
A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@theemathas
Copy link
Contributor

In this code, the commented-out lines fail to compile.

struct Foo;

fn f(_: &Foo) {}

fn g(_: &&Foo) {}

fn main() {
    let x: &Foo = &Box::new(Foo);
    f(x);
    f(&Box::new(Foo));
    f(&(Box::new(Foo)));
    //f(&{Box::new(Foo)});
    let y: &Foo = &Box::new(Foo);
    g(&y);
    //g(&&Box::new(Foo));
    //g(&(&Box::new(Foo)));
    g(&{&Box::new(Foo)});
}

playpen

I believe that, among other things, f(&{Box::new(Foo)}); should compile since &{Box::new(Foo)} has type &Box<Foo>, which is deref-coercible to &Foo.

Additionally, the behavior is so inconsistent that it is basically impossible to predict whether the other lines will compile without trying them.

@steveklabnik
Copy link
Member

Whoah, &{}? I haven't seen that, though I guess it makes some degree of sense...

@steveklabnik steveklabnik added the A-type-system Area: Type system label Mar 4, 2015
@steveklabnik
Copy link
Member

Triage; no change.

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@jturner314
Copy link
Contributor

jturner314 commented Dec 6, 2017

I came across this today while writing a macro. Until this bug gets fixed, if you're in a situation where you need deref coercion of

&{ expr }

to work correctly, a workaround is to add an extra &*, like this:

&*&{ expr }

which will then coerce correctly.

@Spoonbender
Copy link

Triage: issue still reproduces on 2021 edition, tested on rustc 1.59.0 (9d1b2106e 2022-02-23)

@theemathas
Copy link
Contributor Author

theemathas commented Mar 21, 2022

Related or duplicate of #26978, apparently.

@pwnorbitals
Copy link

pwnorbitals commented May 25, 2022

Got bitten by this today with func(&mut unsafe { *(ptr as *mut _) }), struggling to understand why a move was triggered. Needed to unnecessarily expand the unsafe block (not too bad but this was surprising)

@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. labels Apr 5, 2023
@Noratrieb
Copy link
Member

quoting @compiler-errors about the first example:

it has to do with how "expectations" are propagated around blocks
and how we derive expectations due to & operators

Expectation::rvalue_hint(self, *ty)

fixing this will liekly unfortunately break other things

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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

7 participants