-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve: prohibit foreign statics w/ generics
This commit modifies resolve to disallow foreign statics that use parent generics. `improper_ctypes` is not written to support type parameters, as these are normally disallowed before the lint is run. Thus, type parameters in foreign statics must be prohibited before the lint. The only other case where this *could* have occured is in functions, but typeck prohibits this with a "foreign items may not have type parameters" error - a similar error did not exist for statics, because statics cannot have type parameters, but they can use any type parameters that are in scope (which isn't the case for functions). Signed-off-by: David Wood <david@davidtw.co>
- Loading branch information
Showing
8 changed files
with
266 additions
and
181 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
10 changes: 10 additions & 0 deletions
10
src/test/ui/resolve/issue-65025-extern-static-parent-generics.rs
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,10 @@ | ||
unsafe fn foo<A>() { | ||
extern "C" { | ||
static baz: *const A; | ||
//~^ ERROR can't use generic parameters from outer function | ||
} | ||
|
||
let bar: *const u64 = core::mem::transmute(&baz); | ||
} | ||
|
||
fn main() { } |
12 changes: 12 additions & 0 deletions
12
src/test/ui/resolve/issue-65025-extern-static-parent-generics.stderr
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 @@ | ||
error[E0401]: can't use generic parameters from outer function | ||
--> $DIR/issue-65025-extern-static-parent-generics.rs:3:28 | ||
| | ||
LL | unsafe fn foo<A>() { | ||
| - type parameter from outer function | ||
LL | extern "C" { | ||
LL | static baz: *const A; | ||
| ^ use of generic parameter from outer function | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0401`. |
29 changes: 29 additions & 0 deletions
29
src/test/ui/resolve/issue-65035-static-with-parent-generics.rs
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,29 @@ | ||
#![feature(const_generics)] | ||
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash | ||
|
||
fn f<T>() { | ||
extern "C" { | ||
static a: *const T; | ||
//~^ ERROR can't use generic parameters from outer function | ||
} | ||
} | ||
|
||
fn g<T: Default>() { | ||
static a: *const T = Default::default(); | ||
//~^ ERROR can't use generic parameters from outer function | ||
} | ||
|
||
fn h<const N: usize>() { | ||
extern "C" { | ||
static a: [u8; N]; | ||
//~^ ERROR can't use generic parameters from outer function | ||
} | ||
} | ||
|
||
fn i<const N: usize>() { | ||
static a: [u8; N] = [0; N]; | ||
//~^ ERROR can't use generic parameters from outer function | ||
//~^^ ERROR can't use generic parameters from outer function | ||
} | ||
|
||
fn main() {} |
53 changes: 53 additions & 0 deletions
53
src/test/ui/resolve/issue-65035-static-with-parent-generics.stderr
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,53 @@ | ||
error[E0401]: can't use generic parameters from outer function | ||
--> $DIR/issue-65035-static-with-parent-generics.rs:6:26 | ||
| | ||
LL | fn f<T>() { | ||
| - type parameter from outer function | ||
LL | extern "C" { | ||
LL | static a: *const T; | ||
| ^ use of generic parameter from outer function | ||
|
||
error[E0401]: can't use generic parameters from outer function | ||
--> $DIR/issue-65035-static-with-parent-generics.rs:12:22 | ||
| | ||
LL | fn g<T: Default>() { | ||
| - type parameter from outer function | ||
LL | static a: *const T = Default::default(); | ||
| ^ use of generic parameter from outer function | ||
|
||
error[E0401]: can't use generic parameters from outer function | ||
--> $DIR/issue-65035-static-with-parent-generics.rs:18:24 | ||
| | ||
LL | fn h<const N: usize>() { | ||
| - const parameter from outer function | ||
LL | extern "C" { | ||
LL | static a: [u8; N]; | ||
| ^ use of generic parameter from outer function | ||
|
||
error[E0401]: can't use generic parameters from outer function | ||
--> $DIR/issue-65035-static-with-parent-generics.rs:24:20 | ||
| | ||
LL | fn i<const N: usize>() { | ||
| - const parameter from outer function | ||
LL | static a: [u8; N] = [0; N]; | ||
| ^ use of generic parameter from outer function | ||
|
||
error[E0401]: can't use generic parameters from outer function | ||
--> $DIR/issue-65035-static-with-parent-generics.rs:24:29 | ||
| | ||
LL | fn i<const N: usize>() { | ||
| - const parameter from outer function | ||
LL | static a: [u8; N] = [0; N]; | ||
| ^ use of generic parameter from outer function | ||
|
||
warning: the feature `const_generics` is incomplete and may cause the compiler to crash | ||
--> $DIR/issue-65035-static-with-parent-generics.rs:1:12 | ||
| | ||
LL | #![feature(const_generics)] | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(incomplete_features)]` on by default | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0401`. |