Expect each lint in attribute individually (RFC-2383)

This commit is contained in:
xFrednet 2021-11-24 21:57:38 +01:00
parent a9bf9eaef5
commit aa2a0a83d9
No known key found for this signature in database
GPG Key ID: FCDCBF29AF64D601
12 changed files with 210 additions and 103 deletions

View File

@ -942,10 +942,16 @@ impl Handler {
let mut inner = self.inner.borrow_mut();
for mut diag in diags.into_iter() {
if let Some(unstable_id) = diag.level.get_expectation_id() {
if let Some(stable_id) = unstable_to_stable.get(&unstable_id) {
diag.level = Level::Expect(*stable_id);
inner.fulfilled_expectations.insert(*stable_id);
if let Some(mut unstable_id) = diag.level.get_expectation_id() {
let lint_index = unstable_id.get_lint_index();
// The unstable to stable map only maps the unstable it to a stable id
// the lint index is manually transferred here.
unstable_id.set_lint_index(None);
if let Some(mut stable_id) = unstable_to_stable.get(&unstable_id).map(|id| *id) {
stable_id.set_lint_index(lint_index);
diag.level = Level::Expect(stable_id);
inner.fulfilled_expectations.insert(stable_id);
}
}
@ -1007,7 +1013,7 @@ impl HandlerInner {
// Diagnostics created before the definition of `HirId`s are unstable and can not yet
// be stored. Instead, they are buffered until the `LintExpectationId` is replaced by
// a stable one by the `LintLevelsBuilder`.
if let Level::Expect(LintExpectationId::Unstable(_)) = diagnostic.level {
if let Level::Expect(LintExpectationId::Unstable { .. }) = diagnostic.level {
self.unstable_expect_diagnostics.push(diagnostic.clone());
return;
}

View File

@ -243,9 +243,7 @@ impl<'s> LintLevelsBuilder<'s> {
let sess = self.sess;
let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
for (attr_index, attr) in attrs.iter().enumerate() {
let level = match Level::from_symbol(attr.name_or_empty(), || {
LintExpectationId::Unstable(attr.id)
}) {
let level = match Level::from_attr(attr) {
None => continue,
Some(Level::Expect(unstable_id)) if source_hir_id.is_some() => {
let stable_id =
@ -305,9 +303,23 @@ impl<'s> LintLevelsBuilder<'s> {
}
}
for li in metas {
let (unfulfilled_lint_lvl, unfulfilled_lint_src) = self.sets.get_lint_level(
builtin::UNFULFILLED_LINT_EXPECTATIONS,
self.cur,
Some(&specs),
&sess,
);
for (lint_index, li) in metas.iter_mut().enumerate() {
let level = match level {
Level::Expect(mut id) => {
id.set_lint_index(Some(lint_index));
Level::Expect(id)
}
level => level,
};
let sp = li.span();
let mut meta_item = match li {
let meta_item = match li {
ast::NestedMetaItem::MetaItem(meta_item) if meta_item.is_word() => meta_item,
_ => {
let mut err = bad_attr(sp);
@ -347,6 +359,17 @@ impl<'s> LintLevelsBuilder<'s> {
self.check_gated_lint(id, attr.span);
self.insert_spec(&mut specs, id, (level, src));
}
if let Level::Expect(expect_id) = level {
self.lint_expectations.insert(
expect_id,
LintExpectation::new(
reason,
sp,
unfulfilled_lint_lvl,
unfulfilled_lint_src,
),
);
}
}
CheckLintNameResult::Tool(result) => {
@ -362,6 +385,17 @@ impl<'s> LintLevelsBuilder<'s> {
for id in ids {
self.insert_spec(&mut specs, *id, (level, src));
}
if let Level::Expect(expect_id) = level {
self.lint_expectations.insert(
expect_id,
LintExpectation::new(
reason,
sp,
unfulfilled_lint_lvl,
unfulfilled_lint_src,
),
);
}
}
Err((Some(ids), ref new_lint_name)) => {
let lint = builtin::RENAMED_AND_REMOVED_LINTS;
@ -398,6 +432,17 @@ impl<'s> LintLevelsBuilder<'s> {
for id in ids {
self.insert_spec(&mut specs, *id, (level, src));
}
if let Level::Expect(expect_id) = level {
self.lint_expectations.insert(
expect_id,
LintExpectation::new(
reason,
sp,
unfulfilled_lint_lvl,
unfulfilled_lint_src,
),
);
}
}
Err((None, _)) => {
// If Tool(Err(None, _)) is returned, then either the lint does not
@ -491,31 +536,22 @@ impl<'s> LintLevelsBuilder<'s> {
self.check_gated_lint(id, attr.span);
self.insert_spec(&mut specs, id, (level, src));
}
if let Level::Expect(expect_id) = level {
self.lint_expectations.insert(
expect_id,
LintExpectation::new(
reason,
sp,
unfulfilled_lint_lvl,
unfulfilled_lint_src,
),
);
}
} else {
panic!("renamed lint does not exist: {}", new_name);
}
}
}
if !specs.is_empty() {
// Only lints that are currently registered in the lint store
// have been found and added to `specs`. Creating the expectation
// here ensures that it can be fulfilled during this compilation
// session.
if let Level::Expect(expect_id) = level {
let has_lints = specs
.values()
.any(|(lvl, _src)| matches!(lvl, Level::Expect(check_id) if check_id.eq(&expect_id)));
if has_lints {
let lint = builtin::UNFULFILLED_LINT_EXPECTATIONS;
let (lvl, src) =
self.sets.get_lint_level(lint, self.cur, Some(&specs), &sess);
let expectation = LintExpectation::new(reason, attr.span, lvl, src);
self.lint_expectations.insert(expect_id, expectation);
}
}
}
}
if !is_crate_node {
@ -565,7 +601,7 @@ impl<'s> LintLevelsBuilder<'s> {
hir_id: HirId,
attr_index: usize,
) -> LintExpectationId {
let stable_id = LintExpectationId::Stable { hir_id, attr_index };
let stable_id = LintExpectationId::Stable { hir_id, attr_index, lint_index: None };
self.expectation_id_map.insert(unstable_id, stable_id);

View File

@ -5,7 +5,7 @@ extern crate rustc_macros;
pub use self::Level::*;
use rustc_ast::node_id::{NodeId, NodeMap};
use rustc_ast::AttrId;
use rustc_ast::{AttrId, Attribute};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_hir::HirId;
use rustc_serialize::json::Json;
@ -63,56 +63,72 @@ pub enum Applicability {
/// These `LintExpectationId` will be updated to use the stable [`HirId`] once the
/// AST has been lowered. The transformation is done by the
/// [`LintLevelsBuilder`][`rustc_lint::levels::LintLevelsBuilder`]
///
/// Each lint inside the `expect` attribute is tracked individually, the `lint_index`
/// identifies the lint inside the attribute and ensures that the IDs are unique.
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, Encodable, Decodable)]
pub enum LintExpectationId {
/// Used for lints emitted during the `EarlyLintPass`. This id is not
/// has stable and should not be cached.
Unstable(AttrId),
Unstable { attr_id: AttrId, lint_index: Option<usize> },
/// The [`HirId`] that the lint expectation is attached to. This id is
/// 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: usize },
Stable { hir_id: HirId, attr_index: usize, lint_index: Option<usize> },
}
impl LintExpectationId {
pub fn is_stable(&self) -> bool {
match self {
LintExpectationId::Unstable(_) => false,
LintExpectationId::Unstable { .. } => false,
LintExpectationId::Stable { .. } => true,
}
}
pub fn get_lint_index(&self) -> Option<usize> {
let (LintExpectationId::Unstable { lint_index, .. }
| LintExpectationId::Stable { lint_index, .. }) = self;
*lint_index
}
pub fn set_lint_index(&mut self, new_lint_index: Option<usize>) {
let (LintExpectationId::Unstable { ref mut lint_index, .. }
| LintExpectationId::Stable { ref mut lint_index, .. }) = self;
*lint_index = new_lint_index
}
}
impl<HCX: rustc_hir::HashStableContext> HashStable<HCX> for LintExpectationId {
#[inline]
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
match self {
LintExpectationId::Unstable(_) => {
unreachable!(
"HashStable should never be called for an unstable `LintExpectationId`"
)
}
LintExpectationId::Stable { hir_id, attr_index } => {
LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
hir_id.hash_stable(hcx, hasher);
attr_index.hash_stable(hcx, hasher);
lint_index.hash_stable(hcx, hasher);
}
_ => {
unreachable!("HashStable should only be called for a filled `LintExpectationId`")
}
}
}
}
impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectationId {
type KeyType = (HirId, usize);
type KeyType = (HirId, usize, usize);
#[inline]
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
match self {
LintExpectationId::Unstable(_) => {
unreachable!(
"HashStable should never be called for an unstable `LintExpectationId`"
)
LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
(*hir_id, *attr_index, *lint_index)
}
_ => {
unreachable!("HashStable should only be called for a filled `LintExpectationId`")
}
LintExpectationId::Stable { hir_id, attr_index } => (*hir_id, *attr_index),
}
}
}
@ -176,13 +192,13 @@ impl Level {
}
/// Converts a symbol to a level.
pub fn from_symbol<F>(x: Symbol, create_expectation_id: F) -> Option<Level>
where
F: FnOnce() -> LintExpectationId,
{
match x {
pub fn from_attr(attr: &Attribute) -> Option<Level> {
match attr.name_or_empty() {
sym::allow => Some(Level::Allow),
sym::expect => Some(Level::Expect(create_expectation_id())),
sym::expect => Some(Level::Expect(LintExpectationId::Unstable {
attr_id: attr.id,
lint_index: None,
})),
sym::warn => Some(Level::Warn),
sym::deny => Some(Level::Deny),
sym::forbid => Some(Level::Forbid),

View File

@ -49,7 +49,7 @@ use rustc_middle::mir::FakeReadCause;
use rustc_query_system::ich::{NodeIdHashingMode, StableHashingContext};
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames};
use rustc_session::lint::{Level, Lint, LintExpectationId};
use rustc_session::lint::{Level, Lint};
use rustc_session::Limit;
use rustc_session::Session;
use rustc_span::def_id::{DefPathHash, StableCrateId};
@ -2755,13 +2755,7 @@ impl<'tcx> TyCtxt<'tcx> {
return bound;
}
if hir.attrs(id).iter().enumerate().any(|(attr_index, attr)| {
Level::from_symbol(attr.name_or_empty(), || LintExpectationId::Stable {
hir_id: id,
attr_index,
})
.is_some()
}) {
if hir.attrs(id).iter().any(|attr| Level::from_attr(attr).is_some()) {
return id;
}
let next = hir.get_parent_node(id);

View File

@ -1,8 +1,8 @@
warning: this lint expectation is unfulfilled
--> $DIR/crate_level_expect.rs:7:1
--> $DIR/crate_level_expect.rs:7:11
|
LL | #![expect(unused_mut)]
| ^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^
|
= note: `#[warn(unfulfilled_lint_expectations)]` on by default

View File

@ -4,28 +4,55 @@
#![warn(unused)]
// The warnings are not double triggers, they identify different unfulfilled lint
// expectations one for each listed lint.
#[expect(unused_variables, unused_mut, while_true)]
//~^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
//~| NOTE `#[warn(unfulfilled_lint_expectations)]` on by default
//~| WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
fn check_multiple_lints_1() {
// This only trigger `unused_variables`
let who_am_i = 666;
}
#[expect(unused_variables, unused_mut, while_true)]
//~^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
//~| WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
fn check_multiple_lints_2() {
// This only triggers `unused_mut`
let mut x = 0;
println!("I use x: {}", x);
}
#[expect(unused_variables, unused_mut, while_true)]
//~^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
//~| WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
fn check_multiple_lints_3() {
// This only triggers `while_true` which is also an early lint
while true {}
}
#[expect(unused, while_true)]
//~^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
fn check_multiple_lints_with_lint_group_1() {
let who_am_i = 666;
let mut x = 0;
println!("I use x: {}", x);
}
#[expect(unused, while_true)]
//~^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
fn check_multiple_lints_with_lint_group_2() {
while true {}
}
fn main() {
check_multiple_lints_1();
check_multiple_lints_2();
check_multiple_lints_3();
check_multiple_lints_with_lint_group_1();
check_multiple_lints_with_lint_group_2();
}

View File

@ -0,0 +1,52 @@
warning: this lint expectation is unfulfilled
--> $DIR/expect_multiple_lints.rs:10:28
|
LL | #[expect(unused_variables, unused_mut, while_true)]
| ^^^^^^^^^^
|
= note: `#[warn(unfulfilled_lint_expectations)]` on by default
warning: this lint expectation is unfulfilled
--> $DIR/expect_multiple_lints.rs:28:10
|
LL | #[expect(unused_variables, unused_mut, while_true)]
| ^^^^^^^^^^^^^^^^
warning: this lint expectation is unfulfilled
--> $DIR/expect_multiple_lints.rs:19:10
|
LL | #[expect(unused_variables, unused_mut, while_true)]
| ^^^^^^^^^^^^^^^^
warning: this lint expectation is unfulfilled
--> $DIR/expect_multiple_lints.rs:28:28
|
LL | #[expect(unused_variables, unused_mut, while_true)]
| ^^^^^^^^^^
warning: this lint expectation is unfulfilled
--> $DIR/expect_multiple_lints.rs:36:18
|
LL | #[expect(unused, while_true)]
| ^^^^^^^^^^
warning: this lint expectation is unfulfilled
--> $DIR/expect_multiple_lints.rs:19:40
|
LL | #[expect(unused_variables, unused_mut, while_true)]
| ^^^^^^^^^^
warning: this lint expectation is unfulfilled
--> $DIR/expect_multiple_lints.rs:45:10
|
LL | #[expect(unused, while_true)]
| ^^^^^^
warning: this lint expectation is unfulfilled
--> $DIR/expect_multiple_lints.rs:10:40
|
LL | #[expect(unused_variables, unused_mut, while_true)]
| ^^^^^^^^^^
warning: 8 warnings emitted

View File

@ -6,10 +6,10 @@
#[expect(
unused_mut,
//~^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
//~| NOTE this `expect` is overridden by a `allow` attribute before the `unused_mut` lint is triggered
reason = "this `expect` is overridden by a `allow` attribute before the `unused_mut` lint is triggered"
)]
//~^^^^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
//~| NOTE this `expect` is overridden by a `allow` attribute before the `unused_mut` lint is triggered
mod foo {
fn bar() {
#[allow(
@ -22,11 +22,11 @@ mod foo {
#[expect(
unused_mut,
//~^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
//~| NOTE `#[warn(unfulfilled_lint_expectations)]` on by default
//~| NOTE this `expect` is overridden by a `warn` attribute before the `unused_mut` lint is triggered
reason = "this `expect` is overridden by a `warn` attribute before the `unused_mut` lint is triggered"
)]
//~^^^^ WARNING this lint expectation is unfulfilled [unfulfilled_lint_expectations]
//~| NOTE `#[warn(unfulfilled_lint_expectations)]` on by default
//~| NOTE this `expect` is overridden by a `warn` attribute before the `unused_mut` lint is triggered
mod oof {
#[warn(
unused_mut,

View File

@ -14,25 +14,19 @@ LL | unused_mut,
| ^^^^^^^^^^
warning: this lint expectation is unfulfilled
--> $DIR/expect_nested_lint_levels.rs:23:1
--> $DIR/expect_nested_lint_levels.rs:24:5
|
LL | / #[expect(
LL | | unused_mut,
LL | | reason = "this `expect` is overridden by a `warn` attribute before the `unused_mut` lint is triggered"
LL | | )]
| |__^
LL | unused_mut,
| ^^^^^^^^^^
|
= note: `#[warn(unfulfilled_lint_expectations)]` on by default
= note: this `expect` is overridden by a `warn` attribute before the `unused_mut` lint is triggered
warning: this lint expectation is unfulfilled
--> $DIR/expect_nested_lint_levels.rs:7:1
--> $DIR/expect_nested_lint_levels.rs:8:5
|
LL | / #[expect(
LL | | unused_mut,
LL | | reason = "this `expect` is overridden by a `allow` attribute before the `unused_mut` lint is triggered"
LL | | )]
| |__^
LL | unused_mut,
| ^^^^^^^^^^
|
= note: this `expect` is overridden by a `allow` attribute before the `unused_mut` lint is triggered

View File

@ -1,8 +1,8 @@
warning: this lint expectation is unfulfilled
--> $DIR/expect_with_reason.rs:6:1
--> $DIR/expect_with_reason.rs:6:11
|
LL | #![expect(unused_variables, reason = "<This should fail and display this reason>")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^
|
= note: `#[warn(unfulfilled_lint_expectations)]` on by default
= note: <This should fail and display this reason>

View File

@ -21,21 +21,6 @@ fn check_multiple_lint_emissions() {
let t = 9;
}
mod check_multiple_expected_lints {
#[expect(unused_variables, unused_mut)]
pub fn check_lint_1() {
// unused_variables should fulfill the expectation
let c = 17;
}
#[expect(unused_variables, unused_mut)]
pub fn check_lint_2() {
// unused_mut should fulfill the expectation
let mut c = 17;
let _ = c;
}
}
mod check_fulfilled_expect_in_macro {
macro_rules! expect_inside_macro {
() => {
@ -54,8 +39,5 @@ fn main() {
check_lint_group();
check_multiple_lint_emissions();
check_multiple_expected_lints::check_lint_1();
check_multiple_expected_lints::check_lint_2();
check_fulfilled_expect_in_macro::check_macro();
}

View File

@ -1,8 +1,8 @@
warning: this lint expectation is unfulfilled
--> $DIR/multiple_expect_attrs.rs:7:1
--> $DIR/multiple_expect_attrs.rs:7:10
|
LL | #[expect(unused_variables)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^
|
= note: `#[warn(unfulfilled_lint_expectations)]` on by default