1
Fork 0

mv compiler to compiler/

This commit is contained in:
mark 2020-08-27 22:58:48 -05:00 committed by Vadim Petrochenkov
parent db534b3ac2
commit 9e5f7d5631
1686 changed files with 941 additions and 1051 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,328 @@
//
// Unused import checking
//
// Although this is mostly a lint pass, it lives in here because it depends on
// resolve data structures and because it finalises the privacy information for
// `use` items.
//
// Unused trait imports can't be checked until the method resolution. We save
// candidates here, and do the actual check in librustc_typeck/check_unused.rs.
//
// Checking for unused imports is split into three steps:
//
// - `UnusedImportCheckVisitor` walks the AST to find all the unused imports
// inside of `UseTree`s, recording their `NodeId`s and grouping them by
// the parent `use` item
//
// - `calc_unused_spans` then walks over all the `use` items marked in the
// previous step to collect the spans associated with the `NodeId`s and to
// calculate the spans that can be removed by rustfix; This is done in a
// separate step to be able to collapse the adjacent spans that rustfix
// will remove
//
// - `check_crate` finally emits the diagnostics based on the data generated
// in the last step
use crate::imports::ImportKind;
use crate::Resolver;
use rustc_ast as ast;
use rustc_ast::node_id::NodeMap;
use rustc_ast::visit::{self, Visitor};
use rustc_ast_lowering::ResolverAstLowering;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::pluralize;
use rustc_middle::ty;
use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_IMPORTS};
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_span::{MultiSpan, Span, DUMMY_SP};
struct UnusedImport<'a> {
use_tree: &'a ast::UseTree,
use_tree_id: ast::NodeId,
item_span: Span,
unused: FxHashSet<ast::NodeId>,
}
impl<'a> UnusedImport<'a> {
fn add(&mut self, id: ast::NodeId) {
self.unused.insert(id);
}
}
struct UnusedImportCheckVisitor<'a, 'b> {
r: &'a mut Resolver<'b>,
/// All the (so far) unused imports, grouped path list
unused_imports: NodeMap<UnusedImport<'a>>,
base_use_tree: Option<&'a ast::UseTree>,
base_id: ast::NodeId,
item_span: Span,
}
impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> {
// We have information about whether `use` (import) items are actually
// used now. If an import is not used at all, we signal a lint error.
fn check_import(&mut self, id: ast::NodeId) {
let mut used = false;
self.r.per_ns(|this, ns| used |= this.used_imports.contains(&(id, ns)));
let def_id = self.r.local_def_id(id);
if !used {
if self.r.maybe_unused_trait_imports.contains(&def_id) {
// Check later.
return;
}
self.unused_import(self.base_id).add(id);
} else {
// This trait import is definitely used, in a way other than
// method resolution.
self.r.maybe_unused_trait_imports.remove(&def_id);
if let Some(i) = self.unused_imports.get_mut(&self.base_id) {
i.unused.remove(&id);
}
}
}
fn unused_import(&mut self, id: ast::NodeId) -> &mut UnusedImport<'a> {
let use_tree_id = self.base_id;
let use_tree = self.base_use_tree.unwrap();
let item_span = self.item_span;
self.unused_imports.entry(id).or_insert_with(|| UnusedImport {
use_tree,
use_tree_id,
item_span,
unused: FxHashSet::default(),
})
}
}
impl<'a, 'b> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b> {
fn visit_item(&mut self, item: &'a ast::Item) {
self.item_span = item.span;
// Ignore is_public import statements because there's no way to be sure
// whether they're used or not. Also ignore imports with a dummy span
// because this means that they were generated in some fashion by the
// compiler and we don't need to consider them.
if let ast::ItemKind::Use(..) = item.kind {
if item.vis.node.is_pub() || item.span.is_dummy() {
return;
}
}
visit::walk_item(self, item);
}
fn visit_use_tree(&mut self, use_tree: &'a ast::UseTree, id: ast::NodeId, nested: bool) {
// Use the base UseTree's NodeId as the item id
// This allows the grouping of all the lints in the same item
if !nested {
self.base_id = id;
self.base_use_tree = Some(use_tree);
}
if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
if items.is_empty() {
self.unused_import(self.base_id).add(id);
}
} else {
self.check_import(id);
}
visit::walk_use_tree(self, use_tree, id);
}
}
enum UnusedSpanResult {
Used,
FlatUnused(Span, Span),
NestedFullUnused(Vec<Span>, Span),
NestedPartialUnused(Vec<Span>, Vec<Span>),
}
fn calc_unused_spans(
unused_import: &UnusedImport<'_>,
use_tree: &ast::UseTree,
use_tree_id: ast::NodeId,
) -> UnusedSpanResult {
// The full span is the whole item's span if this current tree is not nested inside another
// This tells rustfix to remove the whole item if all the imports are unused
let full_span = if unused_import.use_tree.span == use_tree.span {
unused_import.item_span
} else {
use_tree.span
};
match use_tree.kind {
ast::UseTreeKind::Simple(..) | ast::UseTreeKind::Glob => {
if unused_import.unused.contains(&use_tree_id) {
UnusedSpanResult::FlatUnused(use_tree.span, full_span)
} else {
UnusedSpanResult::Used
}
}
ast::UseTreeKind::Nested(ref nested) => {
if nested.is_empty() {
return UnusedSpanResult::FlatUnused(use_tree.span, full_span);
}
let mut unused_spans = Vec::new();
let mut to_remove = Vec::new();
let mut all_nested_unused = true;
let mut previous_unused = false;
for (pos, (use_tree, use_tree_id)) in nested.iter().enumerate() {
let remove = match calc_unused_spans(unused_import, use_tree, *use_tree_id) {
UnusedSpanResult::Used => {
all_nested_unused = false;
None
}
UnusedSpanResult::FlatUnused(span, remove) => {
unused_spans.push(span);
Some(remove)
}
UnusedSpanResult::NestedFullUnused(mut spans, remove) => {
unused_spans.append(&mut spans);
Some(remove)
}
UnusedSpanResult::NestedPartialUnused(mut spans, mut to_remove_extra) => {
all_nested_unused = false;
unused_spans.append(&mut spans);
to_remove.append(&mut to_remove_extra);
None
}
};
if let Some(remove) = remove {
let remove_span = if nested.len() == 1 {
remove
} else if pos == nested.len() - 1 || !all_nested_unused {
// Delete everything from the end of the last import, to delete the
// previous comma
nested[pos - 1].0.span.shrink_to_hi().to(use_tree.span)
} else {
// Delete everything until the next import, to delete the trailing commas
use_tree.span.to(nested[pos + 1].0.span.shrink_to_lo())
};
// Try to collapse adjacent spans into a single one. This prevents all cases of
// overlapping removals, which are not supported by rustfix
if previous_unused && !to_remove.is_empty() {
let previous = to_remove.pop().unwrap();
to_remove.push(previous.to(remove_span));
} else {
to_remove.push(remove_span);
}
}
previous_unused = remove.is_some();
}
if unused_spans.is_empty() {
UnusedSpanResult::Used
} else if all_nested_unused {
UnusedSpanResult::NestedFullUnused(unused_spans, full_span)
} else {
UnusedSpanResult::NestedPartialUnused(unused_spans, to_remove)
}
}
}
}
impl Resolver<'_> {
crate fn check_unused(&mut self, krate: &ast::Crate) {
for import in self.potentially_unused_imports.iter() {
match import.kind {
_ if import.used.get()
|| import.vis.get() == ty::Visibility::Public
|| import.span.is_dummy() =>
{
if let ImportKind::MacroUse = import.kind {
if !import.span.is_dummy() {
self.lint_buffer.buffer_lint(
MACRO_USE_EXTERN_CRATE,
import.id,
import.span,
"deprecated `#[macro_use]` attribute used to \
import macros should be replaced at use sites \
with a `use` item to import the macro \
instead",
);
}
}
}
ImportKind::ExternCrate { .. } => {
let def_id = self.local_def_id(import.id);
self.maybe_unused_extern_crates.push((def_id, import.span));
}
ImportKind::MacroUse => {
let msg = "unused `#[macro_use]` import";
self.lint_buffer.buffer_lint(UNUSED_IMPORTS, import.id, import.span, msg);
}
_ => {}
}
}
let mut visitor = UnusedImportCheckVisitor {
r: self,
unused_imports: Default::default(),
base_use_tree: None,
base_id: ast::DUMMY_NODE_ID,
item_span: DUMMY_SP,
};
visit::walk_crate(&mut visitor, krate);
for unused in visitor.unused_imports.values() {
let mut fixes = Vec::new();
let mut spans = match calc_unused_spans(unused, unused.use_tree, unused.use_tree_id) {
UnusedSpanResult::Used => continue,
UnusedSpanResult::FlatUnused(span, remove) => {
fixes.push((remove, String::new()));
vec![span]
}
UnusedSpanResult::NestedFullUnused(spans, remove) => {
fixes.push((remove, String::new()));
spans
}
UnusedSpanResult::NestedPartialUnused(spans, remove) => {
for fix in &remove {
fixes.push((*fix, String::new()));
}
spans
}
};
let len = spans.len();
spans.sort();
let ms = MultiSpan::from_spans(spans.clone());
let mut span_snippets = spans
.iter()
.filter_map(|s| match visitor.r.session.source_map().span_to_snippet(*s) {
Ok(s) => Some(format!("`{}`", s)),
_ => None,
})
.collect::<Vec<String>>();
span_snippets.sort();
let msg = format!(
"unused import{}{}",
pluralize!(len),
if !span_snippets.is_empty() {
format!(": {}", span_snippets.join(", "))
} else {
String::new()
}
);
let fix_msg = if fixes.len() == 1 && fixes[0].0 == unused.item_span {
"remove the whole `use` item"
} else if spans.len() > 1 {
"remove the unused imports"
} else {
"remove the unused import"
};
visitor.r.lint_buffer.buffer_lint_with_diagnostic(
UNUSED_IMPORTS,
unused.use_tree_id,
ms,
&msg,
BuiltinLintDiagnostics::UnusedImports(fix_msg.into(), fixes),
);
}
}
}

