-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Unexpected token: expected ;
, found #
.
#106020
Comments
expected
;, found
#`.;
, found #
.
What is the issue you are reporting here @Jerrody ? |
Well, I thought that compiler can understand that |
Can you post a minimal reproducible example that demonstrates your issue? Or at least mention exactly what you'd expect to happen and why. Even code that is cfged out is still required to parse successfully. |
Let's show you what exactly problem I have. let instance_extensions = {
let surface_extensions =
ash_window::enumerate_required_extensions(window.raw_display_handle()).track()?;
#[cfg(feature = "validation")]
let debug_utils_ext = vec![ash::extensions::ext::DebugUtils::name().as_ptr()];
#[cfg(feature = "validation")]
[debug_utils_ext, (*surface_extensions).to_owned()].concat()
#[cfg(not(feature = "validation"))]
surface_extensions.to_owned()
}; And I get this error: error: expected `;`, found `#`
--> src\engine\renderer\context\instance.rs:44:73
|
44 | [debug_utils_ext, (*surface_extensions).to_owned()].concat()
| ^ help: add `;` here
45 |
46 | #[cfg(not(feature = "validation"))]
| - unexpected token |
You seem to already have posted this in this discussion in the internals forum a month ago. What you're asking seems a rather big change to me, I'm not that familiar with how the compiler parser works but I believe it currently requires your program syntax to be correct before |
Oh, damn, sorry. I totally forgot that I made a post. |
The workaround for this is to wrap the expressions in a block: let instance_extensions = {
let surface_extensions =
ash_window::enumerate_required_extensions(window.raw_display_handle()).track()?;
#[cfg(feature = "validation")]
let debug_utils_ext = vec![ash::extensions::ext::DebugUtils::name().as_ptr()];
#[cfg(feature = "validation")]
{ [debug_utils_ext, (*surface_extensions).to_owned()].concat() }
#[cfg(not(feature = "validation"))]
{ surface_extensions.to_owned() }
}; |
I solved this issue exactly as you did. |
Another way to do this is using the let instance_extensions = {
let surface_extensions =
ash_window::enumerate_required_extensions(window.raw_display_handle()).track()?;
#[cfg(feature = "validation")]
let debug_utils_ext = vec![ash::extensions::ext::DebugUtils::name().as_ptr()];
if cfg!(feature = "validation") {
[debug_utils_ext, (*surface_extensions).to_owned()].concat()
} else {
surface_extensions.to_owned()
}
}; |
Work is not the same, due to |
Handle attempts to have multiple `cfg`d tail expressions When encountering code that seems like it might be trying to have multiple tail expressions depending on `cfg` information, suggest alternatives that will success to parse. ```rust fn foo() -> String { #[cfg(feature = "validation")] [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() #[cfg(not(feature = "validation"))] String::new() } ``` ``` error: expected `;`, found `#` --> $DIR/multiple-tail-expr-behind-cfg.rs:5:64 | LL | #[cfg(feature = "validation")] | ------------------------------ only `;` terminated statements or tail expressions are allowed after this attribute LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() | ^ expected `;` here LL | #[cfg(not(feature = "validation"))] | - unexpected token | help: add `;` here | LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>(); | + help: alternatively, consider surrounding the expression with a block | LL | { [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() } | + + help: it seems like you are trying to provide different expressions depending on `cfg`, consider using `if cfg!(..)` | LL ~ if cfg!(feature = "validation") { LL ~ [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() LL ~ } else if cfg!(not(feature = "validation")) { LL ~ String::new() LL + } | ``` Fix rust-lang#106020. r? `@oli-obk`
Rollup merge of rust-lang#117988 - estebank:issue-106020, r=cjgillot Handle attempts to have multiple `cfg`d tail expressions When encountering code that seems like it might be trying to have multiple tail expressions depending on `cfg` information, suggest alternatives that will success to parse. ```rust fn foo() -> String { #[cfg(feature = "validation")] [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() #[cfg(not(feature = "validation"))] String::new() } ``` ``` error: expected `;`, found `#` --> $DIR/multiple-tail-expr-behind-cfg.rs:5:64 | LL | #[cfg(feature = "validation")] | ------------------------------ only `;` terminated statements or tail expressions are allowed after this attribute LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() | ^ expected `;` here LL | #[cfg(not(feature = "validation"))] | - unexpected token | help: add `;` here | LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>(); | + help: alternatively, consider surrounding the expression with a block | LL | { [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() } | + + help: it seems like you are trying to provide different expressions depending on `cfg`, consider using `if cfg!(..)` | LL ~ if cfg!(feature = "validation") { LL ~ [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() LL ~ } else if cfg!(not(feature = "validation")) { LL ~ String::new() LL + } | ``` Fix rust-lang#106020. r? `@oli-obk`
Error code:
Metadata:
The text was updated successfully, but these errors were encountered: