Skip to content
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

Move more of rustc::lint into rustc_lint #68045

Merged
merged 14 commits into from
Jan 12, 2020
2 changes: 1 addition & 1 deletion src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! item.

use crate::hir::map::Map;
use crate::lint::builtin::UNUSED_ATTRIBUTES;
use crate::ty::query::Providers;
use crate::ty::TyCtxt;

Expand All @@ -16,6 +15,7 @@ use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::DUMMY_HIR_ID;
use rustc_hir::{self, HirId, Item, ItemKind, TraitItem, TraitItemKind};
use rustc_session::lint::builtin::UNUSED_ATTRIBUTES;
use rustc_span::symbol::sym;
use rustc_span::Span;
use syntax::ast::Attribute;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

use self::TargetLint::*;

use crate::hir::map::{definitions::DisambiguatedDefPathData, DefPathData};
use crate::hir::map::definitions::{DefPathData, DisambiguatedDefPathData};
use crate::lint::levels::{LintLevelSets, LintLevelsBuilder};
use crate::lint::{EarlyLintPassObject, LateLintPassObject};
use crate::middle::privacy::AccessLevels;
use crate::middle::stability;
use crate::session::Session;
use crate::ty::layout::{LayoutError, LayoutOf, TyLayout};
use crate::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
use rustc_data_structures::fx::FxHashMap;
Expand All @@ -32,6 +31,7 @@ use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
use rustc_session::Session;
use rustc_span::{symbol::Symbol, MultiSpan, Span, DUMMY_SP};
use syntax::ast;
use syntax::util::lev_distance::find_best_match_for_name;
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/lint/levels.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::cmp;

use crate::ich::StableHashingContext;
use crate::lint::builtin;
use crate::lint::context::{CheckLintNameResult, LintStore};
use crate::lint::{self, Level, Lint, LintId, LintSource};
use crate::session::Session;
use crate::lint::{self, LintSource};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir::HirId;
use rustc_session::lint::{builtin, Level, Lint, LintId};
use rustc_session::Session;
use rustc_span::source_map::MultiSpan;
use rustc_span::symbol::{sym, Symbol};
use syntax::ast;
Expand Down
8 changes: 0 additions & 8 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,3 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
ExpnKind::Macro(..) => true, // definitely a plugin
}
}

/// Returns `true` if `span` originates in a derive-macro's expansion.
pub fn in_derive_expansion(span: Span) -> bool {
if let ExpnKind::Macro(MacroKind::Derive, _) = span.ctxt().outer_expn_data().kind {
return true;
}
false
}
7 changes: 3 additions & 4 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

pub use self::StabilityLevel::*;