View file

@ -0,0 +1,293 @@
use crate::Resolver;
use rustc_ast::token::{self, Token};
use rustc_ast::visit::{self, FnKind};
use rustc_ast::walk_list;
use rustc_ast::*;
use rustc_ast_lowering::ResolverAstLowering;
use rustc_expand::expand::AstFragment;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::definitions::*;
use rustc_span::hygiene::ExpnId;
use rustc_span::symbol::{kw, sym};
use rustc_span::Span;
use tracing::debug;
crate fn collect_definitions(
resolver: &mut Resolver<'_>,
fragment: &AstFragment,
expansion: ExpnId,
) {
let parent_def = resolver.invocation_parents[&expansion];
fragment.visit_with(&mut DefCollector { resolver, parent_def, expansion });
}
/// Creates `DefId`s for nodes in the AST.
struct DefCollector<'a, 'b> {
resolver: &'a mut Resolver<'b>,
parent_def: LocalDefId,
expansion: ExpnId,
}
impl<'a, 'b> DefCollector<'a, 'b> {
fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> LocalDefId {
let parent_def = self.parent_def;
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
self.resolver.create_def(parent_def, node_id, data, self.expansion, span)
}
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def);
f(self);
self.parent_def = orig_parent_def;
}
fn collect_field(&mut self, field: &'a StructField, index: Option<usize>) {
let index = |this: &Self| {
index.unwrap_or_else(|| {
let node_id = NodeId::placeholder_from_expn_id(this.expansion);
this.resolver.placeholder_field_indices[&node_id]
})
};
if field.is_placeholder {
let old_index = self.resolver.placeholder_field_indices.insert(field.id, index(self));
assert!(old_index.is_none(), "placeholder field index is reset for a node ID");
self.visit_macro_invoc(field.id);
} else {
let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name);
let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span);
self.with_parent(def, |this| visit::walk_struct_field(this, field));
}
}
fn visit_macro_invoc(&mut self, id: NodeId) {
let old_parent =
self.resolver.invocation_parents.insert(id.placeholder_to_expn_id(), self.parent_def);
assert!(old_parent.is_none(), "parent `LocalDefId` is reset for an invocation");
}
}
impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
fn visit_item(&mut self, i: &'a Item) {
debug!("visit_item: {:?}", i);
// Pick the def data. This need not be unique, but the more
// information we encapsulate into, the better
let def_data = match &i.kind {
ItemKind::Impl { .. } => DefPathData::Impl,
ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
return visit::walk_item(self, i);
}
ItemKind::Mod(..)
| ItemKind::Trait(..)
| ItemKind::TraitAlias(..)
| ItemKind::Enum(..)
| ItemKind::Struct(..)
| ItemKind::Union(..)
| ItemKind::ExternCrate(..)
| ItemKind::ForeignMod(..)
| ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) => {
DefPathData::ValueNs(i.ident.name)
}
ItemKind::MacroDef(..) => DefPathData::MacroNs(i.ident.name),
ItemKind::MacCall(..) => return self.visit_macro_invoc(i.id),
ItemKind::GlobalAsm(..) => DefPathData::Misc,
ItemKind::Use(..) => {
return visit::walk_item(self, i);
}
};
let def = self.create_def(i.id, def_data, i.span);
self.with_parent(def, |this| {
match i.kind {
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
// If this is a unit or tuple-like struct, register the constructor.
if let Some(ctor_hir_id) = struct_def.ctor_id() {
this.create_def(ctor_hir_id, DefPathData::Ctor, i.span);
}
}
_ => {}
}
visit::walk_item(this, i);
});
}
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
if let FnKind::Fn(_, _, sig, _, body) = fn_kind {
if let Async::Yes { closure_id, return_impl_trait_id, .. } = sig.header.asyncness {
self.create_def(return_impl_trait_id, DefPathData::ImplTrait, span);
// For async functions, we need to create their inner defs inside of a
// closure to match their desugared representation. Besides that,
// we must mirror everything that `visit::walk_fn` below does.
self.visit_fn_header(&sig.header);
visit::walk_fn_decl(self, &sig.decl);
let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
self.with_parent(closure_def, |this| walk_list!(this, visit_block, body));
return;
}
}
visit::walk_fn(self, fn_kind, span);
}
fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
self.create_def(id, DefPathData::Misc, use_tree.span);
visit::walk_use_tree(self, use_tree, id);
}
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
if let ForeignItemKind::MacCall(_) = foreign_item.kind {
return self.visit_macro_invoc(foreign_item.id);
}
let def = self.create_def(
foreign_item.id,
DefPathData::ValueNs(foreign_item.ident.name),
foreign_item.span,
);
self.with_parent(def, |this| {
visit::walk_foreign_item(this, foreign_item);
});
}
fn visit_variant(&mut self, v: &'a Variant) {
if v.is_placeholder {
return self.visit_macro_invoc(v.id);
}
let def = self.create_def(v.id, DefPathData::TypeNs(v.ident.name), v.span);
self.with_parent(def, |this| {
if let Some(ctor_hir_id) = v.data.ctor_id() {
this.create_def(ctor_hir_id, DefPathData::Ctor, v.span);
}
visit::walk_variant(this, v)
});
}
fn visit_variant_data(&mut self, data: &'a VariantData) {
// The assumption here is that non-`cfg` macro expansion cannot change field indices.
// It currently holds because only inert attributes are accepted on fields,
// and every such attribute expands into a single field after it's resolved.
for (index, field) in data.fields().iter().enumerate() {
self.collect_field(field, Some(index));
}
}
fn visit_generic_param(&mut self, param: &'a GenericParam) {
if param.is_placeholder {
self.visit_macro_invoc(param.id);
return;
}
let name = param.ident.name;
let def_path_data = match param.kind {
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name),
GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
};
self.create_def(param.id, def_path_data, param.ident.span);
visit::walk_generic_param(self, param);
}
fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
let def_data = match &i.kind {
AssocItemKind::Fn(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(i.ident.name),
AssocItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
AssocItemKind::MacCall(..) => return self.visit_macro_invoc(i.id),
};
let def = self.create_def(i.id, def_data, i.span);
self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt));
}
fn visit_pat(&mut self, pat: &'a Pat) {
match pat.kind {
PatKind::MacCall(..) => self.visit_macro_invoc(pat.id),
_ => visit::walk_pat(self, pat),
}
}
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
let def = self.create_def(constant.id, DefPathData::AnonConst, constant.value.span);
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
}
fn visit_expr(&mut self, expr: &'a Expr) {
let parent_def = match expr.kind {
ExprKind::MacCall(..) => return self.visit_macro_invoc(expr.id),
ExprKind::Closure(_, asyncness, ..) => {
// Async closures desugar to closures inside of closures, so
// we must create two defs.
let closure_def = self.create_def(expr.id, DefPathData::ClosureExpr, expr.span);
match asyncness {
Async::Yes { closure_id, .. } => {
self.create_def(closure_id, DefPathData::ClosureExpr, expr.span)
}
Async::No => closure_def,
}
}
ExprKind::Async(_, async_id, _) => {
self.create_def(async_id, DefPathData::ClosureExpr, expr.span)
}
_ => self.parent_def,
};
self.with_parent(parent_def, |this| visit::walk_expr(this, expr));
}
fn visit_ty(&mut self, ty: &'a Ty) {
match ty.kind {
TyKind::MacCall(..) => return self.visit_macro_invoc(ty.id),
TyKind::ImplTrait(node_id, _) => {
self.create_def(node_id, DefPathData::ImplTrait, ty.span);
}
_ => {}
}
visit::walk_ty(self, ty);
}
fn visit_stmt(&mut self, stmt: &'a Stmt) {
match stmt.kind {
StmtKind::MacCall(..) => self.visit_macro_invoc(stmt.id),
_ => visit::walk_stmt(self, stmt),
}
}
fn visit_token(&mut self, t: Token) {
if let token::Interpolated(nt) = t.kind {
if let token::NtExpr(ref expr) = *nt {
if let ExprKind::MacCall(..) = expr.kind {
self.visit_macro_invoc(expr.id);
}
}
}
}
fn visit_arm(&mut self, arm: &'a Arm) {
if arm.is_placeholder { self.visit_macro_invoc(arm.id) } else { visit::walk_arm(self, arm) }
}
fn visit_field(&mut self, f: &'a Field) {
if f.is_placeholder { self.visit_macro_invoc(f.id) } else { visit::walk_field(self, f) }
}
fn visit_field_pattern(&mut self, fp: &'a FieldPat) {
if fp.is_placeholder {
self.visit_macro_invoc(fp.id)
} else {
visit::walk_field_pattern(self, fp)
}
}
fn visit_param(&mut self, p: &'a Param) {
if p.is_placeholder { self.visit_macro_invoc(p.id) } else { visit::walk_param(self, p) }
}
// This method is called only when we are visiting an individual field
// after expanding an attribute on it.
fn visit_struct_field(&mut self, field: &'a StructField) {
self.collect_field(field, None);
}
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff