diff options
Diffstat (limited to 'compiler/rustc_lint_defs')
-rw-r--r-- | compiler/rustc_lint_defs/src/builtin.rs | 101 | ||||
-rw-r--r-- | compiler/rustc_lint_defs/src/lib.rs | 61 |
2 files changed, 105 insertions, 57 deletions
diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index f00165cd3..61ee467f5 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -264,37 +264,6 @@ declare_lint! { } declare_lint! { - /// The `const_err` lint detects an erroneous expression while doing - /// constant evaluation. - /// - /// ### Example - /// - /// ```rust,compile_fail - /// #![allow(unconditional_panic)] - /// const C: i32 = 1/0; - /// ``` - /// - /// {{produces}} - /// - /// ### Explanation - /// - /// This lint detects constants that fail to evaluate. Allowing the lint will accept the - /// constant declaration, but any use of this constant will still lead to a hard error. This is - /// a future incompatibility lint; the plan is to eventually entirely forbid even declaring - /// constants that cannot be evaluated. See [issue #71800] for more details. - /// - /// [issue #71800]: https://github.com/rust-lang/rust/issues/71800 - pub CONST_ERR, - Deny, - "constant evaluation encountered erroneous expression", - @future_incompatible = FutureIncompatibleInfo { - reference: "issue #71800 <https://github.com/rust-lang/rust/issues/71800>", - reason: FutureIncompatibilityReason::FutureReleaseErrorReportNow, - }; - report_in_external_macro -} - -declare_lint! { /// The `unused_imports` lint detects imports that are never used. /// /// ### Example @@ -1458,6 +1427,7 @@ declare_lint! { "trait-object types were treated as different depending on marker-trait order", @future_incompatible = FutureIncompatibleInfo { reference: "issue #56484 <https://github.com/rust-lang/rust/issues/56484>", + reason: FutureIncompatibilityReason::FutureReleaseErrorReportNow, }; } @@ -2909,7 +2879,7 @@ declare_lint! { /// ### Example /// /// ```rust - /// #![feature(naked_functions)] + /// #![feature(asm_experimental_arch, naked_functions)] /// /// use std::arch::asm; /// @@ -3094,7 +3064,7 @@ declare_lint! { /// /// ### Example /// - /// ```rust + /// ```rust,compile_fail /// #![cfg_attr(debug_assertions, crate_type = "lib")] /// ``` /// @@ -3114,7 +3084,7 @@ declare_lint! { /// rustc instead of `#![cfg_attr(..., crate_type = "...")]` and /// `--crate-name` instead of `#![cfg_attr(..., crate_name = "...")]`. pub DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME, - Warn, + Deny, "detects usage of `#![cfg_attr(..., crate_type/crate_name = \"...\")]`", @future_incompatible = FutureIncompatibleInfo { reference: "issue #91632 <https://github.com/rust-lang/rust/issues/91632>", @@ -3206,12 +3176,62 @@ declare_lint! { /// [future-incompatible]: ../index.md#future-incompatible-lints pub REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS, Warn, - "tranparent type contains an external ZST that is marked #[non_exhaustive] or contains private fields", + "transparent type contains an external ZST that is marked #[non_exhaustive] or contains private fields", @future_incompatible = FutureIncompatibleInfo { reference: "issue #78586 <https://github.com/rust-lang/rust/issues/78586>", }; } +declare_lint! { + /// The `unstable_syntax_pre_expansion` lint detects the use of unstable + /// syntax that is discarded during attribute expansion. + /// + /// ### Example + /// + /// ```rust + /// #[cfg(FALSE)] + /// macro foo() {} + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// The input to active attributes such as `#[cfg]` or procedural macro + /// attributes is required to be valid syntax. Previously, the compiler only + /// gated the use of unstable syntax features after resolving `#[cfg]` gates + /// and expanding procedural macros. + /// + /// To avoid relying on unstable syntax, move the use of unstable syntax + /// into a position where the compiler does not parse the syntax, such as a + /// functionlike macro. + /// + /// ```rust + /// # #![deny(unstable_syntax_pre_expansion)] + /// + /// macro_rules! identity { + /// ( $($tokens:tt)* ) => { $($tokens)* } + /// } + /// + /// #[cfg(FALSE)] + /// identity! { + /// macro foo() {} + /// } + /// ``` + /// + /// This is a [future-incompatible] lint to transition this + /// to a hard error in the future. See [issue #65860] for more details. + /// + /// [issue #65860]: https://github.com/rust-lang/rust/issues/65860 + /// [future-incompatible]: ../index.md#future-incompatible-lints + pub UNSTABLE_SYNTAX_PRE_EXPANSION, + Warn, + "unstable syntax can change at any point in the future, causing a hard error!", + @future_incompatible = FutureIncompatibleInfo { + reference: "issue #65860 <https://github.com/rust-lang/rust/issues/65860>", + }; +} + declare_lint_pass! { /// Does nothing as a lint pass, but registers some `Lint`s /// that are used by other parts of the compiler. @@ -3245,7 +3265,6 @@ declare_lint_pass! { EXPORTED_PRIVATE_DEPENDENCIES, PUB_USE_OF_PRIVATE_EXTERN_CRATE, INVALID_TYPE_PARAM_DEFAULT, - CONST_ERR, RENAMED_AND_REMOVED_LINTS, UNALIGNED_REFERENCES, CONST_ITEM_MUTATION, @@ -3280,6 +3299,7 @@ declare_lint_pass! { POINTER_STRUCTURAL_MATCH, NONTRIVIAL_STRUCTURAL_MATCH, SOFT_UNSTABLE, + UNSTABLE_SYNTAX_PRE_EXPANSION, INLINE_NO_SANITIZE, BAD_ASM_STYLE, ASM_SUB_REGISTER, @@ -3314,7 +3334,6 @@ declare_lint_pass! { DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME, DUPLICATE_MACRO_ATTRIBUTES, SUSPICIOUS_AUTO_TRAIT_IMPLS, - UNEXPECTED_CFGS, DEPRECATED_WHERE_CLAUSE_LOCATION, TEST_UNSTABLE_LINT, FFI_UNWIND_CALLS, @@ -3357,7 +3376,7 @@ declare_lint! { /// /// ### Example of drop reorder /// - /// ```rust,compile_fail + /// ```rust,edition2018,compile_fail /// #![deny(rust_2021_incompatible_closure_captures)] /// # #![allow(unused)] /// @@ -3393,7 +3412,7 @@ declare_lint! { /// /// ### Example of auto-trait /// - /// ```rust,compile_fail + /// ```rust,edition2018,compile_fail /// #![deny(rust_2021_incompatible_closure_captures)] /// use std::thread; /// @@ -3919,7 +3938,7 @@ declare_lint! { /// /// The compiler disables the automatic implementation if an explicit one /// exists for given type constructor. The exact rules governing this - /// are currently unsound and quite subtle and and will be modified in the future. + /// are currently unsound, quite subtle, and will be modified in the future. /// This change will cause the automatic implementation to be disabled in more /// cases, potentially breaking some code. pub SUSPICIOUS_AUTO_TRAIT_IMPLS, @@ -3938,8 +3957,6 @@ declare_lint! { /// ### Example /// /// ```rust - /// #![feature(generic_associated_types)] - /// /// trait Trait { /// type Assoc<'a> where Self: 'a; /// } diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 6acbe97a7..aa54b3d8a 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -1,4 +1,6 @@ #![feature(min_specialization)] +#![deny(rustc::untranslatable_diagnostic)] +#![deny(rustc::diagnostic_outside_of_impl)] #[macro_use] extern crate rustc_macros; @@ -7,7 +9,7 @@ pub use self::Level::*; use rustc_ast::node_id::{NodeId, NodeMap}; use rustc_ast::{AttrId, Attribute}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; -use rustc_error_messages::MultiSpan; +use rustc_error_messages::{DiagnosticMessage, MultiSpan}; use rustc_hir::HashStableContext; use rustc_hir::HirId; use rustc_span::edition::Edition; @@ -23,6 +25,9 @@ macro_rules! pluralize { ($x:expr) => { if $x != 1 { "s" } else { "" } }; + ("has", $x:expr) => { + if $x == 1 { "has" } else { "have" } + }; ("is", $x:expr) => { if $x == 1 { "is" } else { "are" } }; @@ -39,7 +44,8 @@ macro_rules! pluralize { /// All suggestions are marked with an `Applicability`. Tools use the applicability of a suggestion /// to determine whether it should be automatically applied or if the user should be consulted /// before applying the suggestion. -#[derive(Copy, Clone, Debug, PartialEq, Hash, Encodable, Decodable, Serialize, Deserialize)] +#[derive(Copy, Clone, Debug, Hash, Encodable, Decodable, Serialize, Deserialize)] +#[derive(PartialEq, Eq, PartialOrd, Ord)] pub enum Applicability { /// The suggestion is definitely what the user intended, or maintains the exact meaning of the code. /// This suggestion should be automatically applied. @@ -89,7 +95,7 @@ pub enum LintExpectationId { /// stable and can be cached. The additional index ensures that nodes with /// several expectations can correctly match diagnostics to the individual /// expectation. - Stable { hir_id: HirId, attr_index: u16, lint_index: Option<u16> }, + Stable { hir_id: HirId, attr_index: u16, lint_index: Option<u16>, attr_id: Option<AttrId> }, } impl LintExpectationId { @@ -113,13 +119,31 @@ impl LintExpectationId { *lint_index = new_lint_index } + + /// Prepares the id for hashing. Removes references to the ast. + /// Should only be called when the id is stable. + pub fn normalize(self) -> Self { + match self { + Self::Stable { hir_id, attr_index, lint_index, .. } => { + Self::Stable { hir_id, attr_index, lint_index, attr_id: None } + } + Self::Unstable { .. } => { + unreachable!("`normalize` called when `ExpectationId` is unstable") + } + } + } } impl<HCX: rustc_hir::HashStableContext> HashStable<HCX> for LintExpectationId { #[inline] fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { match self { - LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => { + LintExpectationId::Stable { + hir_id, + attr_index, + lint_index: Some(lint_index), + attr_id: _, + } => { hir_id.hash_stable(hcx, hasher); attr_index.hash_stable(hcx, hasher); lint_index.hash_stable(hcx, hasher); @@ -139,9 +163,12 @@ impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectation #[inline] fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType { match self { - LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => { - (*hir_id, *attr_index, *lint_index) - } + LintExpectationId::Stable { + hir_id, + attr_index, + lint_index: Some(lint_index), + attr_id: _, + } => (*hir_id, *attr_index, *lint_index), _ => { unreachable!("HashStable should only be called for a filled `LintExpectationId`") } @@ -489,7 +516,7 @@ pub struct BufferedEarlyLint { pub span: MultiSpan, /// The lint message. - pub msg: String, + pub msg: DiagnosticMessage, /// The `NodeId` of the AST node that generated the lint. pub node_id: NodeId, @@ -518,11 +545,11 @@ impl LintBuffer { lint: &'static Lint, node_id: NodeId, span: MultiSpan, - msg: &str, + msg: impl Into<DiagnosticMessage>, diagnostic: BuiltinLintDiagnostics, ) { let lint_id = LintId::of(lint); - let msg = msg.to_string(); + let msg = msg.into(); self.add_early_lint(BufferedEarlyLint { lint_id, node_id, span, msg, diagnostic }); } @@ -535,7 +562,7 @@ impl LintBuffer { lint: &'static Lint, id: NodeId, sp: impl Into<MultiSpan>, - msg: &str, + msg: impl Into<DiagnosticMessage>, ) { self.add_lint(lint, id, sp.into(), msg, BuiltinLintDiagnostics::Normal) } @@ -545,7 +572,7 @@ impl LintBuffer { lint: &'static Lint, id: NodeId, sp: impl Into<MultiSpan>, - msg: &str, + msg: impl Into<DiagnosticMessage>, diagnostic: BuiltinLintDiagnostics, ) { self.add_lint(lint, id, sp.into(), msg, diagnostic) @@ -655,18 +682,21 @@ macro_rules! declare_lint { macro_rules! declare_tool_lint { ( $(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level: ident, $desc: expr + $(, @feature_gate = $gate:expr;)? ) => ( - $crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, false} + $crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, false $(, @feature_gate = $gate;)?} ); ( $(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr, report_in_external_macro: $rep:expr + $(, @feature_gate = $gate:expr;)? ) => ( - $crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, $rep} + $crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, $rep $(, @feature_gate = $gate;)?} ); ( $(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr, $external:expr + $(, @feature_gate = $gate:expr;)? ) => ( $(#[$attr])* $vis static $NAME: &$crate::Lint = &$crate::Lint { @@ -677,8 +707,9 @@ macro_rules! declare_tool_lint { report_in_external_macro: $external, future_incompatible: None, is_plugin: true, - feature_gate: None, + $(feature_gate: Some($gate),)? crate_level_only: false, + ..$crate::Lint::default_fields_for_macro() }; ); } |