-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Implement allow-by-default multiple_supertrait_upcastable
lint
#105484
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::{LateContext, LateLintPass, LintContext}; | ||
|
||
use rustc_errors::DelayDm; | ||
use rustc_hir as hir; | ||
use rustc_span::sym; | ||
|
||
declare_lint! { | ||
/// The `multiple_supertrait_upcastable` lint detects when an object-safe trait has multiple | ||
/// supertraits. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// trait A {} | ||
/// trait B {} | ||
/// | ||
/// #[warn(multiple_supertrait_upcastable)] | ||
/// trait C: A + B {} | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// To support upcasting with multiple supertraits, we need to store multiple vtables and this | ||
/// can result in extra space overhead, even if no code actually uses upcasting. | ||
/// This lint allows users to identify when such scenarios occur and to decide whether the | ||
/// additional overhead is justified. | ||
pub MULTIPLE_SUPERTRAIT_UPCASTABLE, | ||
Allow, | ||
"detect when an object-safe trait has multiple supertraits", | ||
@feature_gate = sym::multiple_supertrait_upcastable; | ||
} | ||
|
||
declare_lint_pass!(MultipleSupertraitUpcastable => [MULTIPLE_SUPERTRAIT_UPCASTABLE]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { | ||
let def_id = item.owner_id.to_def_id(); | ||
if let hir::ItemKind::Trait(_, _, _, _, _) = item.kind | ||
&& cx.tcx.is_object_safe(def_id) | ||
{ | ||
let direct_super_traits_iter = cx.tcx | ||
.super_predicates_of(def_id) | ||
.predicates | ||
.into_iter() | ||
.filter_map(|(pred, _)| pred.to_opt_poly_trait_pred()); | ||
if direct_super_traits_iter.count() > 1 { | ||
cx.struct_span_lint( | ||
MULTIPLE_SUPERTRAIT_UPCASTABLE, | ||
cx.tcx.def_span(def_id), | ||
DelayDm(|| { | ||
format!( | ||
"`{}` is object-safe and has multiple supertraits", | ||
item.ident, | ||
) | ||
}), | ||
|diag| diag, | ||
); | ||
} | ||
} | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/test/ui/feature-gates/feature-gate-multiple_supertrait_upcastable.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,12 @@ | ||
// check-pass | ||
|
||
#![deny(multiple_supertrait_upcastable)] | ||
//~^ WARNING unknown lint: `multiple_supertrait_upcastable` | ||
//~| WARNING unknown lint: `multiple_supertrait_upcastable` | ||
//~| WARNING unknown lint: `multiple_supertrait_upcastable` | ||
#![warn(multiple_supertrait_upcastable)] | ||
//~^ WARNING unknown lint: `multiple_supertrait_upcastable` | ||
//~| WARNING unknown lint: `multiple_supertrait_upcastable` | ||
//~| WARNING unknown lint: `multiple_supertrait_upcastable` | ||
|
||
fn main() {} |
57 changes: 57 additions & 0 deletions
57
src/test/ui/feature-gates/feature-gate-multiple_supertrait_upcastable.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,57 @@ | ||
warning: unknown lint: `multiple_supertrait_upcastable` | ||
--> $DIR/feature-gate-multiple_supertrait_upcastable.rs:3:1 | ||
| | ||
LL | #![deny(multiple_supertrait_upcastable)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `multiple_supertrait_upcastable` lint is unstable | ||
= help: add `#![feature(multiple_supertrait_upcastable)]` to the crate attributes to enable | ||
= note: `#[warn(unknown_lints)]` on by default | ||
|
||
warning: unknown lint: `multiple_supertrait_upcastable` | ||
--> $DIR/feature-gate-multiple_supertrait_upcastable.rs:7:1 | ||
| | ||
LL | #![warn(multiple_supertrait_upcastable)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `multiple_supertrait_upcastable` lint is unstable | ||
= help: add `#![feature(multiple_supertrait_upcastable)]` to the crate attributes to enable | ||
|
||
warning: unknown lint: `multiple_supertrait_upcastable` | ||
--> $DIR/feature-gate-multiple_supertrait_upcastable.rs:3:1 | ||
| | ||
LL | #![deny(multiple_supertrait_upcastable)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `multiple_supertrait_upcastable` lint is unstable | ||
= help: add `#![feature(multiple_supertrait_upcastable)]` to the crate attributes to enable | ||
|
||
warning: unknown lint: `multiple_supertrait_upcastable` | ||
--> $DIR/feature-gate-multiple_supertrait_upcastable.rs:7:1 | ||
| | ||
LL | #![warn(multiple_supertrait_upcastable)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `multiple_supertrait_upcastable` lint is unstable | ||
= help: add `#![feature(multiple_supertrait_upcastable)]` to the crate attributes to enable | ||
|
||
warning: unknown lint: `multiple_supertrait_upcastable` | ||
--> $DIR/feature-gate-multiple_supertrait_upcastable.rs:3:1 | ||
| | ||
LL | #![deny(multiple_supertrait_upcastable)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `multiple_supertrait_upcastable` lint is unstable | ||
= help: add `#![feature(multiple_supertrait_upcastable)]` to the crate attributes to enable | ||
|
||
warning: unknown lint: `multiple_supertrait_upcastable` | ||
--> $DIR/feature-gate-multiple_supertrait_upcastable.rs:7:1 | ||
| | ||
LL | #![warn(multiple_supertrait_upcastable)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `multiple_supertrait_upcastable` lint is unstable | ||
= help: add `#![feature(multiple_supertrait_upcastable)]` to the crate attributes to enable | ||
|
||
warning: 6 warnings emitted | ||
|
10 changes: 10 additions & 0 deletions
10
src/test/ui/traits/trait-upcasting/multiple_supertrait_upcastable.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 @@ | ||
#![feature(multiple_supertrait_upcastable)] | ||
#![deny(multiple_supertrait_upcastable)] | ||
|
||
trait A {} | ||
trait B {} | ||
|
||
trait C: A + B {} | ||
//~^ ERROR `C` is object-safe and has multiple supertraits | ||
|
||
fn main() {} |
14 changes: 14 additions & 0 deletions
14
src/test/ui/traits/trait-upcasting/multiple_supertrait_upcastable.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,14 @@ | ||
error: `C` is object-safe and has multiple supertraits | ||
--> $DIR/multiple_supertrait_upcastable.rs:7:1 | ||
| | ||
LL | trait C: A + B {} | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/multiple_supertrait_upcastable.rs:2:9 | ||
| | ||
LL | #![deny(multiple_supertrait_upcastable)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are there so many copies of this error being emitted lol