Skip to content

Matching Box(<pattern>) against Box<T> should give privacy error, not type mismatch #22207

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

Closed
shepmaster opened this issue Feb 12, 2015 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@shepmaster
Copy link
Member

While looking at #22205, I tried this as a workaround:

struct NonCopy;

fn main() {
    let x = Box::new(NonCopy);
    let Box(y) = x;
}

However, this has the error:

<anon>:5:9: 5:15 error: mismatched types:
 expected `Box<NonCopy>`,
    found `alloc::boxed::Box<_>`
(expected box,
    found struct `alloc::boxed::Box`) [E0308]
<anon>:5     let Box(y) = x;
                 ^~~~~~
@huonw
Copy link
Member

huonw commented Feb 12, 2015

Other than the rather strange error message, this isn't a bug (at least, there's no reason it should work). The Box struct is defined like

struct Box<T>(Unique<T>);

so a Box(y) pattern is trying to bind y to the Unique<T> pointer, not to the T itself.

It should be failing with a privacy error, like:

pub mod foo {
    pub struct Bar(i32);
    impl Bar {
        pub fn new(x: i32) -> Bar { Bar(x) }
    }
}

use foo::Bar;

fn main() {
    let x = Bar::new(1);
    let Bar(y) = x;
}
<anon>:12:13: 12:14 error: field #1 of struct `foo::Bar` is private
<anon>:12     let Bar(y) = x;
                      ^

@huonw huonw added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 12, 2015
@huonw huonw changed the title Cannot use a Box as a destructuring target Matching Box(<pattern>) against Box<T> should give privacy error, not type mismatch Feb 12, 2015
@pnkfelix
Copy link
Member

See also #22045, which argues that we might want to be able to put a separate Box entity into the value-namespace, and leave the existing box solely in the type-namespace (i.e., change it to a non-tuple struct), which would change the expected error message here.

But yeah, I wouldn't be at all surprised if this were a consequence of the current special treatment of Box in the compiler.

@florence
Copy link

FWIW, I work around this by using #![feature(box_syntax)].

let box y = x;

compiles just fine.

@steveklabnik
Copy link
Member

Triage: still giveing the original error, rather than a privacy error.

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

This now gives a privacy error; I don't think we really need a test here. Closing.

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 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

6 participants