-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a lint for
'static
impl shenanigans (#265)
* wip: lint for impls of 'static things * Add lint for impl blocks containing `'static` things * Add more tests * Lint the span of the problematic type rather than the whole impl block * exhaustively match GenericArg Co-authored-by: Jubilee <[email protected]>
- Loading branch information
Showing
7 changed files
with
235 additions
and
0 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,18 @@ | ||
#![crate_type = "lib"] | ||
|
||
trait Foo {} | ||
struct Bar<A, B>(core::marker::PhantomData<(A, B)>); | ||
struct Baz<'a, A>(core::marker::PhantomData<&'a A>); | ||
trait Quux<A> {} | ||
|
||
impl<T> Foo for &'static T {} | ||
impl<T> Foo for &'static mut T {} | ||
impl<T> Foo for [&'static T] {} | ||
impl<T> Foo for &[&'static T] {} | ||
impl<T> Foo for (i32, [&'static T]) {} | ||
impl<T> Foo for (i32, [&'static T; 1]) {} | ||
impl<T> Foo for *const &'static T {} | ||
impl<T> Foo for Bar<i32, &'static T> {} | ||
impl<T> Foo for Baz<'static, T> {} | ||
impl<T> Foo for dyn Quux<&'static T> {} | ||
impl<T> Foo for &'static dyn Quux<T> {} |
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,70 @@ | ||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:8:17 | ||
| | ||
LL | impl<T> Foo for &'static T {} | ||
| ^^^^^^^^^^ | ||
| | ||
= note: `-F plrust-static-impls` implied by `-F plrust-lints` | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:9:17 | ||
| | ||
LL | impl<T> Foo for &'static mut T {} | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:10:17 | ||
| | ||
LL | impl<T> Foo for [&'static T] {} | ||
| ^^^^^^^^^^^^ | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:11:17 | ||
| | ||
LL | impl<T> Foo for &[&'static T] {} | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:12:17 | ||
| | ||
LL | impl<T> Foo for (i32, [&'static T]) {} | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:13:17 | ||
| | ||
LL | impl<T> Foo for (i32, [&'static T; 1]) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:14:17 | ||
| | ||
LL | impl<T> Foo for *const &'static T {} | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:15:17 | ||
| | ||
LL | impl<T> Foo for Bar<i32, &'static T> {} | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:16:17 | ||
| | ||
LL | impl<T> Foo for Baz<'static, T> {} | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:17:17 | ||
| | ||
LL | impl<T> Foo for dyn Quux<&'static T> {} | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_etc.rs:18:17 | ||
| | ||
LL | impl<T> Foo for &'static dyn Quux<T> {} | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 11 previous errors | ||
|
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,27 @@ | ||
#![crate_type = "lib"] | ||
|
||
use std::fmt::Display; | ||
|
||
trait Displayable { | ||
fn display(self) -> Box<dyn Display>; | ||
} | ||
|
||
// This is more complex than the one in the issue, to make sure the `Box`'s | ||
// lang_item status doesn't bite us. | ||
impl<T: Display> Displayable for (T, Box<Option<&'static T>>) { | ||
fn display(self) -> Box<dyn Display> { | ||
Box::new(self.0) | ||
} | ||
} | ||
|
||
fn extend_lt<T, U>(val: T) -> Box<dyn Display> | ||
where | ||
(T, Box<Option<U>>): Displayable, | ||
{ | ||
Displayable::display((val, Box::new(None))) | ||
} | ||
|
||
pub fn get_garbage(s: &str) -> String { | ||
let val = extend_lt(&String::from(s)); | ||
val.to_string() | ||
} |
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 @@ | ||
error: `impl` blocks for types containing `'static` references are not allowed | ||
--> $DIR/static_impl_unsound.rs:11:34 | ||
| | ||
LL | impl<T: Display> Displayable for (T, Box<Option<&'static T>>) { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-F plrust-static-impls` implied by `-F plrust-lints` | ||
|
||
error: aborting due to previous error | ||
|