-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #85478 - FabianWolff:issue-85348, r=petrochenkov
Disallow shadowing const parameters This pull request fixes #85348. Trying to shadow a `const` parameter as follows: ```rust fn foo<const N: i32>() { let N @ _ = 0; } ``` currently causes an ICE. With my changes, I get: ``` error[E0530]: let bindings cannot shadow const parameters --> test.rs:2:9 | 1 | fn foo<const N: i32>() { | - the const parameter `N` is defined here 2 | let N @ _ = 0; | ^ cannot be named the same as a const parameter error: aborting due to previous error ``` This is the same error you get when trying to shadow a constant: ```rust const N: i32 = 0; let N @ _ = 0; ``` ``` error[E0530]: let bindings cannot shadow constants --> src/lib.rs:3:5 | 2 | const N: i32 = 0; | ----------------- the constant `N` is defined here 3 | let N @ _ = 0; | ^ cannot be named the same as a constant error: aborting due to previous error ``` The reason for disallowing shadowing in both cases is described [here](#33118 (comment)) (the comment there only talks about constants, but the same reasoning applies to `const` parameters).
- Loading branch information
Showing
7 changed files
with
124 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Checks whether shadowing a const parameter leads to an ICE (#85348). | ||
|
||
impl<const N: usize> ArrayWindowsExample { | ||
//~^ ERROR: cannot find type `ArrayWindowsExample` in this scope [E0412] | ||
fn next() { | ||
let mut N; | ||
//~^ ERROR: let bindings cannot shadow const parameters [E0530] | ||
//~| ERROR: type annotations needed [E0282] | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error[E0530]: let bindings cannot shadow const parameters | ||
--> $DIR/issue-85348.rs:6:17 | ||
| | ||
LL | impl<const N: usize> ArrayWindowsExample { | ||
| - the const parameter `N` is defined here | ||
... | ||
LL | let mut N; | ||
| ^ cannot be named the same as a const parameter | ||
|
||
error[E0412]: cannot find type `ArrayWindowsExample` in this scope | ||
--> $DIR/issue-85348.rs:3:22 | ||
| | ||
LL | impl<const N: usize> ArrayWindowsExample { | ||
| ^^^^^^^^^^^^^^^^^^^ not found in this scope | ||
|
||
error[E0282]: type annotations needed | ||
--> $DIR/issue-85348.rs:6:13 | ||
| | ||
LL | let mut N; | ||
| ^^^^^ consider giving `N` a type | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors have detailed explanations: E0282, E0412, E0530. | ||
For more information about an error, try `rustc --explain E0282`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Checks that const parameters cannot be shadowed with fresh bindings | ||
// even in syntactically unambiguous contexts. See | ||
// https://github.com/rust-lang/rust/issues/33118#issuecomment-233962221 | ||
|
||
fn foo<const N: i32>(i: i32) -> bool { | ||
match i { | ||
N @ _ => true, | ||
//~^ ERROR: match bindings cannot shadow const parameters [E0530] | ||
} | ||
} | ||
|
||
fn bar<const N: i32>(i: i32) -> bool { | ||
let N @ _ = 0; | ||
//~^ ERROR: let bindings cannot shadow const parameters [E0530] | ||
match i { | ||
N @ _ => true, | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error[E0530]: match bindings cannot shadow const parameters | ||
--> $DIR/shadow-const-param.rs:7:9 | ||
| | ||
LL | fn foo<const N: i32>(i: i32) -> bool { | ||
| - the const parameter `N` is defined here | ||
LL | match i { | ||
LL | N @ _ => true, | ||
| ^ cannot be named the same as a const parameter | ||
|
||
error[E0530]: let bindings cannot shadow const parameters | ||
--> $DIR/shadow-const-param.rs:13:9 | ||
| | ||
LL | fn bar<const N: i32>(i: i32) -> bool { | ||
| - the const parameter `N` is defined here | ||
LL | let N @ _ = 0; | ||
| ^ cannot be named the same as a const parameter | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0530`. |