-
Notifications
You must be signed in to change notification settings - Fork 13k
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
pretty printer fails to include necessary parens around some block expressions #22450
Comments
(If you are wondering why it is so important to be able to recompile the source generated by |
(oh, and in case its not clear: the workaround presented by |
see also #20937 |
Triage: no change |
Note: the specific issue here is related to #17930, since that suggests that |
Triage: no change |
Fixed by #119105. Output of Before: invalid syntax. #![feature(prelude_import)]
#![allow(unused_parens)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
macro_rules! add1_block { ($e:expr) => { { let val = 1; $e + val } } }
macro_rules! add1_paren_block { ($e:expr) => { ({ let val = 1; $e + val }) } }
fn main() {
let w = { let val = 1; 2 + val };
let x = { { let val = 1; 3 + val } as u64 };
let y = ({ let val = 1; 4 + val });
let z = { ({ let val = 1; 5 + val }) as u64 };
{
::std::io::_print(format_args!("w: {0} x: {1} y: {2} z: {3}\n", w, x,
y, z));
};
} error: expected expression, found `as`
|
14 | let x = { { let val = 1; 3 + val } as u64 };
| ^^ expected expression
|
help: parentheses are required to parse this as an expression
|
14 | let x = { ({ let val = 1; 3 + val }) as u64 };
| + + After: valid. #![feature(prelude_import)]
#![allow(unused_parens)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
macro_rules! add1_block { ($e:expr) => { { let val = 1; $e + val } } }
macro_rules! add1_paren_block { ($e:expr) => { ({ let val = 1; $e + val }) } }
fn main() {
let w = { let val = 1; 2 + val };
let x = { ({ let val = 1; 3 + val }) as u64 };
let y = ({ let val = 1; 4 + val });
let z = { ({ let val = 1; 5 + val }) as u64 };
{
::std::io::_print(format_args!("w: {0} x: {1} y: {2} z: {3}\n", w, x,
y, z));
};
} |
The pretty printer fails to include necessary parens around some block expressions
In particular, consider this code (stored in
/tmp/demo5.rs
):The above code compiles and runs, and you can even run it through the non-expanding pretty printer and compile that and run it:
However, if you apply
--pretty=expanded
, then the generated source fails to compile:The reason for this is that the use of
add1_block
within a cast as the last expression of a block is interpreted as a statement, rather than part of an expression. So you need to wrap it in parentheses (the way thatadd1_paren_block
does) if you want the generated output source to be robust enough to be recompiled.The text was updated successfully, but these errors were encountered: