Skip to content

Can't create box of parenthesised expression (documentation, error reporting) #15386

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
ghost opened this issue Jul 3, 2014 · 5 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@ghost
Copy link

ghost commented Jul 3, 2014

Contents of test.rs:

fn main() {
    let x: Box<int> = box (2 + 2);
}

in 0.11.0, this yields:

test.rs:2:31: 2:32 error: unexpected token: `;`
test.rs:2   let x: Box<int> = box (2 + 2);
                                         ^

However, the following code compiles:

fn main() {
    let temp = 2 + 2;
    let x: Box<int> = box temp;
}

Even doing box (2) is considered invalid syntax.

EDIT: Apparently, box()(2) is valid syntax, but this isn't written down anywhere.

@steveklabnik
Copy link
Member

Yeah, if you add the parens, box currently expects that you're putting in a different allocator or whatever.

@o11c
Copy link

o11c commented Jul 4, 2014

box () (2 + 2)

@ghost
Copy link
Author

ghost commented Jul 5, 2014

I'm going to mark this as missing documentation and poor error reporting, then. The box()(...) syntax is really confusing, although I don't think that I've been following the project enough to put up an RFC for it. It seems to me that it should be reasonable to infer whether something is an allocator or an expression.

@ghost ghost changed the title Can't create box of parenthesised expression Can't create box of parenthesised expression (documentation, error reporting) Jul 5, 2014
@liigo
Copy link
Contributor

liigo commented Jul 6, 2014

box ... in HEAP ?

@ftxqxd
Copy link
Contributor

ftxqxd commented Aug 29, 2014

Is it possible that this ambiguity could be resolved, at least in the majority of cases? The parser could check after parsing box(EXPR) that there is an expression following it, and if there isn’t, parse it as box() (EXPR). The only ambiguity I can think of is with boxed blocks in ifs, whiles, and fors, which could just be parsed greedily:

if box(foo) {
    ...
}
{
    ...
}

would be parsed as

if (box(foo) { ... }) {
    ...
}

as it is today.

I made a small patch to syntax that does this, and it works fine. I’ll open up a pull request if this behaviour is desirable.

ftxqxd added a commit to ftxqxd/rust that referenced this issue Aug 30, 2014
This lets the parser parse expressions like `box (1i + 2)` as `box() (1i + 2)`.
Expressions are parsed greedily in `if`, `while`, and `for` expressions, so that
`if box(foo) {} {}` is parsed as `if (box(foo) {}) {}`. This is the same
behaviour as before this change.

Closes rust-lang#15386.
@bors bors closed this as completed in fb00015 Oct 31, 2014
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants