Skip to content

Commit

Permalink
Move Lint to rustc_session
Browse files Browse the repository at this point in the history
This commit breaks early-lint registration, which will be fixed in the
next commit. This movement will allow essentially all crates in the compiler
tree to declare lints (though not lint passes).
  • Loading branch information
Mark-Simulacrum committed Dec 3, 2019
1 parent 433e546 commit 526ee51
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/librustc/lint/levels.rs
Expand Up @@ -93,7 +93,7 @@ impl LintLevelSets {

// If `level` is none then we actually assume the default level for this
// lint.
let mut level = level.unwrap_or_else(|| lint.default_level(sess));
let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));

// If we're about to issue a warning, check at the last minute for any
// directives against the warnings "lint". If, for example, there's an
Expand Down
86 changes: 1 addition & 85 deletions src/librustc/lint/mod.rs
Expand Up @@ -27,8 +27,6 @@ use crate::hir::def_id::{CrateNum, LOCAL_CRATE};
use crate::hir::intravisit;
use crate::hir;
use crate::lint::builtin::BuiltinLintDiagnostics;
use crate::lint::builtin::parser::{ILL_FORMED_ATTRIBUTE_INPUT, META_VARIABLE_MISUSE};
use crate::lint::builtin::parser::INCOMPLETE_INCLUDE;
use crate::session::{Session, DiagnosticMessageId};
use crate::ty::TyCtxt;
use crate::ty::query::Providers;
Expand All @@ -37,8 +35,6 @@ use errors::{DiagnosticBuilder, DiagnosticId};
use std::{hash, ptr};
use syntax::ast;
use syntax::source_map::{MultiSpan, ExpnKind, DesugaringKind};
use syntax::early_buffered_lints::BufferedEarlyLintId;
use syntax::edition::Edition;
use syntax::symbol::Symbol;
use syntax_pos::hygiene::MacroKind;
use syntax_pos::Span;
Expand All @@ -47,87 +43,7 @@ pub use crate::lint::context::{LateContext, EarlyContext, LintContext, LintStore
check_crate, check_ast_crate, late_lint_mod, CheckLintNameResult,
BufferedEarlyLint,};

pub use rustc_session::lint::Level;

/// Specification of a single lint.
#[derive(Copy, Clone, Debug)]
pub struct Lint {
/// A string identifier for the lint.
///
/// This identifies the lint in attributes and in command-line arguments.
/// In those contexts it is always lowercase, but this field is compared
/// in a way which is case-insensitive for ASCII characters. This allows
/// `declare_lint!()` invocations to follow the convention of upper-case
/// statics without repeating the name.
///
/// The name is written with underscores, e.g., "unused_imports".
/// On the command line, underscores become dashes.
pub name: &'static str,

/// Default level for the lint.
pub default_level: Level,

/// Description of the lint or the issue it detects.
///
/// e.g., "imports that are never used"
pub desc: &'static str,

/// Starting at the given edition, default to the given lint level. If this is `None`, then use
/// `default_level`.
pub edition_lint_opts: Option<(Edition, Level)>,

/// `true` if this lint is reported even inside expansions of external macros.
pub report_in_external_macro: bool,

pub future_incompatible: Option<FutureIncompatibleInfo>,

pub is_plugin: bool,
}

/// Extra information for a future incompatibility lint.
#[derive(Copy, Clone, Debug)]
pub struct FutureIncompatibleInfo {
/// e.g., a URL for an issue/PR/RFC or error code
pub reference: &'static str,
/// If this is an edition fixing lint, the edition in which
/// this lint becomes obsolete
pub edition: Option<Edition>,
}

impl Lint {
pub const fn default_fields_for_macro() -> Self {
Lint {
name: "",
default_level: Level::Forbid,
desc: "",
edition_lint_opts: None,
is_plugin: false,
report_in_external_macro: false,
future_incompatible: None,
}
}

/// Returns the `rust::lint::Lint` for a `syntax::early_buffered_lints::BufferedEarlyLintId`.
pub fn from_parser_lint_id(lint_id: BufferedEarlyLintId) -> &'static Self {
match lint_id {
BufferedEarlyLintId::IllFormedAttributeInput => ILL_FORMED_ATTRIBUTE_INPUT,
BufferedEarlyLintId::MetaVariableMisuse => META_VARIABLE_MISUSE,
BufferedEarlyLintId::IncompleteInclude => INCOMPLETE_INCLUDE,
}
}

/// Gets the lint's name, with ASCII letters converted to lowercase.
pub fn name_lower(&self) -> String {
self.name.to_ascii_lowercase()
}

pub fn default_level(&self, session: &Session) -> Level {
self.edition_lint_opts
.filter(|(e, _)| *e <= session.edition())
.map(|(_, l)| l)
.unwrap_or(self.default_level)
}
}
pub use rustc_session::lint::{Lint, Level, FutureIncompatibleInfo};

/// Declares a static item of type `&'static Lint`.
#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/session/mod.rs
Expand Up @@ -5,7 +5,7 @@ use rustc_session::cgu_reuse_tracker::CguReuseTracker;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};

use crate::lint;
use rustc_session::lint;
use crate::session::config::{OutputType, PrintRequest, Sanitizer, SwitchWithOptPath};
use crate::session::search_paths::{PathKind, SearchPath};
use crate::util::common::{duration_to_secs_str, ErrorReported};
Expand Down
72 changes: 72 additions & 0 deletions src/librustc_session/lint.rs
@@ -1,4 +1,5 @@
use syntax_pos::{Symbol, sym};
use syntax_pos::edition::Edition;
pub use self::Level::*;

/// Setting for how to handle a lint.
Expand Down Expand Up @@ -42,3 +43,74 @@ impl Level {
}
}
}

/// Specification of a single lint.
#[derive(Copy, Clone, Debug)]
pub struct Lint {
/// A string identifier for the lint.
///
/// This identifies the lint in attributes and in command-line arguments.
/// In those contexts it is always lowercase, but this field is compared
/// in a way which is case-insensitive for ASCII characters. This allows
/// `declare_lint!()` invocations to follow the convention of upper-case
/// statics without repeating the name.
///
/// The name is written with underscores, e.g., "unused_imports".
/// On the command line, underscores become dashes.
pub name: &'static str,

/// Default level for the lint.
pub default_level: Level,

/// Description of the lint or the issue it detects.
///
/// e.g., "imports that are never used"
pub desc: &'static str,

/// Starting at the given edition, default to the given lint level. If this is `None`, then use
/// `default_level`.
pub edition_lint_opts: Option<(Edition, Level)>,

/// `true` if this lint is reported even inside expansions of external macros.
pub report_in_external_macro: bool,

pub future_incompatible: Option<FutureIncompatibleInfo>,

pub is_plugin: bool,
}

/// Extra information for a future incompatibility lint.
#[derive(Copy, Clone, Debug)]
pub struct FutureIncompatibleInfo {
/// e.g., a URL for an issue/PR/RFC or error code
pub reference: &'static str,
/// If this is an edition fixing lint, the edition in which
/// this lint becomes obsolete
pub edition: Option<Edition>,
}

impl Lint {
pub const fn default_fields_for_macro() -> Self {
Lint {
name: "",
default_level: Level::Forbid,
desc: "",
edition_lint_opts: None,
is_plugin: false,
report_in_external_macro: false,
future_incompatible: None,
}
}

/// Gets the lint's name, with ASCII letters converted to lowercase.
pub fn name_lower(&self) -> String {
self.name.to_ascii_lowercase()
}

pub fn default_level(&self, edition: Edition) -> Level {
self.edition_lint_opts
.filter(|(e, _)| *e <= edition)
.map(|(_, l)| l)
.unwrap_or(self.default_level)
}
}

0 comments on commit 526ee51

Please sign in to comment.