use crate::lint::{self, in_derive_expansion, Lint};
use crate::session::{DiagnosticMessageId, Session};
use crate::ty::{self, TyCtxt};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
Expand All @@ -13,7 +12,7 @@ use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
use rustc_hir::{self, HirId};
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
use rustc_session::lint::{self, BuiltinLintDiagnostics, Lint, LintBuffer};
use rustc_span::symbol::{sym, Symbol};
use rustc_span::{MultiSpan, Span};
use syntax::ast::CRATE_NODE_ID;
Expand Down Expand Up @@ -201,7 +200,7 @@ pub fn early_report_deprecation(
lint: &'static Lint,
span: Span,
) {
if in_derive_expansion(span) {
if span.in_derive_expansion() {
return;
}

Expand All @@ -218,7 +217,7 @@ fn late_report_deprecation(
def_id: DefId,
hir_id: HirId,
) {
if in_derive_expansion(span) {
if span.in_derive_expansion() {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ rustc_queries! {
desc { "looking up the native libraries of a linked crate" }
}

query lint_levels(_: CrateNum) -> &'tcx lint::LintLevelMap {
query lint_levels(_: CrateNum) -> &'tcx LintLevelMap {
eval_always
desc { "computing the lint levels for items in this crate" }
}
Expand Down
14 changes: 8 additions & 6 deletions src/librustc/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@

use super::elaborate_predicates;

use crate::lint;
use crate::traits::{self, Obligation, ObligationCause};
use crate::ty::subst::{InternalSubsts, Subst};
use crate::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY;
use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP};
use syntax::ast;

use std::borrow::Cow;
use std::iter::{self};
use syntax::ast::{self};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum ObjectSafetyViolation {
Expand Down Expand Up @@ -178,16 +179,17 @@ fn object_safety_violations_for_trait(
{
// Using `CRATE_NODE_ID` is wrong, but it's hard to get a more precise id.
// It's also hard to get a use site span, so we use the method definition span.
tcx.lint_node_note(
lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY,
tcx.struct_span_lint_hir(
WHERE_CLAUSES_OBJECT_SAFETY,
hir::CRATE_HIR_ID,
*span,
&format!(
"the trait `{}` cannot be made into an object",
tcx.def_path_str(trait_def_id)
),
&violation.error_msg(),
);
)
.note(&violation.error_msg())
.emit();
false
} else {
true
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/traits/specialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
pub mod specialization_graph;

use crate::infer::{InferCtxt, InferOk};
use crate::lint;
use crate::traits::select::IntercrateAmbiguityCause;
use crate::traits::{self, coherence, FutureCompatOverlapErrorKind, ObligationCause, TraitEngine};
use crate::ty::subst::{InternalSubsts, Subst, SubstsRef};
use crate::ty::{self, TyCtxt, TypeFoldable};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_hir::def_id::DefId;
use rustc_session::lint::builtin::ORDER_DEPENDENT_TRAIT_OBJECTS;
use rustc_span::DUMMY_SP;

use super::util::impl_trait_ref_and_oblig;
Expand Down Expand Up @@ -342,7 +342,7 @@ pub(super) fn specialization_graph_provider(
unreachable!("converted to hard error above")
}
FutureCompatOverlapErrorKind::Issue33140 => {
lint::builtin::ORDER_DEPENDENT_TRAIT_OBJECTS
ORDER_DEPENDENT_TRAIT_OBJECTS
}
};
tcx.struct_span_lint_hir(
Expand Down
53 changes: 16 additions & 37 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::hir::map as hir_map;
use crate::hir::map::DefPathHash;
use crate::ich::{NodeIdHashingMode, StableHashingContext};
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
use crate::lint::{self, Lint};
use crate::lint::{maybe_lint_level_root, struct_lint_level, LintSource};
use crate::middle;
use crate::middle::cstore::CrateStoreDyn;
use crate::middle::cstore::EncodedMetadata;
Expand Down Expand Up @@ -61,6 +61,7 @@ use rustc_data_structures::sync::{Lock, Lrc, WorkerLocal};
use rustc_errors::DiagnosticBuilder;
use rustc_index::vec::{Idx, IndexVec};
use rustc_macros::HashStable;
use rustc_session::lint::{Level, Lint};
use rustc_session::node_id::NodeMap;
use rustc_span::source_map::MultiSpan;
use rustc_span::symbol::{kw, sym, Symbol};
Expand Down Expand Up @@ -946,7 +947,11 @@ pub struct GlobalCtxt<'tcx> {

pub sess: &'tcx Session,

pub lint_store: Lrc<lint::LintStore>,
/// This only ever stores a `LintStore` but we don't want a dependency on that type here.
///
/// FIXME(Centril): consider `dyn LintStoreMarker` once
/// we can upcast to `Any` for some additional type safety.
pub lint_store: Lrc<dyn Any>,

pub dep_graph: DepGraph,

Expand Down Expand Up @@ -1115,7 +1120,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// reference to the context, to allow formatting values that need it.
pub fn create_global_ctxt(
s: &'tcx Session,
lint_store: Lrc<lint::LintStore>,
lint_store: Lrc<dyn Any>,
local_providers: ty::query::Providers<'tcx>,
extern_providers: ty::query::Providers<'tcx>,
arenas: &'tcx AllArenas,
Expand Down Expand Up @@ -2551,42 +2556,16 @@ impl<'tcx> TyCtxt<'tcx> {
iter.intern_with(|xs| self.intern_goals(xs))
}

pub fn lint_hir<S: Into<MultiSpan>>(
pub fn lint_hir(
self,
lint: &'static Lint,
hir_id: HirId,
span: S,
span: impl Into<MultiSpan>,
msg: &str,
) {
self.struct_span_lint_hir(lint, hir_id, span.into(), msg).emit()
}

pub fn lint_hir_note<S: Into<MultiSpan>>(
self,
lint: &'static Lint,
hir_id: HirId,
span: S,
msg: &str,
note: &str,
) {
let mut err = self.struct_span_lint_hir(lint, hir_id, span.into(), msg);
err.note(note);
err.emit()
}

pub fn lint_node_note<S: Into<MultiSpan>>(
self,
lint: &'static Lint,
id: hir::HirId,
span: S,
msg: &str,
note: &str,
) {
let mut err = self.struct_span_lint_hir(lint, id, span.into(), msg);
err.note(note);
err.emit()
}

/// Walks upwards from `id` to find a node which might change lint levels with attributes.
/// It stops at `bound` and just returns it if reached.
pub fn maybe_lint_level_root_bounded(
Expand All @@ -2598,7 +2577,7 @@ impl<'tcx> TyCtxt<'tcx> {
if id == bound {
return bound;
}
if lint::maybe_lint_level_root(self, id) {
if maybe_lint_level_root(self, id) {
return id;
}
let next = self.hir().get_parent_node(id);
Expand All @@ -2613,7 +2592,7 @@ impl<'tcx> TyCtxt<'tcx> {
self,
lint: &'static Lint,
mut id: hir::HirId,
) -> (lint::Level, lint::LintSource) {
) -> (Level, LintSource) {
let sets = self.lint_levels(LOCAL_CRATE);
loop {
if let Some(pair) = sets.level_and_source(lint, id, self.sess) {
Expand All @@ -2627,15 +2606,15 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

pub fn struct_span_lint_hir<S: Into<MultiSpan>>(
pub fn struct_span_lint_hir(
self,
lint: &'static Lint,
hir_id: HirId,
span: S,
span: impl Into<MultiSpan>,
msg: &str,
) -> DiagnosticBuilder<'tcx> {
let (level, src) = self.lint_level_at_node(lint, hir_id);
lint::struct_lint_level(self.sess, lint, level, src, Some(span.into()), msg)
struct_lint_level(self.sess, lint, level, src, Some(span.into()), msg)
}

pub fn struct_lint_node(
Expand All @@ -2645,7 +2624,7 @@ impl<'tcx> TyCtxt<'tcx> {
msg: &str,
) -> DiagnosticBuilder<'tcx> {
let (level, src) = self.lint_level_at_node(lint, id);
lint::struct_lint_level(self.sess, lint, level, src, None, msg)
struct_lint_level(self.sess, lint, level, src, None, msg)
}

pub fn in_scope_traits(self, id: HirId) -> Option<&'tcx StableVec<TraitCandidate>> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/query/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::dep_graph::{self, DepNode};
use crate::hir::exports::Export;
use crate::infer::canonical::{self, Canonical};
use crate::lint;
use crate::lint::LintLevelMap;
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::cstore::{CrateSource, DepKind, NativeLibraryKind};
use crate::middle::cstore::{ExternCrate, ForeignModule, LinkagePreference, NativeLibrary};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast_passes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ path = "lib.rs"
[dependencies]
log = "0.4"
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_errors = { path = "../librustc_errors", package = "rustc_errors" }
rustc_errors = { path = "../librustc_errors" }
rustc_error_codes = { path = "../librustc_error_codes" }
rustc_feature = { path = "../librustc_feature" }
rustc_parse = { path = "../librustc_parse" }
Expand Down
19 changes: 12 additions & 7 deletions src/librustc_lint/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
//! for all lint attributes.

use rustc::hir::map::Map;
use rustc::lint::LateContext;
use rustc::lint::{LateLintPass, LateLintPassObject};
use rustc::lint::{LateContext, LateLintPass, LateLintPassObject, LintStore};
use rustc::ty::{self, TyCtxt};
use rustc_data_structures::sync::{join, par_iter, ParallelIterator};
use rustc_hir as hir;
Expand All @@ -31,6 +30,12 @@ use syntax::walk_list;
use log::debug;
use std::slice;

/// Extract the `LintStore` from the query context.
/// This function exists because we've erased `LintStore` as `dyn Any` in the context.
crate fn unerased_lint_store<'tcx>(tcx: TyCtxt<'tcx>) -> &'tcx LintStore {
tcx.lint_store.downcast_ref().unwrap()
}

macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
$cx.pass.$f(&$cx.context, $($args),*);
}) }
Expand Down Expand Up @@ -356,7 +361,7 @@ fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
tables: &ty::TypeckTables::empty(None),
param_env: ty::ParamEnv::empty(),
access_levels,
lint_store: &tcx.lint_store,
lint_store: unerased_lint_store(tcx),
last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id).unwrap(),
generics: None,
only_module: true,
Expand Down Expand Up @@ -386,7 +391,7 @@ pub fn late_lint_mod<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
late_lint_mod_pass(tcx, module_def_id, builtin_lints);

let mut passes: Vec<_> =
tcx.lint_store.late_module_passes.iter().map(|pass| (pass)()).collect();
unerased_lint_store(tcx).late_module_passes.iter().map(|pass| (pass)()).collect();

if !passes.is_empty() {
late_lint_mod_pass(tcx, module_def_id, LateLintPassObjects { lints: &mut passes[..] });
Expand All @@ -403,7 +408,7 @@ fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tc
tables: &ty::TypeckTables::empty(None),
param_env: ty::ParamEnv::empty(),
access_levels,
lint_store: &tcx.lint_store,
lint_store: unerased_lint_store(tcx),
last_node_with_lint_attrs: hir::CRATE_HIR_ID,
generics: None,
only_module: false,
Expand All @@ -424,7 +429,7 @@ fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tc
}

fn late_lint_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, builtin_lints: T) {
let mut passes = tcx.lint_store.late_passes.iter().map(|p| (p)()).collect::<Vec<_>>();
let mut passes = unerased_lint_store(tcx).late_passes.iter().map(|p| (p)()).collect::<Vec<_>>();

if !tcx.sess.opts.debugging_opts.no_interleave_lints {
if !passes.is_empty() {
Expand All @@ -443,7 +448,7 @@ fn late_lint_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, b
}

let mut passes: Vec<_> =
tcx.lint_store.late_module_passes.iter().map(|pass| (pass)()).collect();
unerased_lint_store(tcx).late_module_passes.iter().map(|pass| (pass)()).collect();

for pass in &mut passes {
tcx.sess
Expand Down
Loading