Move early lint declarations to librustc_session

This commit is contained in:
Mark Rousskov 2019-11-12 12:09:20 -05:00
parent 526ee51ccc
commit f03d8f305a
18 changed files with 172 additions and 171 deletions

View File

@ -3700,6 +3700,7 @@ dependencies = [
"rustc_error_codes",
"rustc_feature",
"rustc_index",
"rustc_session",
"rustc_target",
"syntax",
"syntax_pos",
@ -4477,6 +4478,7 @@ dependencies = [
"rustc_index",
"rustc_lexer",
"rustc_macros",
"rustc_session",
"scoped-tls",
"serialize",
"smallvec 1.0.0",

View File

@ -12,6 +12,8 @@ use syntax::ast;
use syntax::edition::Edition;
use syntax::source_map::Span;
use syntax::symbol::Symbol;
use syntax::early_buffered_lints::{ILL_FORMED_ATTRIBUTE_INPUT, META_VARIABLE_MISUSE};
use rustc_session::declare_lint;
declare_lint! {
pub EXCEEDING_BITSHIFTS,
@ -404,31 +406,6 @@ declare_lint! {
};
}
/// Some lints that are buffered from `libsyntax`. See `syntax::early_buffered_lints`.
pub mod parser {
declare_lint! {
pub ILL_FORMED_ATTRIBUTE_INPUT,
Deny,
"ill-formed attribute inputs that were previously accepted and used in practice",
@future_incompatible = super::FutureIncompatibleInfo {
reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
edition: None,
};
}
declare_lint! {
pub META_VARIABLE_MISUSE,
Allow,
"possible meta-variable misuse at macro definition"
}
declare_lint! {
pub INCOMPLETE_INCLUDE,
Deny,
"trailing content in included file"
}
}
declare_lint! {
pub DEPRECATED_IN_FUTURE,
Allow,
@ -520,8 +497,8 @@ declare_lint_pass! {
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
MACRO_USE_EXTERN_CRATE,
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
parser::ILL_FORMED_ATTRIBUTE_INPUT,
parser::META_VARIABLE_MISUSE,
ILL_FORMED_ATTRIBUTE_INPUT,
META_VARIABLE_MISUSE,
DEPRECATED_IN_FUTURE,
AMBIGUOUS_ASSOCIATED_ITEMS,
MUTABLE_BORROW_RESERVATION_CONFLICT,

View File

@ -9,6 +9,7 @@ use errors::Applicability;
use rustc_data_structures::fx::FxHashMap;
use syntax::ast::{Ident, Item, ItemKind};
use syntax::symbol::{sym, Symbol};
use rustc_session::declare_tool_lint;
declare_tool_lint! {
pub rustc::DEFAULT_HASH_TYPES,

View File

@ -8,7 +8,7 @@ use crate::lint::{self, Lint, LintId, Level, LintSource};
use crate::session::Session;
use crate::util::nodemap::FxHashMap;
use errors::{Applicability, DiagnosticBuilder};
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHasher};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use syntax::ast;
use syntax::attr;
use syntax::feature_gate;
@ -566,19 +566,3 @@ impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
})
}
}
impl<HCX> HashStable<HCX> for LintId {
#[inline]
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
self.lint_name_raw().hash_stable(hcx, hasher);
}
}
impl<HCX> ToStableHashKey<HCX> for LintId {
type KeyType = &'static str;
#[inline]
fn to_stable_hash_key(&self, _: &HCX) -> &'static str {
self.lint_name_raw()
}
}

View File

@ -32,7 +32,6 @@ use crate::ty::TyCtxt;
use crate::ty::query::Providers;
use crate::util::nodemap::NodeMap;
use errors::{DiagnosticBuilder, DiagnosticId};
use std::{hash, ptr};
use syntax::ast;
use syntax::source_map::{MultiSpan, ExpnKind, DesugaringKind};
use syntax::symbol::Symbol;
@ -43,72 +42,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::{Lint, Level, FutureIncompatibleInfo};
/// Declares a static item of type `&'static Lint`.
#[macro_export]
macro_rules! declare_lint {
($vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
declare_lint!(
$vis $NAME, $Level, $desc,
);
);
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
$(@future_incompatible = $fi:expr;)? $($v:ident),*) => (
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
name: stringify!($NAME),
default_level: $crate::lint::$Level,
desc: $desc,
edition_lint_opts: None,
is_plugin: false,
$($v: true,)*
$(future_incompatible: Some($fi),)*
..$crate::lint::Lint::default_fields_for_macro()
};
);
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
$lint_edition: expr => $edition_level: ident
) => (
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
name: stringify!($NAME),
default_level: $crate::lint::$Level,
desc: $desc,
edition_lint_opts: Some(($lint_edition, $crate::lint::Level::$edition_level)),
report_in_external_macro: false,
is_plugin: false,
};
);
}
#[macro_export]
macro_rules! declare_tool_lint {
(
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level: ident, $desc: expr
) => (
declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, false}
);
(
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
report_in_external_macro: $rep:expr
) => (
declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, $rep}
);
(
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
$external:expr
) => (
$(#[$attr])*
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
name: &concat!(stringify!($tool), "::", stringify!($NAME)),
default_level: $crate::lint::$Level,
desc: $desc,
edition_lint_opts: None,
report_in_external_macro: $external,
future_incompatible: None,
is_plugin: true,
};
);
}
pub use rustc_session::lint::{Lint, LintId, Level, FutureIncompatibleInfo};
/// Declares a static `LintArray` and return it as an expression.
#[macro_export]
@ -420,46 +354,6 @@ pub type EarlyLintPassObject = Box<dyn EarlyLintPass + sync::Send + sync::Sync +
pub type LateLintPassObject = Box<dyn for<'a, 'tcx> LateLintPass<'a, 'tcx> + sync::Send
+ sync::Sync + 'static>;
/// Identifies a lint known to the compiler.
#[derive(Clone, Copy, Debug)]
pub struct LintId {
// Identity is based on pointer equality of this field.
lint: &'static Lint,
}
impl PartialEq for LintId {
fn eq(&self, other: &LintId) -> bool {
ptr::eq(self.lint, other.lint)
}
}
impl Eq for LintId { }
impl hash::Hash for LintId {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
let ptr = self.lint as *const Lint;
ptr.hash(state);
}
}
impl LintId {
/// Gets the `LintId` for a `Lint`.
pub fn of(lint: &'static Lint) -> LintId {
LintId {
lint,
}
}
pub fn lint_name_raw(&self) -> &'static str {
self.lint.name
}
/// Gets the name of the lint.
pub fn to_string(&self) -> String {
self.lint.name_lower()
}
}
/// How a lint level was set.
#[derive(Clone, Copy, PartialEq, Eq, HashStable)]
pub enum LintSource {

View File

@ -1,7 +1,7 @@
//! Contains infrastructure for configuring the compiler, including parsing
//! command-line options.
use crate::lint;
use rustc_session::lint;
use crate::middle::cstore;
use crate::session::{early_error, early_warn, Session};
use crate::session::search_paths::SearchPath;
@ -2854,7 +2854,7 @@ impl PpMode {
/// we have an opt-in scheme here, so one is hopefully forced to think about
/// how the hash should be calculated when adding a new command-line argument.
mod dep_tracking {
use crate::lint;
use rustc_session::lint;
use crate::middle::cstore;
use std::collections::BTreeMap;
use std::hash::Hash;

View File

@ -828,7 +828,7 @@ Available lint options:
fn sort_lints(sess: &Session, mut lints: Vec<&'static Lint>) -> Vec<&'static Lint> {
// The sort doesn't case-fold but it's doubtful we care.
lints.sort_by_cached_key(|x: &&Lint| (x.default_level(sess), x.name));
lints.sort_by_cached_key(|x: &&Lint| (x.default_level(sess.edition()), x.name));
lints
}

View File

@ -439,8 +439,7 @@ fn configure_and_expand_inner<'a>(
sess.parse_sess.buffered_lints.with_lock(|buffered_lints| {
info!("{} parse sess buffered_lints", buffered_lints.len());
for BufferedEarlyLint{id, span, msg, lint_id} in buffered_lints.drain(..) {
let lint = lint::Lint::from_parser_lint_id(lint_id);
resolver.lint_buffer().buffer_lint(lint, id, span, &msg);
resolver.lint_buffer().buffer_lint(lint_id, id, span, &msg);
}
});

View File

@ -18,3 +18,4 @@ rustc_data_structures = { path = "../librustc_data_structures" }
rustc_feature = { path = "../librustc_feature" }
rustc_index = { path = "../librustc_index" }
rustc_error_codes = { path = "../librustc_error_codes" }
rustc_session = { path = "../librustc_session" }

View File

@ -21,6 +21,8 @@
#[macro_use]
extern crate rustc;
#[macro_use]
extern crate rustc_session;
mod array_into_iter;
mod nonstandard_style;

View File

@ -4,7 +4,7 @@ use errors::{PResult, Applicability};
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
use syntax::ast::{self, Attribute, AttrKind, Ident, MacArgs, MetaItem, MetaItemKind};
use syntax::attr::mk_name_value_item_str;
use syntax::early_buffered_lints::BufferedEarlyLintId;
use syntax::early_buffered_lints::ILL_FORMED_ATTRIBUTE_INPUT;
use syntax::sess::ParseSess;
use syntax_pos::{Symbol, sym};
@ -93,7 +93,7 @@ pub fn check_builtin_attribute(
}
if should_warn(name) {
sess.buffer_lint(
BufferedEarlyLintId::IllFormedAttributeInput,
&ILL_FORMED_ATTRIBUTE_INPUT,
meta.span,
ast::CRATE_NODE_ID,
&msg,

View File

@ -1,3 +1,4 @@
pub mod cgu_reuse_tracker;
pub mod utils;
#[macro_use]
pub mod lint;

View File

@ -1,5 +1,6 @@
use syntax_pos::{Symbol, sym};
use syntax_pos::edition::Edition;
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHasher};
pub use self::Level::*;
/// Setting for how to handle a lint.
@ -114,3 +115,124 @@ impl Lint {
.unwrap_or(self.default_level)
}
}
/// Identifies a lint known to the compiler.
#[derive(Clone, Copy, Debug)]
pub struct LintId {
// Identity is based on pointer equality of this field.
pub lint: &'static Lint,
}
impl PartialEq for LintId {
fn eq(&self, other: &LintId) -> bool {
std::ptr::eq(self.lint, other.lint)
}
}
impl Eq for LintId { }
impl std::hash::Hash for LintId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let ptr = self.lint as *const Lint;
ptr.hash(state);
}
}
impl LintId {
/// Gets the `LintId` for a `Lint`.
pub fn of(lint: &'static Lint) -> LintId {
LintId {
lint,
}
}
pub fn lint_name_raw(&self) -> &'static str {
self.lint.name
}
/// Gets the name of the lint.
pub fn to_string(&self) -> String {
self.lint.name_lower()
}
}
impl<HCX> HashStable<HCX> for LintId {
#[inline]
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
self.lint_name_raw().hash_stable(hcx, hasher);
}
}
impl<HCX> ToStableHashKey<HCX> for LintId {
type KeyType = &'static str;
#[inline]
fn to_stable_hash_key(&self, _: &HCX) -> &'static str {
self.lint_name_raw()
}
}
/// Declares a static item of type `&'static Lint`.
#[macro_export]
macro_rules! declare_lint {
($vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
$crate::declare_lint!(
$vis $NAME, $Level, $desc,
);
);
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
$(@future_incompatible = $fi:expr;)? $($v:ident),*) => (
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
name: stringify!($NAME),
default_level: $crate::lint::$Level,
desc: $desc,
edition_lint_opts: None,
is_plugin: false,
$($v: true,)*
$(future_incompatible: Some($fi),)*
..$crate::lint::Lint::default_fields_for_macro()
};
);
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
$lint_edition: expr => $edition_level: ident
) => (
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
name: stringify!($NAME),
default_level: $crate::lint::$Level,
desc: $desc,
edition_lint_opts: Some(($lint_edition, $crate::lint::Level::$edition_level)),
report_in_external_macro: false,
is_plugin: false,
};
);
}
#[macro_export]
macro_rules! declare_tool_lint {
(
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level: ident, $desc: expr
) => (
$crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, false}
);
(
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
report_in_external_macro: $rep:expr
) => (
$crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, $rep}
);
(
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
$external:expr
) => (
$(#[$attr])*
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
name: &concat!(stringify!($tool), "::", stringify!($NAME)),
default_level: $crate::lint::$Level,
desc: $desc,
edition_lint_opts: None,
report_in_external_macro: $external,
future_incompatible: None,
is_plugin: true,
};
);
}

View File

@ -24,3 +24,4 @@ rustc_lexer = { path = "../librustc_lexer" }
rustc_macros = { path = "../librustc_macros" }
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
rustc_error_codes = { path = "../librustc_error_codes" }
rustc_session = { path = "../librustc_session" }

View File

@ -5,13 +5,30 @@
use crate::ast::NodeId;
use syntax_pos::MultiSpan;
use rustc_session::lint::FutureIncompatibleInfo;
use rustc_session::declare_lint;
pub use rustc_session::lint::BufferedEarlyLint;
/// Since we cannot import `LintId`s from `rustc::lint`, we define some Ids here which can later be
/// passed to `rustc::lint::Lint::from_parser_lint_id` to get a `rustc::lint::Lint`.
pub enum BufferedEarlyLintId {
IllFormedAttributeInput,
MetaVariableMisuse,
IncompleteInclude,
declare_lint! {
pub ILL_FORMED_ATTRIBUTE_INPUT,
Deny,
"ill-formed attribute inputs that were previously accepted and used in practice",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
edition: None,
};
}
declare_lint! {
pub META_VARIABLE_MISUSE,
Allow,
"possible meta-variable misuse at macro definition"
}
declare_lint! {
pub INCOMPLETE_INCLUDE,
Deny,
"trailing content in included file"
}
/// Stores buffered lint info which can later be passed to `librustc`.
@ -26,5 +43,5 @@ pub struct BufferedEarlyLint {
pub id: NodeId,
/// A lint Id that can be passed to `rustc::lint::Lint::from_parser_lint_id`.
pub lint_id: BufferedEarlyLintId,
pub lint_id: &'static rustc_session::lint::Lint,
}

View File

@ -2,7 +2,7 @@
//! It also serves as an input to the parser itself.
use crate::ast::{CrateConfig, NodeId};
use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
use crate::early_buffered_lints::BufferedEarlyLint;
use errors::{Applicability, emitter::SilentEmitter, Handler, ColorConfig, DiagnosticBuilder};
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
@ -137,7 +137,7 @@ impl ParseSess {
pub fn buffer_lint(
&self,
lint_id: BufferedEarlyLintId,
lint_id: &'static rustc_session::lint::Lint,
span: impl Into<MultiSpan>,
id: NodeId,
msg: &str,

View File

@ -107,7 +107,7 @@
use crate::mbe::{KleeneToken, TokenTree};
use syntax::ast::NodeId;
use syntax::early_buffered_lints::BufferedEarlyLintId;
use syntax::early_buffered_lints::META_VARIABLE_MISUSE;
use syntax::token::{DelimToken, Token, TokenKind};
use syntax::sess::ParseSess;
use syntax::symbol::{kw, sym};
@ -623,5 +623,5 @@ fn ops_is_prefix(
}
fn buffer_lint(sess: &ParseSess, span: MultiSpan, node_id: NodeId, message: &str) {
sess.buffer_lint(BufferedEarlyLintId::MetaVariableMisuse, span, node_id, message);
sess.buffer_lint(&META_VARIABLE_MISUSE, span, node_id, message);
}

View File

@ -5,7 +5,7 @@ use syntax::ptr::P;
use syntax::symbol::Symbol;
use syntax::token;
use syntax::tokenstream::TokenStream;
use syntax::early_buffered_lints::BufferedEarlyLintId;
use syntax::early_buffered_lints::INCOMPLETE_INCLUDE;
use syntax_expand::panictry;
use syntax_expand::base::{self, *};
@ -101,7 +101,7 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: TokenStream)
let r = panictry!(self.p.parse_expr());
if self.p.token != token::Eof {
self.p.sess.buffer_lint(
BufferedEarlyLintId::IncompleteInclude,
&INCOMPLETE_INCLUDE,
self.p.token.span,
ast::CRATE_NODE_ID,
"include macro expected single expression in source",