From c796b1f46aba25880942a461c3d1079250b9c8ac Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 6 Dec 2018 13:49:26 +0100 Subject: [PATCH 01/22] Add tests for internal lints --- .../internal-lints/default_hash_types.rs | 34 +++ .../internal-lints/default_hash_types.stderr | 56 +++++ .../internal-lints/ty_tykind_usage.rs | 59 ++++++ .../internal-lints/ty_tykind_usage.stderr | 196 ++++++++++++++++++ 4 files changed, 345 insertions(+) create mode 100644 src/test/ui-fulldeps/internal-lints/default_hash_types.rs create mode 100644 src/test/ui-fulldeps/internal-lints/default_hash_types.stderr create mode 100644 src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs create mode 100644 src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr diff --git a/src/test/ui-fulldeps/internal-lints/default_hash_types.rs b/src/test/ui-fulldeps/internal-lints/default_hash_types.rs new file mode 100644 index 0000000000000..6d32744145a8a --- /dev/null +++ b/src/test/ui-fulldeps/internal-lints/default_hash_types.rs @@ -0,0 +1,34 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -Z internal-lints + +#![feature(rustc_private)] + +extern crate rustc_data_structures; + +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; +use std::collections::{HashMap, HashSet}; +//~^ WARNING Prefer FxHashMap over HashMap, it has better performance +//~^^ WARNING Prefer FxHashSet over HashSet, it has better performance + +#[deny(default_hash_types)] +fn main() { + let _map: HashMap = HashMap::default(); + //~^ ERROR Prefer FxHashMap over HashMap, it has better performance + //~^^ ERROR Prefer FxHashMap over HashMap, it has better performance + let _set: HashSet = HashSet::default(); + //~^ ERROR Prefer FxHashSet over HashSet, it has better performance + //~^^ ERROR Prefer FxHashSet over HashSet, it has better performance + + // test that the lint doesn't also match the Fx variants themselves + let _fx_map: FxHashMap = FxHashMap::default(); + let _fx_set: FxHashSet = FxHashSet::default(); +} diff --git a/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr b/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr new file mode 100644 index 0000000000000..4f40c712aec4d --- /dev/null +++ b/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr @@ -0,0 +1,56 @@ +warning: Prefer FxHashMap over HashMap, it has better performance + --> $DIR/default_hash_types.rs:18:24 + | +LL | use std::collections::{HashMap, HashSet}; + | ^^^^^^^ help: use: `FxHashMap` + | + = note: #[warn(default_hash_types)] on by default + = note: a `use rustc_data_structures::fx::FxHashMap` may be necessary + +warning: Prefer FxHashSet over HashSet, it has better performance + --> $DIR/default_hash_types.rs:18:33 + | +LL | use std::collections::{HashMap, HashSet}; + | ^^^^^^^ help: use: `FxHashSet` + | + = note: a `use rustc_data_structures::fx::FxHashSet` may be necessary + +error: Prefer FxHashMap over HashMap, it has better performance + --> $DIR/default_hash_types.rs:24:15 + | +LL | let _map: HashMap = HashMap::default(); + | ^^^^^^^ help: use: `FxHashMap` + | +note: lint level defined here + --> $DIR/default_hash_types.rs:22:8 + | +LL | #[deny(default_hash_types)] + | ^^^^^^^^^^^^^^^^^^ + = note: a `use rustc_data_structures::fx::FxHashMap` may be necessary + +error: Prefer FxHashMap over HashMap, it has better performance + --> $DIR/default_hash_types.rs:24:41 + | +LL | let _map: HashMap = HashMap::default(); + | ^^^^^^^ help: use: `FxHashMap` + | + = note: a `use rustc_data_structures::fx::FxHashMap` may be necessary + +error: Prefer FxHashSet over HashSet, it has better performance + --> $DIR/default_hash_types.rs:27:15 + | +LL | let _set: HashSet = HashSet::default(); + | ^^^^^^^ help: use: `FxHashSet` + | + = note: a `use rustc_data_structures::fx::FxHashSet` may be necessary + +error: Prefer FxHashSet over HashSet, it has better performance + --> $DIR/default_hash_types.rs:27:33 + | +LL | let _set: HashSet = HashSet::default(); + | ^^^^^^^ help: use: `FxHashSet` + | + = note: a `use rustc_data_structures::fx::FxHashSet` may be necessary + +error: aborting due to 4 previous errors + diff --git a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs new file mode 100644 index 0000000000000..9962d9c6bcbbb --- /dev/null +++ b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs @@ -0,0 +1,59 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -Z internal-lints + +#![feature(rustc_private)] + +extern crate rustc; + +use rustc::ty::{self, Ty, TyKind}; + +#[deny(usage_of_ty_tykind)] +fn main() { + let sty = TyKind::Bool; //~ ERROR usage of `ty::TyKind::` + + match sty { + TyKind::Bool => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Char => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Int(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Uint(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Float(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Adt(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Foreign(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Str => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Array(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Slice(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::RawPtr(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Ref(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::FnDef(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::FnPtr(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Dynamic(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Closure(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Generator(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::GeneratorWitness(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Never => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Tuple(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Projection(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::UnnormalizedProjection(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Opaque(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Param(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Bound(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Placeholder(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Infer(..) => (), //~ ERROR usage of `ty::TyKind::` + TyKind::Error => (), //~ ERROR usage of `ty::TyKind::` + } + + if let ty::Int(int_ty) = sty {} + + if let TyKind::Int(int_ty) = sty {} //~ ERROR usage of `ty::TyKind::` + + fn ty_kind(ty_bad: TyKind<'_>, ty_good: Ty<'_>) {} //~ ERROR usage of `ty::TyKind` +} diff --git a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr new file mode 100644 index 0000000000000..82a8c715560e8 --- /dev/null +++ b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr @@ -0,0 +1,196 @@ +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:21:15 + | +LL | let sty = TyKind::Bool; //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + | +note: lint level defined here + --> $DIR/ty_tykind_usage.rs:19:8 + | +LL | #[deny(usage_of_ty_tykind)] + | ^^^^^^^^^^^^^^^^^^ + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:24:9 + | +LL | TyKind::Bool => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:25:9 + | +LL | TyKind::Char => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:26:9 + | +LL | TyKind::Int(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:27:9 + | +LL | TyKind::Uint(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:28:9 + | +LL | TyKind::Float(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:29:9 + | +LL | TyKind::Adt(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:30:9 + | +LL | TyKind::Foreign(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:31:9 + | +LL | TyKind::Str => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:32:9 + | +LL | TyKind::Array(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:33:9 + | +LL | TyKind::Slice(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:34:9 + | +LL | TyKind::RawPtr(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:35:9 + | +LL | TyKind::Ref(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:36:9 + | +LL | TyKind::FnDef(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:37:9 + | +LL | TyKind::FnPtr(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:38:9 + | +LL | TyKind::Dynamic(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:39:9 + | +LL | TyKind::Closure(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:40:9 + | +LL | TyKind::Generator(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:41:9 + | +LL | TyKind::GeneratorWitness(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:42:9 + | +LL | TyKind::Never => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:43:9 + | +LL | TyKind::Tuple(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:44:9 + | +LL | TyKind::Projection(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:45:9 + | +LL | TyKind::UnnormalizedProjection(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:46:9 + | +LL | TyKind::Opaque(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:47:9 + | +LL | TyKind::Param(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:48:9 + | +LL | TyKind::Bound(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:49:9 + | +LL | TyKind::Placeholder(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:50:9 + | +LL | TyKind::Infer(..) => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:51:9 + | +LL | TyKind::Error => (), //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind::` + --> $DIR/ty_tykind_usage.rs:56:12 + | +LL | if let TyKind::Int(int_ty) = sty {} //~ ERROR usage of `ty::TyKind::` + | ^^^^^^ help: try using ty:: directly: `ty` + +error: usage of `ty::TyKind` + --> $DIR/ty_tykind_usage.rs:58:24 + | +LL | fn ty_kind(ty_bad: TyKind<'_>, ty_good: Ty<'_>) {} //~ ERROR usage of `ty::TyKind` + | ^^^^^^^^^^ + | + = help: try using `ty::Ty` instead + +error: aborting due to 31 previous errors + From 5c0656789dfde752ea7af001e3d04a2a916685cf Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 6 Dec 2018 13:50:04 +0100 Subject: [PATCH 02/22] Add internal lints default_hash_types and usage_of_ty_tykind --- src/librustc/lint/internal.rs | 165 ++++++++++++++++++++++++++++++++++ src/librustc/lint/mod.rs | 1 + 2 files changed, 166 insertions(+) create mode 100644 src/librustc/lint/internal.rs diff --git a/src/librustc/lint/internal.rs b/src/librustc/lint/internal.rs new file mode 100644 index 0000000000000..d4cf18123c3e3 --- /dev/null +++ b/src/librustc/lint/internal.rs @@ -0,0 +1,165 @@ +// Copyright 2012-2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Some lints that are only useful in the compiler or crates that use compiler internals, such as +//! Clippy. + +use errors::Applicability; +use hir::{Expr, ExprKind, PatKind, Path, QPath, Ty, TyKind}; +use lint::{ + EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass, +}; +use rustc_data_structures::fx::FxHashMap; +use syntax::ast::Ident; + +declare_lint! { + pub DEFAULT_HASH_TYPES, + Warn, + "forbid HashMap and HashSet and suggest the FxHash* variants" +} + +pub struct DefaultHashTypes { + map: FxHashMap, +} + +impl DefaultHashTypes { + pub fn new() -> Self { + let mut map = FxHashMap::default(); + map.insert("HashMap".to_string(), "FxHashMap".to_string()); + map.insert("HashSet".to_string(), "FxHashSet".to_string()); + Self { map } + } +} + +impl LintPass for DefaultHashTypes { + fn get_lints(&self) -> LintArray { + lint_array!(DEFAULT_HASH_TYPES) + } +} + +impl EarlyLintPass for DefaultHashTypes { + fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) { + let ident_string = ident.to_string(); + if let Some(replace) = self.map.get(&ident_string) { + let msg = format!( + "Prefer {} over {}, it has better performance", + replace, ident_string + ); + let mut db = cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, &msg); + db.span_suggestion_with_applicability( + ident.span, + "use", + replace.to_string(), + Applicability::MaybeIncorrect, // FxHashMap, ... needs another import + ); + db.note(&format!( + "a `use rustc_data_structures::fx::{}` may be necessary", + replace + )) + .emit(); + } + } +} + +declare_lint! { + pub USAGE_OF_TY_TYKIND, + Warn, + "Usage of `ty::TyKind` outside of the `ty::sty` module" +} + +pub struct TyKindUsage; + +impl LintPass for TyKindUsage { + fn get_lints(&self) -> LintArray { + lint_array!(USAGE_OF_TY_TYKIND) + } +} + +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { + fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &'tcx Expr) { + let qpaths = match &expr.node { + ExprKind::Match(_, arms, _) => { + let mut qpaths = vec![]; + for arm in arms { + for pat in &arm.pats { + match &pat.node { + PatKind::Path(qpath) | PatKind::TupleStruct(qpath, ..) => { + qpaths.push(qpath) + } + _ => (), + } + } + } + qpaths + } + ExprKind::Path(qpath) => vec![qpath], + _ => vec![], + }; + for qpath in qpaths { + if let QPath::Resolved(_, path) = qpath { + let segments_iter = path.segments.iter().rev().skip(1).rev(); + + if let Some(last) = segments_iter.clone().last() { + if last.ident.as_str() == "TyKind" { + let path = Path { + span: path.span.with_hi(last.ident.span.hi()), + def: path.def, + segments: segments_iter.cloned().collect(), + }; + + if let Some(def) = last.def { + if def + .def_id() + .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) + { + cx.struct_span_lint( + USAGE_OF_TY_TYKIND, + path.span, + "usage of `ty::TyKind::`", + ) + .span_suggestion_with_applicability( + path.span, + "try using ty:: directly", + "ty".to_string(), + Applicability::MaybeIncorrect, // ty maybe needs an import + ).emit(); + } + } + } + } + } + } + } + + fn check_ty(&mut self, cx: &LateContext<'_, '_>, ty: &'tcx Ty) { + if let TyKind::Path(qpath) = &ty.node { + if let QPath::Resolved(_, path) = qpath { + if let Some(last) = path.segments.iter().last() { + if last.ident.as_str() == "TyKind" { + if let Some(def) = last.def { + if def + .def_id() + .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) + { + cx.struct_span_lint( + USAGE_OF_TY_TYKIND, + path.span, + "usage of `ty::TyKind`", + ) + .help("try using `ty::Ty` instead") + .emit(); + } + } + } + } + } + } + } +} diff --git a/src/librustc/lint/mod.rs b/src/librustc/lint/mod.rs index a5506bb8f59f4..b54d26054da1c 100644 --- a/src/librustc/lint/mod.rs +++ b/src/librustc/lint/mod.rs @@ -574,6 +574,7 @@ impl_stable_hash_for!(enum self::LintSource { pub type LevelSource = (Level, LintSource); pub mod builtin; +pub mod internal; mod context; mod levels; From 4c9fb9361a2a81745e735790450bb2b653b7c279 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 6 Dec 2018 14:02:21 +0100 Subject: [PATCH 03/22] Uplift match_def_path from Clippy --- src/librustc/hir/def_id.rs | 107 +++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 3 deletions(-) diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index 397843fd75afa..abfa96841d9f6 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -1,10 +1,10 @@ -use crate::ty; -use crate::ty::TyCtxt; -use crate::hir::map::definitions::FIRST_FREE_HIGH_DEF_INDEX; +use crate::ty::{self, print::Printer, subst::Kind, Ty, TyCtxt}; +use crate::hir::map::definitions::{DisambiguatedDefPathData, FIRST_FREE_HIGH_DEF_INDEX}; use rustc_data_structures::indexed_vec::Idx; use serialize; use std::fmt; use std::u32; +use syntax::symbol::{LocalInternedString, Symbol}; newtype_index! { pub struct CrateId { @@ -252,6 +252,107 @@ impl DefId { format!("module `{}`", tcx.def_path_str(*self)) } } + + /// Check if a `DefId`'s path matches the given absolute type path usage. + // Uplifted from rust-lang/rust-clippy + pub fn match_path<'a, 'tcx>(self, tcx: TyCtxt<'a, 'tcx, 'tcx>, path: &[&str]) -> bool { + pub struct AbsolutePathPrinter<'a, 'tcx> { + pub tcx: TyCtxt<'a, 'tcx, 'tcx>, + } + + impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { + type Error = !; + + type Path = Vec; + type Region = (); + type Type = (); + type DynExistential = (); + + fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> { + self.tcx + } + + fn print_region(self, _region: ty::Region<'_>) -> Result { + Ok(()) + } + + fn print_type(self, _ty: Ty<'tcx>) -> Result { + Ok(()) + } + + fn print_dyn_existential( + self, + _predicates: &'tcx ty::List>, + ) -> Result { + Ok(()) + } + + fn path_crate(self, cnum: CrateNum) -> Result { + Ok(vec![self.tcx.original_crate_name(cnum).as_str()]) + } + + fn path_qualified( + self, + self_ty: Ty<'tcx>, + trait_ref: Option>, + ) -> Result { + if trait_ref.is_none() { + if let ty::Adt(def, substs) = self_ty.sty { + return self.print_def_path(def.did, substs); + } + } + + // This shouldn't ever be needed, but just in case: + Ok(vec![match trait_ref { + Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(), + None => Symbol::intern(&format!("<{}>", self_ty)).as_str(), + }]) + } + + fn path_append_impl( + self, + print_prefix: impl FnOnce(Self) -> Result, + _disambiguated_data: &DisambiguatedDefPathData, + self_ty: Ty<'tcx>, + trait_ref: Option>, + ) -> Result { + let mut path = print_prefix(self)?; + + // This shouldn't ever be needed, but just in case: + path.push(match trait_ref { + Some(trait_ref) => { + Symbol::intern(&format!("", trait_ref, self_ty)).as_str() + }, + None => Symbol::intern(&format!("", self_ty)).as_str(), + }); + + Ok(path) + } + + fn path_append( + self, + print_prefix: impl FnOnce(Self) -> Result, + disambiguated_data: &DisambiguatedDefPathData, + ) -> Result { + let mut path = print_prefix(self)?; + path.push(disambiguated_data.data.as_interned_str().as_str()); + Ok(path) + } + + fn path_generic_args( + self, + print_prefix: impl FnOnce(Self) -> Result, + _args: &[Kind<'tcx>], + ) -> Result { + print_prefix(self) + } + } + + let names = AbsolutePathPrinter { tcx }.print_def_path(self, &[]).unwrap(); + + names.len() == path.len() + && names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b) + } } impl serialize::UseSpecializedEncodable for DefId {} From a2a8c441068bc73bc37ccec03bfefed2ef9ff054 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 6 Dec 2018 14:03:12 +0100 Subject: [PATCH 04/22] Add register_internals function to `rustc_lint` --- src/librustc_lint/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index c9301a32d83c4..f741f37594ec3 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -61,6 +61,7 @@ use nonstandard_style::*; use builtin::*; use types::*; use unused::*; +use rustc::lint::internal::*; /// Useful for other parts of the compiler. pub use builtin::SoftLints; @@ -488,3 +489,18 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { store.register_removed("bad_repr", "replaced with a generic attribute input check"); } + +pub fn register_internals(store: &mut lint::LintStore, sess: Option<&Session>) { + store.register_early_pass(sess, false, false, box DefaultHashTypes::new()); + store.register_late_pass(sess, false, false, false, box TyKindUsage); + store.register_group( + sess, + false, + "internal", + None, + vec![ + LintId::of(DEFAULT_HASH_TYPES), + LintId::of(USAGE_OF_TY_TYKIND), + ], + ); +} From 157e7974afa53a28804752d209c70738d10b093b Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sun, 24 Feb 2019 17:54:53 +0100 Subject: [PATCH 05/22] Fix rebase fallout --- src/librustc/lint/internal.rs | 31 +++++---- .../internal-lints/default_hash_types.rs | 10 --- .../internal-lints/default_hash_types.stderr | 14 ++-- .../internal-lints/ty_tykind_usage.rs | 10 --- .../internal-lints/ty_tykind_usage.stderr | 64 +++++++++---------- 5 files changed, 54 insertions(+), 75 deletions(-) diff --git a/src/librustc/lint/internal.rs b/src/librustc/lint/internal.rs index d4cf18123c3e3..22386b1c7a588 100644 --- a/src/librustc/lint/internal.rs +++ b/src/librustc/lint/internal.rs @@ -1,21 +1,11 @@ -// Copyright 2012-2018 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - //! Some lints that are only useful in the compiler or crates that use compiler internals, such as //! Clippy. -use errors::Applicability; -use hir::{Expr, ExprKind, PatKind, Path, QPath, Ty, TyKind}; -use lint::{ +use crate::hir::{Expr, ExprKind, PatKind, Path, QPath, Ty, TyKind}; +use crate::lint::{ EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass, }; +use errors::Applicability; use rustc_data_structures::fx::FxHashMap; use syntax::ast::Ident; @@ -42,6 +32,10 @@ impl LintPass for DefaultHashTypes { fn get_lints(&self) -> LintArray { lint_array!(DEFAULT_HASH_TYPES) } + + fn name(&self) -> &'static str { + "DefaultHashTypes" + } } impl EarlyLintPass for DefaultHashTypes { @@ -53,7 +47,7 @@ impl EarlyLintPass for DefaultHashTypes { replace, ident_string ); let mut db = cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, &msg); - db.span_suggestion_with_applicability( + db.span_suggestion( ident.span, "use", replace.to_string(), @@ -80,6 +74,10 @@ impl LintPass for TyKindUsage { fn get_lints(&self) -> LintArray { lint_array!(USAGE_OF_TY_TYKIND) } + + fn name(&self) -> &'static str { + "TyKindUsage" + } } impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { @@ -124,12 +122,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { path.span, "usage of `ty::TyKind::`", ) - .span_suggestion_with_applicability( + .span_suggestion( path.span, "try using ty:: directly", "ty".to_string(), Applicability::MaybeIncorrect, // ty maybe needs an import - ).emit(); + ) + .emit(); } } } diff --git a/src/test/ui-fulldeps/internal-lints/default_hash_types.rs b/src/test/ui-fulldeps/internal-lints/default_hash_types.rs index 6d32744145a8a..a6b0dbafbeb9b 100644 --- a/src/test/ui-fulldeps/internal-lints/default_hash_types.rs +++ b/src/test/ui-fulldeps/internal-lints/default_hash_types.rs @@ -1,13 +1,3 @@ -// Copyright 2018 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - // compile-flags: -Z internal-lints #![feature(rustc_private)] diff --git a/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr b/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr index 4f40c712aec4d..323a3880d1cdc 100644 --- a/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr +++ b/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr @@ -1,5 +1,5 @@ warning: Prefer FxHashMap over HashMap, it has better performance - --> $DIR/default_hash_types.rs:18:24 + --> $DIR/default_hash_types.rs:8:24 | LL | use std::collections::{HashMap, HashSet}; | ^^^^^^^ help: use: `FxHashMap` @@ -8,7 +8,7 @@ LL | use std::collections::{HashMap, HashSet}; = note: a `use rustc_data_structures::fx::FxHashMap` may be necessary warning: Prefer FxHashSet over HashSet, it has better performance - --> $DIR/default_hash_types.rs:18:33 + --> $DIR/default_hash_types.rs:8:33 | LL | use std::collections::{HashMap, HashSet}; | ^^^^^^^ help: use: `FxHashSet` @@ -16,20 +16,20 @@ LL | use std::collections::{HashMap, HashSet}; = note: a `use rustc_data_structures::fx::FxHashSet` may be necessary error: Prefer FxHashMap over HashMap, it has better performance - --> $DIR/default_hash_types.rs:24:15 + --> $DIR/default_hash_types.rs:14:15 | LL | let _map: HashMap = HashMap::default(); | ^^^^^^^ help: use: `FxHashMap` | note: lint level defined here - --> $DIR/default_hash_types.rs:22:8 + --> $DIR/default_hash_types.rs:12:8 | LL | #[deny(default_hash_types)] | ^^^^^^^^^^^^^^^^^^ = note: a `use rustc_data_structures::fx::FxHashMap` may be necessary error: Prefer FxHashMap over HashMap, it has better performance - --> $DIR/default_hash_types.rs:24:41 + --> $DIR/default_hash_types.rs:14:41 | LL | let _map: HashMap = HashMap::default(); | ^^^^^^^ help: use: `FxHashMap` @@ -37,7 +37,7 @@ LL | let _map: HashMap = HashMap::default(); = note: a `use rustc_data_structures::fx::FxHashMap` may be necessary error: Prefer FxHashSet over HashSet, it has better performance - --> $DIR/default_hash_types.rs:27:15 + --> $DIR/default_hash_types.rs:17:15 | LL | let _set: HashSet = HashSet::default(); | ^^^^^^^ help: use: `FxHashSet` @@ -45,7 +45,7 @@ LL | let _set: HashSet = HashSet::default(); = note: a `use rustc_data_structures::fx::FxHashSet` may be necessary error: Prefer FxHashSet over HashSet, it has better performance - --> $DIR/default_hash_types.rs:27:33 + --> $DIR/default_hash_types.rs:17:33 | LL | let _set: HashSet = HashSet::default(); | ^^^^^^^ help: use: `FxHashSet` diff --git a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs index 9962d9c6bcbbb..a1e08cd3b95bc 100644 --- a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs +++ b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs @@ -1,13 +1,3 @@ -// Copyright 2018 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - // compile-flags: -Z internal-lints #![feature(rustc_private)] diff --git a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr index 82a8c715560e8..d3ad5e1264a4d 100644 --- a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr +++ b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr @@ -1,191 +1,191 @@ error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:21:15 + --> $DIR/ty_tykind_usage.rs:11:15 | LL | let sty = TyKind::Bool; //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` | note: lint level defined here - --> $DIR/ty_tykind_usage.rs:19:8 + --> $DIR/ty_tykind_usage.rs:9:8 | LL | #[deny(usage_of_ty_tykind)] | ^^^^^^^^^^^^^^^^^^ error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:24:9 + --> $DIR/ty_tykind_usage.rs:14:9 | LL | TyKind::Bool => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:25:9 + --> $DIR/ty_tykind_usage.rs:15:9 | LL | TyKind::Char => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:26:9 + --> $DIR/ty_tykind_usage.rs:16:9 | LL | TyKind::Int(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:27:9 + --> $DIR/ty_tykind_usage.rs:17:9 | LL | TyKind::Uint(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:28:9 + --> $DIR/ty_tykind_usage.rs:18:9 | LL | TyKind::Float(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:29:9 + --> $DIR/ty_tykind_usage.rs:19:9 | LL | TyKind::Adt(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:30:9 + --> $DIR/ty_tykind_usage.rs:20:9 | LL | TyKind::Foreign(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:31:9 + --> $DIR/ty_tykind_usage.rs:21:9 | LL | TyKind::Str => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:32:9 + --> $DIR/ty_tykind_usage.rs:22:9 | LL | TyKind::Array(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:33:9 + --> $DIR/ty_tykind_usage.rs:23:9 | LL | TyKind::Slice(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:34:9 + --> $DIR/ty_tykind_usage.rs:24:9 | LL | TyKind::RawPtr(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:35:9 + --> $DIR/ty_tykind_usage.rs:25:9 | LL | TyKind::Ref(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:36:9 + --> $DIR/ty_tykind_usage.rs:26:9 | LL | TyKind::FnDef(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:37:9 + --> $DIR/ty_tykind_usage.rs:27:9 | LL | TyKind::FnPtr(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:38:9 + --> $DIR/ty_tykind_usage.rs:28:9 | LL | TyKind::Dynamic(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:39:9 + --> $DIR/ty_tykind_usage.rs:29:9 | LL | TyKind::Closure(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:40:9 + --> $DIR/ty_tykind_usage.rs:30:9 | LL | TyKind::Generator(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:41:9 + --> $DIR/ty_tykind_usage.rs:31:9 | LL | TyKind::GeneratorWitness(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:42:9 + --> $DIR/ty_tykind_usage.rs:32:9 | LL | TyKind::Never => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:43:9 + --> $DIR/ty_tykind_usage.rs:33:9 | LL | TyKind::Tuple(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:44:9 + --> $DIR/ty_tykind_usage.rs:34:9 | LL | TyKind::Projection(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:45:9 + --> $DIR/ty_tykind_usage.rs:35:9 | LL | TyKind::UnnormalizedProjection(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:46:9 + --> $DIR/ty_tykind_usage.rs:36:9 | LL | TyKind::Opaque(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:47:9 + --> $DIR/ty_tykind_usage.rs:37:9 | LL | TyKind::Param(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:48:9 + --> $DIR/ty_tykind_usage.rs:38:9 | LL | TyKind::Bound(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:49:9 + --> $DIR/ty_tykind_usage.rs:39:9 | LL | TyKind::Placeholder(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:50:9 + --> $DIR/ty_tykind_usage.rs:40:9 | LL | TyKind::Infer(..) => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:51:9 + --> $DIR/ty_tykind_usage.rs:41:9 | LL | TyKind::Error => (), //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` - --> $DIR/ty_tykind_usage.rs:56:12 + --> $DIR/ty_tykind_usage.rs:46:12 | LL | if let TyKind::Int(int_ty) = sty {} //~ ERROR usage of `ty::TyKind::` | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind` - --> $DIR/ty_tykind_usage.rs:58:24 + --> $DIR/ty_tykind_usage.rs:48:24 | LL | fn ty_kind(ty_bad: TyKind<'_>, ty_good: Ty<'_>) {} //~ ERROR usage of `ty::TyKind` | ^^^^^^^^^^ From 16acf7d618e13c8446a359fa6728878d375ae26e Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 28 Feb 2019 17:34:01 +0100 Subject: [PATCH 06/22] use check_path instead of check_expr --- src/librustc/lint/internal.rs | 76 +++++++++++++---------------------- 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/src/librustc/lint/internal.rs b/src/librustc/lint/internal.rs index 22386b1c7a588..8314bf7ae9d6e 100644 --- a/src/librustc/lint/internal.rs +++ b/src/librustc/lint/internal.rs @@ -1,7 +1,7 @@ //! Some lints that are only useful in the compiler or crates that use compiler internals, such as //! Clippy. -use crate::hir::{Expr, ExprKind, PatKind, Path, QPath, Ty, TyKind}; +use crate::hir::{HirId, Path, QPath, Ty, TyKind}; use crate::lint::{ EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass, }; @@ -81,56 +81,34 @@ impl LintPass for TyKindUsage { } impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { - fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &'tcx Expr) { - let qpaths = match &expr.node { - ExprKind::Match(_, arms, _) => { - let mut qpaths = vec![]; - for arm in arms { - for pat in &arm.pats { - match &pat.node { - PatKind::Path(qpath) | PatKind::TupleStruct(qpath, ..) => { - qpaths.push(qpath) - } - _ => (), - } - } - } - qpaths - } - ExprKind::Path(qpath) => vec![qpath], - _ => vec![], - }; - for qpath in qpaths { - if let QPath::Resolved(_, path) = qpath { - let segments_iter = path.segments.iter().rev().skip(1).rev(); + fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path, _: HirId) { + let segments_iter = path.segments.iter().rev().skip(1).rev(); - if let Some(last) = segments_iter.clone().last() { - if last.ident.as_str() == "TyKind" { - let path = Path { - span: path.span.with_hi(last.ident.span.hi()), - def: path.def, - segments: segments_iter.cloned().collect(), - }; + if let Some(last) = segments_iter.clone().last() { + if last.ident.as_str() == "TyKind" { + let path = Path { + span: path.span.with_hi(last.ident.span.hi()), + def: path.def, + segments: segments_iter.cloned().collect(), + }; - if let Some(def) = last.def { - if def - .def_id() - .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) - { - cx.struct_span_lint( - USAGE_OF_TY_TYKIND, - path.span, - "usage of `ty::TyKind::`", - ) - .span_suggestion( - path.span, - "try using ty:: directly", - "ty".to_string(), - Applicability::MaybeIncorrect, // ty maybe needs an import - ) - .emit(); - } - } + if let Some(def) = last.def { + if def + .def_id() + .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) + { + cx.struct_span_lint( + USAGE_OF_TY_TYKIND, + path.span, + "usage of `ty::TyKind::`", + ) + .span_suggestion( + path.span, + "try using ty:: directly", + "ty".to_string(), + Applicability::MaybeIncorrect, // ty maybe needs an import + ) + .emit(); } } } From 9b2bf7085133f916219a357c78c7b8c75a3269ba Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sat, 16 Mar 2019 14:59:34 +0100 Subject: [PATCH 07/22] Make internal lints allow-by-default --- src/librustc/lint/internal.rs | 4 ++-- src/librustc_interface/util.rs | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/librustc/lint/internal.rs b/src/librustc/lint/internal.rs index 8314bf7ae9d6e..7d82076454996 100644 --- a/src/librustc/lint/internal.rs +++ b/src/librustc/lint/internal.rs @@ -11,7 +11,7 @@ use syntax::ast::Ident; declare_lint! { pub DEFAULT_HASH_TYPES, - Warn, + Allow, "forbid HashMap and HashSet and suggest the FxHash* variants" } @@ -64,7 +64,7 @@ impl EarlyLintPass for DefaultHashTypes { declare_lint! { pub USAGE_OF_TY_TYKIND, - Warn, + Allow, "Usage of `ty::TyKind` outside of the `ty::sty` module" } diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index 6e4f2bf24e32f..f6e25d2c4db88 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -109,6 +109,7 @@ pub fn create_session( let codegen_backend = get_codegen_backend(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); + rustc_lint::register_internals(&mut sess.lint_store.borrow_mut(), Some(&sess)); let mut cfg = config::build_configuration(&sess, config::to_crate_config(cfg)); add_configuration(&mut cfg, &sess, &*codegen_backend); From 5a788f0ff7c8bbc83dc1cb6db58194ff5759b881 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 19 Mar 2019 22:46:45 +0100 Subject: [PATCH 08/22] Fix bug in TyKind lint --- src/librustc/lint/internal.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/librustc/lint/internal.rs b/src/librustc/lint/internal.rs index 7d82076454996..3ae0796829699 100644 --- a/src/librustc/lint/internal.rs +++ b/src/librustc/lint/internal.rs @@ -1,7 +1,7 @@ //! Some lints that are only useful in the compiler or crates that use compiler internals, such as //! Clippy. -use crate::hir::{HirId, Path, QPath, Ty, TyKind}; +use crate::hir::{def::Def, HirId, Path, QPath, Ty, TyKind}; use crate::lint::{ EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass, }; @@ -92,10 +92,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { segments: segments_iter.cloned().collect(), }; - if let Some(def) = last.def { - if def - .def_id() - .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) + match last.def { + Some(Def::Err) => (), + Some(def) + if def + .def_id() + .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) => { cx.struct_span_lint( USAGE_OF_TY_TYKIND, @@ -110,6 +112,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { ) .emit(); } + _ => (), } } } From e536037af3b5efde17b91b979591b4e0e677b080 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 21 Mar 2019 17:03:45 +0100 Subject: [PATCH 09/22] Deduplicate code in TyKind lint --- src/librustc/lint/internal.rs | 78 ++++++++++++++--------------------- 1 file changed, 30 insertions(+), 48 deletions(-) diff --git a/src/librustc/lint/internal.rs b/src/librustc/lint/internal.rs index 3ae0796829699..d5f8876d1621f 100644 --- a/src/librustc/lint/internal.rs +++ b/src/librustc/lint/internal.rs @@ -1,7 +1,7 @@ //! Some lints that are only useful in the compiler or crates that use compiler internals, such as //! Clippy. -use crate::hir::{def::Def, HirId, Path, QPath, Ty, TyKind}; +use crate::hir::{HirId, Path, PathSegment, QPath, Ty, TyKind}; use crate::lint::{ EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass, }; @@ -82,38 +82,19 @@ impl LintPass for TyKindUsage { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path, _: HirId) { - let segments_iter = path.segments.iter().rev().skip(1).rev(); - - if let Some(last) = segments_iter.clone().last() { - if last.ident.as_str() == "TyKind" { - let path = Path { - span: path.span.with_hi(last.ident.span.hi()), - def: path.def, - segments: segments_iter.cloned().collect(), - }; - - match last.def { - Some(Def::Err) => (), - Some(def) - if def - .def_id() - .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) => - { - cx.struct_span_lint( - USAGE_OF_TY_TYKIND, - path.span, - "usage of `ty::TyKind::`", - ) - .span_suggestion( - path.span, - "try using ty:: directly", - "ty".to_string(), - Applicability::MaybeIncorrect, // ty maybe needs an import - ) - .emit(); - } - _ => (), - } + let segments = path.segments.iter().rev().skip(1).rev(); + + if let Some(last) = segments.last() { + let span = path.span.with_hi(last.ident.span.hi()); + if lint_ty_kind_usage(cx, last) { + cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, "usage of `ty::TyKind::`") + .span_suggestion( + span, + "try using ty:: directly", + "ty".to_string(), + Applicability::MaybeIncorrect, // ty maybe needs an import + ) + .emit(); } } } @@ -122,24 +103,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { if let TyKind::Path(qpath) = &ty.node { if let QPath::Resolved(_, path) = qpath { if let Some(last) = path.segments.iter().last() { - if last.ident.as_str() == "TyKind" { - if let Some(def) = last.def { - if def - .def_id() - .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) - { - cx.struct_span_lint( - USAGE_OF_TY_TYKIND, - path.span, - "usage of `ty::TyKind`", - ) - .help("try using `ty::Ty` instead") - .emit(); - } - } + if lint_ty_kind_usage(cx, last) { + cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, "usage of `ty::TyKind`") + .help("try using `ty::Ty` instead") + .emit(); } } } } } } + +fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool { + if segment.ident.as_str() == "TyKind" { + if let Some(def) = segment.def { + if let Some(did) = def.opt_def_id() { + return did.match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]); + } + } + } + + false +} From 28a5c414c34b524fa8a264d2ec86bc31b7474105 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 21 Mar 2019 17:09:04 +0100 Subject: [PATCH 10/22] Check for unstable-options flag before register internals --- src/librustc_interface/util.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index f6e25d2c4db88..7f620de0f67ce 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -109,7 +109,9 @@ pub fn create_session( let codegen_backend = get_codegen_backend(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); - rustc_lint::register_internals(&mut sess.lint_store.borrow_mut(), Some(&sess)); + if sess.unstable_options() { + rustc_lint::register_internals(&mut sess.lint_store.borrow_mut(), Some(&sess)); + } let mut cfg = config::build_configuration(&sess, config::to_crate_config(cfg)); add_configuration(&mut cfg, &sess, &*codegen_backend); From 2045dfed248d179c94d58eed7ca2922010b0fffc Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 21 Mar 2019 17:10:25 +0100 Subject: [PATCH 11/22] Update tests --- .../internal-lints/default_hash_types.rs | 4 +- .../internal-lints/default_hash_types.stderr | 27 ++------ .../internal-lints/ty_tykind_usage.rs | 2 +- .../internal-lints/ty_tykind_usage.stderr | 62 +++++++++---------- 4 files changed, 38 insertions(+), 57 deletions(-) diff --git a/src/test/ui-fulldeps/internal-lints/default_hash_types.rs b/src/test/ui-fulldeps/internal-lints/default_hash_types.rs index a6b0dbafbeb9b..3264099c876d0 100644 --- a/src/test/ui-fulldeps/internal-lints/default_hash_types.rs +++ b/src/test/ui-fulldeps/internal-lints/default_hash_types.rs @@ -1,4 +1,4 @@ -// compile-flags: -Z internal-lints +// compile-flags: -Z unstable-options #![feature(rustc_private)] @@ -6,8 +6,6 @@ extern crate rustc_data_structures; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use std::collections::{HashMap, HashSet}; -//~^ WARNING Prefer FxHashMap over HashMap, it has better performance -//~^^ WARNING Prefer FxHashSet over HashSet, it has better performance #[deny(default_hash_types)] fn main() { diff --git a/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr b/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr index 323a3880d1cdc..64f322cb0c165 100644 --- a/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr +++ b/src/test/ui-fulldeps/internal-lints/default_hash_types.stderr @@ -1,35 +1,18 @@ -warning: Prefer FxHashMap over HashMap, it has better performance - --> $DIR/default_hash_types.rs:8:24 - | -LL | use std::collections::{HashMap, HashSet}; - | ^^^^^^^ help: use: `FxHashMap` - | - = note: #[warn(default_hash_types)] on by default - = note: a `use rustc_data_structures::fx::FxHashMap` may be necessary - -warning: Prefer FxHashSet over HashSet, it has better performance - --> $DIR/default_hash_types.rs:8:33 - | -LL | use std::collections::{HashMap, HashSet}; - | ^^^^^^^ help: use: `FxHashSet` - | - = note: a `use rustc_data_structures::fx::FxHashSet` may be necessary - error: Prefer FxHashMap over HashMap, it has better performance - --> $DIR/default_hash_types.rs:14:15 + --> $DIR/default_hash_types.rs:12:15 | LL | let _map: HashMap = HashMap::default(); | ^^^^^^^ help: use: `FxHashMap` | note: lint level defined here - --> $DIR/default_hash_types.rs:12:8 + --> $DIR/default_hash_types.rs:10:8 | LL | #[deny(default_hash_types)] | ^^^^^^^^^^^^^^^^^^ = note: a `use rustc_data_structures::fx::FxHashMap` may be necessary error: Prefer FxHashMap over HashMap, it has better performance - --> $DIR/default_hash_types.rs:14:41 + --> $DIR/default_hash_types.rs:12:41 | LL | let _map: HashMap = HashMap::default(); | ^^^^^^^ help: use: `FxHashMap` @@ -37,7 +20,7 @@ LL | let _map: HashMap = HashMap::default(); = note: a `use rustc_data_structures::fx::FxHashMap` may be necessary error: Prefer FxHashSet over HashSet, it has better performance - --> $DIR/default_hash_types.rs:17:15 + --> $DIR/default_hash_types.rs:15:15 | LL | let _set: HashSet = HashSet::default(); | ^^^^^^^ help: use: `FxHashSet` @@ -45,7 +28,7 @@ LL | let _set: HashSet = HashSet::default(); = note: a `use rustc_data_structures::fx::FxHashSet` may be necessary error: Prefer FxHashSet over HashSet, it has better performance - --> $DIR/default_hash_types.rs:17:33 + --> $DIR/default_hash_types.rs:15:33 | LL | let _set: HashSet = HashSet::default(); | ^^^^^^^ help: use: `FxHashSet` diff --git a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs index a1e08cd3b95bc..dba0db69b7f39 100644 --- a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs +++ b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.rs @@ -1,4 +1,4 @@ -// compile-flags: -Z internal-lints +// compile-flags: -Z unstable-options #![feature(rustc_private)] diff --git a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr index d3ad5e1264a4d..4e94af12453cd 100644 --- a/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr +++ b/src/test/ui-fulldeps/internal-lints/ty_tykind_usage.stderr @@ -1,7 +1,7 @@ error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:11:15 | -LL | let sty = TyKind::Bool; //~ ERROR usage of `ty::TyKind::` +LL | let sty = TyKind::Bool; | ^^^^^^ help: try using ty:: directly: `ty` | note: lint level defined here @@ -13,181 +13,181 @@ LL | #[deny(usage_of_ty_tykind)] error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:14:9 | -LL | TyKind::Bool => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Bool => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:15:9 | -LL | TyKind::Char => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Char => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:16:9 | -LL | TyKind::Int(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Int(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:17:9 | -LL | TyKind::Uint(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Uint(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:18:9 | -LL | TyKind::Float(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Float(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:19:9 | -LL | TyKind::Adt(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Adt(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:20:9 | -LL | TyKind::Foreign(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Foreign(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:21:9 | -LL | TyKind::Str => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Str => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:22:9 | -LL | TyKind::Array(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Array(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:23:9 | -LL | TyKind::Slice(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Slice(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:24:9 | -LL | TyKind::RawPtr(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::RawPtr(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:25:9 | -LL | TyKind::Ref(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Ref(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:26:9 | -LL | TyKind::FnDef(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::FnDef(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:27:9 | -LL | TyKind::FnPtr(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::FnPtr(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:28:9 | -LL | TyKind::Dynamic(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Dynamic(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:29:9 | -LL | TyKind::Closure(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Closure(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:30:9 | -LL | TyKind::Generator(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Generator(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:31:9 | -LL | TyKind::GeneratorWitness(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::GeneratorWitness(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:32:9 | -LL | TyKind::Never => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Never => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:33:9 | -LL | TyKind::Tuple(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Tuple(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:34:9 | -LL | TyKind::Projection(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Projection(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:35:9 | -LL | TyKind::UnnormalizedProjection(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::UnnormalizedProjection(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:36:9 | -LL | TyKind::Opaque(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Opaque(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:37:9 | -LL | TyKind::Param(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Param(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:38:9 | -LL | TyKind::Bound(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Bound(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:39:9 | -LL | TyKind::Placeholder(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Placeholder(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:40:9 | -LL | TyKind::Infer(..) => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Infer(..) => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:41:9 | -LL | TyKind::Error => (), //~ ERROR usage of `ty::TyKind::` +LL | TyKind::Error => (), | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind::` --> $DIR/ty_tykind_usage.rs:46:12 | -LL | if let TyKind::Int(int_ty) = sty {} //~ ERROR usage of `ty::TyKind::` +LL | if let TyKind::Int(int_ty) = sty {} | ^^^^^^ help: try using ty:: directly: `ty` error: usage of `ty::TyKind` --> $DIR/ty_tykind_usage.rs:48:24 | -LL | fn ty_kind(ty_bad: TyKind<'_>, ty_good: Ty<'_>) {} //~ ERROR usage of `ty::TyKind` +LL | fn ty_kind(ty_bad: TyKind<'_>, ty_good: Ty<'_>) {} | ^^^^^^^^^^ | = help: try using `ty::Ty` instead From dfcd1ef102b78768351e3c31ed3c6c15c3af38e4 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sun, 31 Mar 2019 14:57:18 +0200 Subject: [PATCH 12/22] Add unstable-options flag to stage!=0 --- src/bootstrap/bin/rustc.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index 7429492f914a3..86ce5fd01a812 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -316,6 +316,11 @@ fn main() { } } + // This is required for internal lints. + if stage != "0" { + cmd.arg("-Zunstable-options"); + } + // Force all crates compiled by this compiler to (a) be unstable and (b) // allow the `rustc_private` feature to link to other unstable crates // also in the sysroot. We also do this for host crates, since those From 69f74df4294cea8c8bb0095e2d73cc677f16fc48 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sun, 31 Mar 2019 15:01:46 +0200 Subject: [PATCH 13/22] Deny internal lints in librustc --- src/librustc/ich/hcx.rs | 8 +++----- src/librustc/infer/error_reporting/mod.rs | 22 +++++++++++----------- src/librustc/lib.rs | 1 + src/librustc/mir/tcx.rs | 2 +- src/librustc/ty/mod.rs | 2 ++ src/librustc/util/common.rs | 9 ++++----- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index e60fdd62debd1..a8e5db26eadc1 100644 --- a/src/librustc/ich/hcx.rs +++ b/src/librustc/ich/hcx.rs @@ -9,7 +9,6 @@ use crate::session::Session; use std::cmp::Ord; use std::hash as std_hash; -use std::collections::HashMap; use std::cell::RefCell; use syntax::ast; @@ -394,13 +393,12 @@ impl<'a> HashStable> for DelimSpan { } } -pub fn hash_stable_trait_impls<'a, 'gcx, W, R>( +pub fn hash_stable_trait_impls<'a, 'gcx, W>( hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher, blanket_impls: &[DefId], - non_blanket_impls: &HashMap, R>) - where W: StableHasherResult, - R: std_hash::BuildHasher, + non_blanket_impls: &FxHashMap>) + where W: StableHasherResult { { let mut blanket_impls: SmallVec<[_; 8]> = blanket_impls diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs index 2810b5a8e6ada..19663161fe3fa 100644 --- a/src/librustc/infer/error_reporting/mod.rs +++ b/src/librustc/infer/error_reporting/mod.rs @@ -56,7 +56,7 @@ use crate::hir::Node; use crate::middle::region; use crate::traits::{ObligationCause, ObligationCauseCode}; use crate::ty::error::TypeError; -use crate::ty::{self, subst::{Subst, SubstsRef}, Region, Ty, TyCtxt, TyKind, TypeFoldable}; +use crate::ty::{self, subst::{Subst, SubstsRef}, Region, Ty, TyCtxt, TypeFoldable}; use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString}; use std::{cmp, fmt}; use syntax_pos::{Pos, Span}; @@ -1094,14 +1094,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { (_, false, _) => { if let Some(exp_found) = exp_found { let (def_id, ret_ty) = match exp_found.found.sty { - TyKind::FnDef(def, _) => { + ty::FnDef(def, _) => { (Some(def), Some(self.tcx.fn_sig(def).output())) } _ => (None, None), }; let exp_is_struct = match exp_found.expected.sty { - TyKind::Adt(def, _) => def.is_struct(), + ty::Adt(def, _) => def.is_struct(), _ => false, }; @@ -1140,8 +1140,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { diag: &mut DiagnosticBuilder<'tcx>, ) { match (&exp_found.expected.sty, &exp_found.found.sty) { - (TyKind::Adt(exp_def, exp_substs), TyKind::Ref(_, found_ty, _)) => { - if let TyKind::Adt(found_def, found_substs) = found_ty.sty { + (ty::Adt(exp_def, exp_substs), ty::Ref(_, found_ty, _)) => { + if let ty::Adt(found_def, found_substs) = found_ty.sty { let path_str = format!("{:?}", exp_def); if exp_def == &found_def { let opt_msg = "you can convert from `&Option` to `Option<&T>` using \ @@ -1164,17 +1164,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { let mut show_suggestion = true; for (exp_ty, found_ty) in exp_substs.types().zip(found_substs.types()) { match exp_ty.sty { - TyKind::Ref(_, exp_ty, _) => { + ty::Ref(_, exp_ty, _) => { match (&exp_ty.sty, &found_ty.sty) { - (_, TyKind::Param(_)) | - (_, TyKind::Infer(_)) | - (TyKind::Param(_), _) | - (TyKind::Infer(_), _) => {} + (_, ty::Param(_)) | + (_, ty::Infer(_)) | + (ty::Param(_), _) | + (ty::Infer(_), _) => {} _ if ty::TyS::same_type(exp_ty, found_ty) => {} _ => show_suggestion = false, }; } - TyKind::Param(_) | TyKind::Infer(_) => {} + ty::Param(_) | ty::Infer(_) => {} _ => show_suggestion = false, } } diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index b87343b43c9f9..0b75cb6c8a3e2 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -29,6 +29,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![allow(explicit_outlives_requirements)] #![feature(arbitrary_self_types)] diff --git a/src/librustc/mir/tcx.rs b/src/librustc/mir/tcx.rs index 0eb37f0ac9e96..23be1bbf6c687 100644 --- a/src/librustc/mir/tcx.rs +++ b/src/librustc/mir/tcx.rs @@ -36,7 +36,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> { pub fn field_ty(self, tcx: TyCtxt<'a, 'gcx, 'tcx>, f: &Field) -> Ty<'tcx> { let answer = match self.ty.sty { - ty::TyKind::Adt(adt_def, substs) => { + ty::Adt(adt_def, substs) => { let variant_def = match self.variant_index { None => adt_def.non_enum_variant(), Some(variant_index) => { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 102057c1380ac..7d47867cea125 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(stage0), allow(usage_of_ty_tykind))] + pub use self::Variance::*; pub use self::AssociatedItemContainer::*; pub use self::BorrowKind::*; diff --git a/src/librustc/util/common.rs b/src/librustc/util/common.rs index 5622fe434363a..26194176350ac 100644 --- a/src/librustc/util/common.rs +++ b/src/librustc/util/common.rs @@ -1,11 +1,10 @@ #![allow(non_camel_case_types)] -use rustc_data_structures::sync::Lock; +use rustc_data_structures::{fx::FxHashMap, sync::Lock}; use std::cell::{RefCell, Cell}; -use std::collections::HashMap; use std::fmt::Debug; -use std::hash::{Hash, BuildHasher}; +use std::hash::Hash; use std::panic; use std::env; use std::time::{Duration, Instant}; @@ -341,8 +340,8 @@ pub trait MemoizationMap { where OP: FnOnce() -> Self::Value; } -impl MemoizationMap for RefCell> - where K: Hash+Eq+Clone, V: Clone, S: BuildHasher +impl MemoizationMap for RefCell> + where K: Hash+Eq+Clone, V: Clone { type Key = K; type Value = V; From d3f0cb9b62d174213cabbe9bb4d9ba24e21b77c6 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sun, 31 Mar 2019 23:18:22 +0200 Subject: [PATCH 14/22] Deny internal lints on non conflicting crates - libarena - librustc_allocator - librustc_borrowck - librustc_codegen_ssa - librustc_codegen_utils - librustc_driver - librustc_errors - librustc_incremental - librustc_metadata - librustc_passes - librustc_privacy - librustc_resolve - librustc_save_analysis - librustc_target - librustc_traits - libsyntax - libsyntax_ext - libsyntax_pos --- src/libarena/lib.rs | 1 + src/librustc_allocator/lib.rs | 1 + src/librustc_borrowck/lib.rs | 1 + src/librustc_codegen_ssa/lib.rs | 1 + src/librustc_codegen_utils/lib.rs | 1 + src/librustc_driver/lib.rs | 1 + src/librustc_errors/lib.rs | 1 + src/librustc_incremental/lib.rs | 1 + src/librustc_metadata/lib.rs | 1 + src/librustc_passes/lib.rs | 1 + src/librustc_privacy/lib.rs | 1 + src/librustc_resolve/lib.rs | 1 + src/librustc_save_analysis/lib.rs | 1 + src/librustc_target/lib.rs | 1 + src/librustc_traits/lib.rs | 1 + src/libsyntax/lib.rs | 1 + src/libsyntax_ext/lib.rs | 1 + src/libsyntax_pos/lib.rs | 1 + 18 files changed, 18 insertions(+) diff --git a/src/libarena/lib.rs b/src/libarena/lib.rs index 0a5b79c36aad8..6fa15884f5abb 100644 --- a/src/libarena/lib.rs +++ b/src/libarena/lib.rs @@ -12,6 +12,7 @@ test(no_crate_inject, attr(deny(warnings))))] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![feature(alloc)] #![feature(core_intrinsics)] diff --git a/src/librustc_allocator/lib.rs b/src/librustc_allocator/lib.rs index 9d6e728e13557..a9e422fb238b8 100644 --- a/src/librustc_allocator/lib.rs +++ b/src/librustc_allocator/lib.rs @@ -2,6 +2,7 @@ #![feature(rustc_private)] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] pub mod expand; diff --git a/src/librustc_borrowck/lib.rs b/src/librustc_borrowck/lib.rs index cf4669db87e5e..3761a52bcccf2 100644 --- a/src/librustc_borrowck/lib.rs +++ b/src/librustc_borrowck/lib.rs @@ -2,6 +2,7 @@ #![allow(non_camel_case_types)] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![feature(nll)] diff --git a/src/librustc_codegen_ssa/lib.rs b/src/librustc_codegen_ssa/lib.rs index 1e898ced7a600..e2917578c0ece 100644 --- a/src/librustc_codegen_ssa/lib.rs +++ b/src/librustc_codegen_ssa/lib.rs @@ -14,6 +14,7 @@ #![allow(unused_attributes)] #![allow(dead_code)] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![allow(explicit_outlives_requirements)] #![recursion_limit="256"] diff --git a/src/librustc_codegen_utils/lib.rs b/src/librustc_codegen_utils/lib.rs index 1a3914e6ef44c..330cfe154e302 100644 --- a/src/librustc_codegen_utils/lib.rs +++ b/src/librustc_codegen_utils/lib.rs @@ -16,6 +16,7 @@ #![recursion_limit="256"] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #[macro_use] extern crate rustc; diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 4b7cffaad5509..2781bfa3ec849 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -17,6 +17,7 @@ #![recursion_limit="256"] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] pub extern crate getopts; #[cfg(unix)] diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index ebbc5a3d3a340..71bef54cd17a1 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -6,6 +6,7 @@ #![feature(nll)] #![feature(optin_builtin_traits)] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #[allow(unused_extern_crates)] extern crate serialize as rustc_serialize; // used by deriving diff --git a/src/librustc_incremental/lib.rs b/src/librustc_incremental/lib.rs index fe75bbc36c3b1..d7db324f3463e 100644 --- a/src/librustc_incremental/lib.rs +++ b/src/librustc_incremental/lib.rs @@ -8,6 +8,7 @@ #![recursion_limit="256"] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #[macro_use] extern crate rustc; #[allow(unused_extern_crates)] diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs index 14416b5ce0759..4078171733fc3 100644 --- a/src/librustc_metadata/lib.rs +++ b/src/librustc_metadata/lib.rs @@ -14,6 +14,7 @@ #![recursion_limit="256"] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] extern crate libc; #[allow(unused_extern_crates)] diff --git a/src/librustc_passes/lib.rs b/src/librustc_passes/lib.rs index ff2e345d08401..20442a4a566ec 100644 --- a/src/librustc_passes/lib.rs +++ b/src/librustc_passes/lib.rs @@ -12,6 +12,7 @@ #![recursion_limit="256"] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #[macro_use] extern crate rustc; diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 030883c0159fb..9a8970b2935e0 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -1,6 +1,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![feature(nll)] #![feature(rustc_diagnostic_macros)] diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 1934f800af757..5216156c0cab8 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -8,6 +8,7 @@ #![recursion_limit="256"] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] pub use rustc::hir::def::{Namespace, PerNS}; diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index 898ea62046d48..a363fe1141891 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -2,6 +2,7 @@ #![feature(custom_attribute)] #![feature(nll)] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![allow(unused_attributes)] #![recursion_limit="256"] diff --git a/src/librustc_target/lib.rs b/src/librustc_target/lib.rs index efffb1985728d..f1812c20dccde 100644 --- a/src/librustc_target/lib.rs +++ b/src/librustc_target/lib.rs @@ -16,6 +16,7 @@ #![feature(step_trait)] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #[macro_use] extern crate log; diff --git a/src/librustc_traits/lib.rs b/src/librustc_traits/lib.rs index d52a976981db0..bc034e1fb1627 100644 --- a/src/librustc_traits/lib.rs +++ b/src/librustc_traits/lib.rs @@ -2,6 +2,7 @@ //! the guts are broken up into modules; see the comments in those modules. #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![feature(crate_visibility_modifier)] #![feature(in_band_lifetimes)] diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index a6145d5dcb38c..9905b981395c0 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -8,6 +8,7 @@ test(attr(deny(warnings))))] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![feature(crate_visibility_modifier)] #![feature(label_break_value)] diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index aa472eee3cab3..ee0b86963f31d 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -3,6 +3,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![feature(in_band_lifetimes)] #![feature(proc_macro_diagnostic)] diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index 48d087ee43cd5..db1543ff13f7e 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -7,6 +7,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![feature(const_fn)] #![feature(crate_visibility_modifier)] From 818d300451591b456e7e7b2fc6dbb95698b0ed74 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sun, 31 Mar 2019 23:29:35 +0200 Subject: [PATCH 15/22] Deny internal lints on librustc_interface --- src/librustc_interface/interface.rs | 1 - src/librustc_interface/lib.rs | 1 + src/librustc_interface/util.rs | 1 - 3 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustc_interface/interface.rs b/src/librustc_interface/interface.rs index 245a2bf92d530..74085123f89ee 100644 --- a/src/librustc_interface/interface.rs +++ b/src/librustc_interface/interface.rs @@ -12,7 +12,6 @@ use rustc_data_structures::OnDrop; use rustc_data_structures::sync::Lrc; use rustc_data_structures::fx::{FxHashSet, FxHashMap}; use rustc_metadata::cstore::CStore; -use std::collections::HashSet; use std::io::Write; use std::path::PathBuf; use std::result; diff --git a/src/librustc_interface/lib.rs b/src/librustc_interface/lib.rs index 3314681b6981a..353ff6a57a5ef 100644 --- a/src/librustc_interface/lib.rs +++ b/src/librustc_interface/lib.rs @@ -7,6 +7,7 @@ #![cfg_attr(unix, feature(libc))] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![allow(unused_imports)] diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index 7f620de0f67ce..17523aedffb58 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -21,7 +21,6 @@ use rustc_plugin; use rustc_privacy; use rustc_resolve; use rustc_typeck; -use std::collections::HashSet; use std::env; use std::env::consts::{DLL_PREFIX, DLL_SUFFIX}; use std::io::{self, Write}; From d2bc99135f9a8df325de6b6359390f97ad654831 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sun, 31 Mar 2019 23:48:48 +0200 Subject: [PATCH 16/22] Deny internal lints on librustc_lint --- src/librustc_lint/builtin.rs | 6 +++--- src/librustc_lint/lib.rs | 1 + src/librustc_lint/types.rs | 11 ++++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 1fae931e9f1fd..541d779c477fc 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1036,7 +1036,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableTransmutes { let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \ consider instead using an UnsafeCell"; - match get_transmute_from_to(cx, expr) { + match get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (&ty1.sty, &ty2.sty)) { Some((&ty::Ref(_, _, from_mt), &ty::Ref(_, _, to_mt))) => { if to_mt == hir::Mutability::MutMutable && from_mt == hir::Mutability::MutImmutable { @@ -1049,7 +1049,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableTransmutes { fn get_transmute_from_to<'a, 'tcx> (cx: &LateContext<'a, 'tcx>, expr: &hir::Expr) - -> Option<(&'tcx ty::TyKind<'tcx>, &'tcx ty::TyKind<'tcx>)> { + -> Option<(Ty<'tcx>, Ty<'tcx>)> { let def = if let hir::ExprKind::Path(ref qpath) = expr.node { cx.tables.qpath_def(qpath, expr.hir_id) } else { @@ -1062,7 +1062,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableTransmutes { let sig = cx.tables.node_type(expr.hir_id).fn_sig(cx.tcx); let from = sig.inputs().skip_binder()[0]; let to = *sig.output().skip_binder(); - return Some((&from.sty, &to.sty)); + return Some((from, to)); } None } diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index f741f37594ec3..7e77962a16e0b 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -20,6 +20,7 @@ #![recursion_limit="256"] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #[macro_use] extern crate rustc; diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index 494a9bb73ed4b..fa106532662d1 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -321,7 +321,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { // // No suggestion for: `isize`, `usize`. fn get_type_suggestion<'a>( - t: &ty::TyKind<'_>, + t: Ty<'_>, val: u128, negative: bool, ) -> Option { @@ -347,14 +347,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { } } } - match t { - &ty::Int(i) => find_fit!(i, val, negative, + match t.sty { + ty::Int(i) => find_fit!(i, val, negative, I8 => [U8] => [I16, I32, I64, I128], I16 => [U16] => [I32, I64, I128], I32 => [U32] => [I64, I128], I64 => [U64] => [I128], I128 => [U128] => []), - &ty::Uint(u) => find_fit!(u, val, negative, + ty::Uint(u) => find_fit!(u, val, negative, U8 => [U8, U16, U32, U64, U128] => [], U16 => [U16, U32, U64, U128] => [], U32 => [U32, U64, U128] => [], @@ -364,6 +364,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { } } + #[cfg_attr(not(stage0), allow(usage_of_ty_tykind))] fn report_bin_hex_error( cx: &LateContext<'_, '_>, expr: &hir::Expr, @@ -398,7 +399,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { repr_str, val, t, actually, t )); if let Some(sugg_ty) = - get_type_suggestion(&cx.tables.node_type(expr.hir_id).sty, val, negative) + get_type_suggestion(&cx.tables.node_type(expr.hir_id), val, negative) { if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') { let (sans_suffix, _) = repr_str.split_at(pos); From e4b87f5edb3b5b0442a5fbe887bfc85068b6a4ef Mon Sep 17 00:00:00 2001 From: flip1995 Date: Mon, 1 Apr 2019 00:02:46 +0200 Subject: [PATCH 17/22] Deny internal lints on librustc_mir --- .../borrow_check/error_reporting.rs | 30 +++++++++---------- src/librustc_mir/borrow_check/mod.rs | 2 +- src/librustc_mir/borrow_check/move_errors.rs | 4 +-- .../borrow_check/mutability_errors.rs | 10 +++---- .../borrow_check/nll/explain_borrow/mod.rs | 2 +- .../nll/region_infer/error_reporting/mod.rs | 2 +- .../borrow_check/nll/type_check/mod.rs | 8 ++--- .../dataflow/move_paths/builder.rs | 2 +- src/librustc_mir/hair/pattern/_match.rs | 4 +-- src/librustc_mir/hair/pattern/check_match.rs | 12 ++++---- src/librustc_mir/lib.rs | 1 + src/librustc_mir/lints.rs | 4 +-- src/librustc_mir/transform/instcombine.rs | 4 +-- src/librustc_mir/transform/lower_128bit.rs | 6 ++-- 14 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs index c04a36fe9c69c..01c06739e2903 100644 --- a/src/librustc_mir/borrow_check/error_reporting.rs +++ b/src/librustc_mir/borrow_check/error_reporting.rs @@ -223,7 +223,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { Some(ref name) => format!("`{}`", name), None => "value".to_owned(), }; - if let ty::TyKind::Param(param_ty) = ty.sty { + if let ty::Param(param_ty) = ty.sty { let tcx = self.infcx.tcx; let generics = tcx.generics_of(self.mir_def_id); let def_id = generics.type_param(¶m_ty, tcx).def_id; @@ -1529,7 +1529,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { if let TerminatorKind::Call { func: Operand::Constant(box Constant { literal: ty::Const { - ty: &ty::TyS { sty: ty::TyKind::FnDef(id, _), .. }, + ty: &ty::TyS { sty: ty::FnDef(id, _), .. }, .. }, .. @@ -1547,7 +1547,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { }; debug!("add_moved_or_invoked_closure_note: closure={:?}", closure); - if let ty::TyKind::Closure(did, _) = self.mir.local_decls[closure].ty.sty { + if let ty::Closure(did, _) = self.mir.local_decls[closure].ty.sty { let hir_id = self.infcx.tcx.hir().as_local_hir_id(did).unwrap(); if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did) @@ -1570,7 +1570,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { // Check if we are just moving a closure after it has been invoked. if let Some(target) = target { - if let ty::TyKind::Closure(did, _) = self.mir.local_decls[target].ty.sty { + if let ty::Closure(did, _) = self.mir.local_decls[target].ty.sty { let hir_id = self.infcx.tcx.hir().as_local_hir_id(did).unwrap(); if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did) @@ -1919,7 +1919,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { } else { let ty = self.infcx.tcx.type_of(self.mir_def_id); match ty.sty { - ty::TyKind::FnDef(_, _) | ty::TyKind::FnPtr(_) => self.annotate_fn_sig( + ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig( self.mir_def_id, self.infcx.tcx.fn_sig(self.mir_def_id), ), @@ -2164,12 +2164,12 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { // anything. let return_ty = sig.output(); match return_ty.skip_binder().sty { - ty::TyKind::Ref(return_region, _, _) if return_region.has_name() && !is_closure => { + ty::Ref(return_region, _, _) if return_region.has_name() && !is_closure => { // This is case 1 from above, return type is a named reference so we need to // search for relevant arguments. let mut arguments = Vec::new(); for (index, argument) in sig.inputs().skip_binder().iter().enumerate() { - if let ty::TyKind::Ref(argument_region, _, _) = argument.sty { + if let ty::Ref(argument_region, _, _) = argument.sty { if argument_region == return_region { // Need to use the `rustc::ty` types to compare against the // `return_region`. Then use the `rustc::hir` type to get only @@ -2206,7 +2206,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { return_span, }) } - ty::TyKind::Ref(_, _, _) if is_closure => { + ty::Ref(_, _, _) if is_closure => { // This is case 2 from above but only for closures, return type is anonymous // reference so we select // the first argument. @@ -2215,9 +2215,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { // Closure arguments are wrapped in a tuple, so we need to get the first // from that. - if let ty::TyKind::Tuple(elems) = argument_ty.sty { + if let ty::Tuple(elems) = argument_ty.sty { let argument_ty = elems.first()?; - if let ty::TyKind::Ref(_, _, _) = argument_ty.sty { + if let ty::Ref(_, _, _) = argument_ty.sty { return Some(AnnotatedBorrowFnSignature::Closure { argument_ty, argument_span, @@ -2227,7 +2227,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { None } - ty::TyKind::Ref(_, _, _) => { + ty::Ref(_, _, _) => { // This is also case 2 from above but for functions, return type is still an // anonymous reference so we select the first argument. let argument_span = fn_decl.inputs.first()?.span; @@ -2238,7 +2238,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { // We expect the first argument to be a reference. match argument_ty.sty { - ty::TyKind::Ref(_, _, _) => {} + ty::Ref(_, _, _) => {} _ => return None, } @@ -2366,8 +2366,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { // this by hooking into the pretty printer and telling it to label the // lifetimes without names with the value `'0`. match ty.sty { - ty::TyKind::Ref(ty::RegionKind::ReLateBound(_, br), _, _) - | ty::TyKind::Ref( + ty::Ref(ty::RegionKind::ReLateBound(_, br), _, _) + | ty::Ref( ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }), _, _, @@ -2386,7 +2386,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS); let region = match ty.sty { - ty::TyKind::Ref(region, _, _) => { + ty::Ref(region, _, _) => { match region { ty::RegionKind::ReLateBound(_, br) | ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => { diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index a8c151a22eebc..bf297ae0debf0 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -1741,7 +1741,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { // no move out from an earlier location) then this is an attempt at initialization // of the union - we should error in that case. let tcx = this.infcx.tcx; - if let ty::TyKind::Adt(def, _) = base.ty(this.mir, tcx).ty.sty { + if let ty::Adt(def, _) = base.ty(this.mir, tcx).ty.sty { if def.is_union() { if this.move_data.path_map[mpi].iter().any(|moi| { this.move_data.moves[*moi].source.is_predecessor_of( diff --git a/src/librustc_mir/borrow_check/move_errors.rs b/src/librustc_mir/borrow_check/move_errors.rs index b6e7996586e4a..7efe1d83c2e5f 100644 --- a/src/librustc_mir/borrow_check/move_errors.rs +++ b/src/librustc_mir/borrow_check/move_errors.rs @@ -532,7 +532,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { if let StatementKind::Assign(_, box Rvalue::Ref(_, _, source)) = &stmt.kind { let ty = source.ty(self.mir, self.infcx.tcx).ty; let ty = match ty.sty { - ty::TyKind::Ref(_, ty, _) => ty, + ty::Ref(_, ty, _) => ty, _ => ty, }; debug!("borrowed_content_source: ty={:?}", ty); @@ -557,7 +557,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { let ty = source.ty(self.mir, self.infcx.tcx).ty; let ty = match ty.sty { - ty::TyKind::Ref(_, ty, _) => ty, + ty::Ref(_, ty, _) => ty, _ => ty, }; debug!("borrowed_content_source: ty={:?}", ty); diff --git a/src/librustc_mir/borrow_check/mutability_errors.rs b/src/librustc_mir/borrow_check/mutability_errors.rs index 8a55a59b15b17..b780511315d81 100644 --- a/src/librustc_mir/borrow_check/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/mutability_errors.rs @@ -5,7 +5,7 @@ use rustc::mir::{ Mutability, Operand, Place, PlaceBase, Projection, ProjectionElem, Static, StaticKind, }; use rustc::mir::{Terminator, TerminatorKind}; -use rustc::ty::{self, Const, DefIdTree, TyS, TyKind, TyCtxt}; +use rustc::ty::{self, Const, DefIdTree, TyS, TyCtxt}; use rustc_data_structures::indexed_vec::Idx; use syntax_pos::Span; use syntax_pos::symbol::keywords; @@ -261,7 +261,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { // Otherwise, check if the name is the self kewyord - in which case // we have an explicit self. Do the same thing in this case and check // for a `self: &mut Self` to suggest removing the `&mut`. - if let ty::TyKind::Ref( + if let ty::Ref( _, _, hir::Mutability::MutMutable ) = local_decl.ty.sty { true @@ -476,7 +476,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { func: Operand::Constant(box Constant { literal: Const { ty: &TyS { - sty: TyKind::FnDef(id, substs), + sty: ty::FnDef(id, substs), .. }, .. @@ -633,8 +633,8 @@ fn annotate_struct_field( field: &mir::Field, ) -> Option<(Span, String)> { // Expect our local to be a reference to a struct of some kind. - if let ty::TyKind::Ref(_, ty, _) = ty.sty { - if let ty::TyKind::Adt(def, _) = ty.sty { + if let ty::Ref(_, ty, _) = ty.sty { + if let ty::Adt(def, _) = ty.sty { let field = def.all_fields().nth(field.index())?; // Use the HIR types to construct the diagnostic message. let hir_id = tcx.hir().as_local_hir_id(field.did)?; diff --git a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs index 67b77605f3c92..e30938bc32659 100644 --- a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs +++ b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs @@ -589,7 +589,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { // Check the type for a trait object. return match ty.sty { // `&dyn Trait` - ty::TyKind::Ref(_, ty, _) if ty.is_trait() => true, + ty::Ref(_, ty, _) if ty.is_trait() => true, // `Box` _ if ty.is_box() && ty.boxed_ty().is_trait() => true, // `dyn Trait` diff --git a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs index 3773f1a40c792..917e383cae827 100644 --- a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs +++ b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs @@ -583,7 +583,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { (self.to_error_region(fr), self.to_error_region(outlived_fr)) { if let Some(ty::TyS { - sty: ty::TyKind::Opaque(did, substs), + sty: ty::Opaque(did, substs), .. }) = infcx .tcx diff --git a/src/librustc_mir/borrow_check/nll/type_check/mod.rs b/src/librustc_mir/borrow_check/nll/type_check/mod.rs index b06ebbdbf34f6..ec5637d17072d 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -39,7 +39,7 @@ use rustc::traits::{ObligationCause, PredicateObligations}; use rustc::ty::fold::TypeFoldable; use rustc::ty::subst::{Subst, SubstsRef, UnpackedKind, UserSubsts}; use rustc::ty::{ - self, RegionVid, ToPolyTraitRef, Ty, TyCtxt, TyKind, UserType, + self, RegionVid, ToPolyTraitRef, Ty, TyCtxt, UserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, UserTypeAnnotationIndex, }; @@ -746,7 +746,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> { let (variant, substs) = match base_ty { PlaceTy { ty, variant_index: Some(variant_index) } => { match ty.sty { - ty::TyKind::Adt(adt_def, substs) => (&adt_def.variants[variant_index], substs), + ty::Adt(adt_def, substs) => (&adt_def.variants[variant_index], substs), _ => bug!("can't have downcast of non-adt type"), } } @@ -1136,7 +1136,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { category: ConstraintCategory, ) -> Fallible<()> { if let Err(terr) = self.sub_types(sub, sup, locations, category) { - if let TyKind::Opaque(..) = sup.sty { + if let ty::Opaque(..) = sup.sty { // When you have `let x: impl Foo = ...` in a closure, // the resulting inferend values are stored with the // def-id of the base function. @@ -1389,7 +1389,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { } => { let place_type = place.ty(mir, tcx).ty; let adt = match place_type.sty { - TyKind::Adt(adt, _) if adt.is_enum() => adt, + ty::Adt(adt, _) if adt.is_enum() => adt, _ => { span_bug!( stmt.source_info.span, diff --git a/src/librustc_mir/dataflow/move_paths/builder.rs b/src/librustc_mir/dataflow/move_paths/builder.rs index 011dc54f3b315..2471c01e3f3d0 100644 --- a/src/librustc_mir/dataflow/move_paths/builder.rs +++ b/src/librustc_mir/dataflow/move_paths/builder.rs @@ -425,7 +425,7 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> { base, elem: ProjectionElem::Field(_, _), }) if match base.ty(self.builder.mir, self.builder.tcx).ty.sty { - ty::TyKind::Adt(def, _) if def.is_union() => true, + ty::Adt(def, _) if def.is_union() => true, _ => false, } => base, // Otherwise, lookup the place. diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 303ffcb3bfb3a..a9c521f59a96c 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -1754,7 +1754,7 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>( // they should be pointing to memory is when they are subslices of nonzero // slices let (opt_ptr, n, ty) = match value.ty.sty { - ty::TyKind::Array(t, n) => { + ty::Array(t, n) => { match value.val { ConstValue::ByRef(ptr, alloc) => ( Some((ptr, alloc)), @@ -1767,7 +1767,7 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>( ), } }, - ty::TyKind::Slice(t) => { + ty::Slice(t) => { match value.val { ConstValue::Slice(ptr, n) => ( ptr.to_ptr().ok().map(|ptr| ( diff --git a/src/librustc_mir/hair/pattern/check_match.rs b/src/librustc_mir/hair/pattern/check_match.rs index 50df676aea9fb..7ded973701edc 100644 --- a/src/librustc_mir/hair/pattern/check_match.rs +++ b/src/librustc_mir/hair/pattern/check_match.rs @@ -10,7 +10,7 @@ use rustc::middle::expr_use_visitor as euv; use rustc::middle::mem_categorization::cmt_; use rustc::middle::region; use rustc::session::Session; -use rustc::ty::{self, Ty, TyCtxt, TyKind}; +use rustc::ty::{self, Ty, TyCtxt}; use rustc::ty::subst::{InternalSubsts, SubstsRef}; use rustc::lint; use rustc_errors::{Applicability, DiagnosticBuilder}; @@ -481,7 +481,7 @@ fn check_exhaustive<'p, 'a: 'p, 'tcx: 'a>( } let patterns = witnesses.iter().map(|p| (**p).clone()).collect::>>(); if patterns.len() < 4 { - for sp in maybe_point_at_variant(cx, &scrut_ty.sty, patterns.as_slice()) { + for sp in maybe_point_at_variant(cx, scrut_ty, patterns.as_slice()) { err.span_label(sp, "not covered"); } } @@ -498,11 +498,11 @@ fn check_exhaustive<'p, 'a: 'p, 'tcx: 'a>( fn maybe_point_at_variant( cx: &mut MatchCheckCtxt<'a, 'tcx>, - sty: &TyKind<'tcx>, + ty: Ty<'tcx>, patterns: &[Pattern<'_>], ) -> Vec { let mut covered = vec![]; - if let ty::Adt(def, _) = sty { + if let ty::Adt(def, _) = ty.sty { // Don't point at variants that have already been covered due to other patterns to avoid // visual clutter for pattern in patterns { @@ -518,7 +518,7 @@ fn maybe_point_at_variant( .map(|field_pattern| field_pattern.pattern.clone()) .collect::>(); covered.extend( - maybe_point_at_variant(cx, sty, subpatterns.as_slice()), + maybe_point_at_variant(cx, ty, subpatterns.as_slice()), ); } } @@ -526,7 +526,7 @@ fn maybe_point_at_variant( let subpatterns = subpatterns.iter() .map(|field_pattern| field_pattern.pattern.clone()) .collect::>(); - covered.extend(maybe_point_at_variant(cx, sty, subpatterns.as_slice())); + covered.extend(maybe_point_at_variant(cx, ty, subpatterns.as_slice())); } } } diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index c45e694ebf832..deeed9a0b9846 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -28,6 +28,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! #![recursion_limit="256"] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![allow(explicit_outlives_requirements)] #[macro_use] extern crate log; diff --git a/src/librustc_mir/lints.rs b/src/librustc_mir/lints.rs index 6d6a3f9147269..572f7133cad84 100644 --- a/src/librustc_mir/lints.rs +++ b/src/librustc_mir/lints.rs @@ -4,7 +4,7 @@ use rustc::hir::intravisit::FnKind; use rustc::hir::map::blocks::FnLikeNode; use rustc::lint::builtin::UNCONDITIONAL_RECURSION; use rustc::mir::{self, Mir, TerminatorKind}; -use rustc::ty::{AssociatedItem, AssociatedItemContainer, Instance, TyCtxt, TyKind}; +use rustc::ty::{self, AssociatedItem, AssociatedItemContainer, Instance, TyCtxt}; use rustc::ty::subst::InternalSubsts; pub fn check(tcx: TyCtxt<'a, 'tcx, 'tcx>, @@ -86,7 +86,7 @@ fn check_fn_for_unconditional_recursion(tcx: TyCtxt<'a, 'tcx, 'tcx>, TerminatorKind::Call { ref func, .. } => { let func_ty = func.ty(mir, tcx); - if let TyKind::FnDef(fn_def_id, substs) = func_ty.sty { + if let ty::FnDef(fn_def_id, substs) = func_ty.sty { let (call_fn_id, call_substs) = if let Some(instance) = Instance::resolve(tcx, param_env, diff --git a/src/librustc_mir/transform/instcombine.rs b/src/librustc_mir/transform/instcombine.rs index 7e925f65ee2d7..8187a81f0edab 100644 --- a/src/librustc_mir/transform/instcombine.rs +++ b/src/librustc_mir/transform/instcombine.rs @@ -2,7 +2,7 @@ use rustc::mir::{Constant, Location, Place, PlaceBase, Mir, Operand, ProjectionElem, Rvalue, Local}; use rustc::mir::visit::{MutVisitor, Visitor}; -use rustc::ty::{TyCtxt, TyKind}; +use rustc::ty::{self, TyCtxt}; use rustc::util::nodemap::{FxHashMap, FxHashSet}; use rustc_data_structures::indexed_vec::Idx; use std::mem; @@ -90,7 +90,7 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for OptimizationFinder<'b, 'a, 'tcx> { if let Rvalue::Len(ref place) = *rvalue { let place_ty = place.ty(&self.mir.local_decls, self.tcx).ty; - if let TyKind::Array(_, len) = place_ty.sty { + if let ty::Array(_, len) = place_ty.sty { let span = self.mir.source_info(location).span; let ty = self.tcx.types.usize; let constant = Constant { span, ty, literal: len, user_ty: None }; diff --git a/src/librustc_mir/transform/lower_128bit.rs b/src/librustc_mir/transform/lower_128bit.rs index dda9457cc8ccf..fd9d6bb5760b1 100644 --- a/src/librustc_mir/transform/lower_128bit.rs +++ b/src/librustc_mir/transform/lower_128bit.rs @@ -3,7 +3,7 @@ use rustc::hir::def_id::DefId; use rustc::middle::lang_items::LangItem; use rustc::mir::*; -use rustc::ty::{List, Ty, TyCtxt, TyKind}; +use rustc::ty::{self, List, Ty, TyCtxt}; use rustc_data_structures::indexed_vec::{Idx}; use crate::transform::{MirPass, MirSource}; @@ -183,8 +183,8 @@ impl RhsKind { fn sign_of_128bit(ty: Ty<'_>) -> Option { match ty.sty { - TyKind::Int(syntax::ast::IntTy::I128) => Some(true), - TyKind::Uint(syntax::ast::UintTy::U128) => Some(false), + ty::Int(syntax::ast::IntTy::I128) => Some(true), + ty::Uint(syntax::ast::UintTy::U128) => Some(false), _ => None, } } From 4d2a3bb13bb04d3ad6c0a3add4883c64200fa96c Mon Sep 17 00:00:00 2001 From: flip1995 Date: Mon, 1 Apr 2019 00:10:48 +0200 Subject: [PATCH 18/22] Deny internal lints on librustc_typeck --- src/librustc_typeck/astconv.rs | 1 + src/librustc_typeck/check/wfcheck.rs | 4 ++-- src/librustc_typeck/lib.rs | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index fba4414c1275c..fba6a607eef06 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -102,6 +102,7 @@ enum GenericArgPosition { /// Dummy type used for the `Self` of a `TraitRef` created for converting /// a trait object, and which gets removed in `ExistentialTraitRef`. /// This type must not appear anywhere in other converted types. +#[cfg_attr(not(stage0), allow(usage_of_ty_tykind))] const TRAIT_OBJECT_DUMMY_SELF: ty::TyKind<'static> = ty::Infer(ty::FreshTy(0)); impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 3579810b8d75f..d108e7c3107af 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -3,7 +3,7 @@ use crate::constrained_generic_params::{identify_constrained_generic_params, Par use crate::hir::def_id::DefId; use rustc::traits::{self, ObligationCauseCode}; -use rustc::ty::{self, Lift, Ty, TyCtxt, TyKind, GenericParamDefKind, TypeFoldable, ToPredicate}; +use rustc::ty::{self, Lift, Ty, TyCtxt, GenericParamDefKind, TypeFoldable, ToPredicate}; use rustc::ty::subst::{Subst, InternalSubsts}; use rustc::util::nodemap::{FxHashSet, FxHashMap}; use rustc::mir::interpret::ConstValue; @@ -354,7 +354,7 @@ fn check_item_type<'a, 'tcx>( let mut forbid_unsized = true; if allow_foreign_ty { - if let TyKind::Foreign(_) = fcx.tcx.struct_tail(item_ty).sty { + if let ty::Foreign(_) = fcx.tcx.struct_tail(item_ty).sty { forbid_unsized = false; } } diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 1fa16352b867a..4a7b1e67366e8 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -71,6 +71,7 @@ This API is completely unstable and subject to change. #![recursion_limit="256"] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![allow(explicit_outlives_requirements)] #[macro_use] extern crate log; From dd7483c750945b554e2d18f0d54674d32b054b85 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 2 Apr 2019 16:57:12 +0200 Subject: [PATCH 19/22] Remove TyKind arg from report_bin_hex_error function --- src/librustc_lint/types.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index fa106532662d1..c82985b549e8e 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -104,7 +104,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { report_bin_hex_error( cx, e, - ty::Int(t), + attr::IntType::SignedInt(t), repr_str, v, negative, @@ -159,7 +159,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { report_bin_hex_error( cx, e, - ty::Uint(t), + attr::IntType::UnsignedInt(t), repr_str, lit_val, false, @@ -364,29 +364,24 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { } } - #[cfg_attr(not(stage0), allow(usage_of_ty_tykind))] fn report_bin_hex_error( cx: &LateContext<'_, '_>, expr: &hir::Expr, - ty: ty::TyKind<'_>, + ty: attr::IntType, repr_str: String, val: u128, negative: bool, ) { + let size = layout::Integer::from_attr(&cx.tcx, ty).size(); let (t, actually) = match ty { - ty::Int(t) => { - let ity = attr::IntType::SignedInt(t); - let size = layout::Integer::from_attr(&cx.tcx, ity).size(); + attr::IntType::SignedInt(t) => { let actually = sign_extend(val, size) as i128; (format!("{:?}", t), actually.to_string()) } - ty::Uint(t) => { - let ity = attr::IntType::UnsignedInt(t); - let size = layout::Integer::from_attr(&cx.tcx, ity).size(); + attr::IntType::UnsignedInt(t) => { let actually = truncate(val, size); (format!("{:?}", t), actually.to_string()) } - _ => bug!(), }; let mut err = cx.struct_span_lint( OVERFLOWING_LITERALS, From 51a792d01b3af2c96850d3be9392139a592931dd Mon Sep 17 00:00:00 2001 From: flip1995 Date: Wed, 3 Apr 2019 11:46:40 +0200 Subject: [PATCH 20/22] Add trait_object_dummy_self to CommonTypes --- src/librustc/ty/context.rs | 7 +++++++ src/librustc_typeck/astconv.rs | 20 ++++++++------------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 03936a82c7151..aa5610739fd6d 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -219,6 +219,11 @@ pub struct CommonTypes<'tcx> { pub never: Ty<'tcx>, pub err: Ty<'tcx>, + /// Dummy type used for the `Self` of a `TraitRef` created for converting + /// a trait object, and which gets removed in `ExistentialTraitRef`. + /// This type must not appear anywhere in other converted types. + pub trait_object_dummy_self: Ty<'tcx>, + pub re_empty: Region<'tcx>, pub re_static: Region<'tcx>, pub re_erased: Region<'tcx>, @@ -955,6 +960,8 @@ impl<'tcx> CommonTypes<'tcx> { f32: mk(Float(ast::FloatTy::F32)), f64: mk(Float(ast::FloatTy::F64)), + trait_object_dummy_self: mk(Infer(ty::FreshTy(0))), + re_empty: mk_region(RegionKind::ReEmpty), re_static: mk_region(RegionKind::ReStatic), re_erased: mk_region(RegionKind::ReErased), diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index fba6a607eef06..ab5c2fee0ae0b 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -99,12 +99,6 @@ enum GenericArgPosition { MethodCall, } -/// Dummy type used for the `Self` of a `TraitRef` created for converting -/// a trait object, and which gets removed in `ExistentialTraitRef`. -/// This type must not appear anywhere in other converted types. -#[cfg_attr(not(stage0), allow(usage_of_ty_tykind))] -const TRAIT_OBJECT_DUMMY_SELF: ty::TyKind<'static> = ty::Infer(ty::FreshTy(0)); - impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { pub fn ast_region_to_region(&self, lifetime: &hir::Lifetime, @@ -596,7 +590,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { infer_types, ); - let is_object = self_ty.map_or(false, |ty| ty.sty == TRAIT_OBJECT_DUMMY_SELF); + let is_object = self_ty.map_or(false, |ty| { + ty.sty == self.tcx().types.trait_object_dummy_self.sty + }); let default_needs_object_self = |param: &ty::GenericParamDef| { if let GenericParamDefKind::Type { has_default, .. } = param.kind { if is_object && has_default { @@ -957,10 +953,10 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { } /// Transform a `PolyTraitRef` into a `PolyExistentialTraitRef` by - /// removing the dummy `Self` type (`TRAIT_OBJECT_DUMMY_SELF`). + /// removing the dummy `Self` type (`trait_object_dummy_self`). fn trait_ref_to_existential(&self, trait_ref: ty::TraitRef<'tcx>) -> ty::ExistentialTraitRef<'tcx> { - if trait_ref.self_ty().sty != TRAIT_OBJECT_DUMMY_SELF { + if trait_ref.self_ty().sty != self.tcx().types.trait_object_dummy_self.sty { bug!("trait_ref_to_existential called on {:?} with non-dummy Self", trait_ref); } ty::ExistentialTraitRef::erase_self_ty(self.tcx(), trait_ref) @@ -981,7 +977,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { } let mut projection_bounds = Vec::new(); - let dummy_self = tcx.mk_ty(TRAIT_OBJECT_DUMMY_SELF); + let dummy_self = self.tcx().types.trait_object_dummy_self; let (principal, potential_assoc_types) = self.instantiate_poly_trait_ref( &trait_bounds[0], dummy_self, @@ -1031,7 +1027,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { } ty::Predicate::Projection(pred) => { // A `Self` within the original bound will be substituted with a - // `TRAIT_OBJECT_DUMMY_SELF`, so check for that. + // `trait_object_dummy_self`, so check for that. let references_self = pred.skip_binder().ty.walk().any(|t| t == dummy_self); @@ -1131,7 +1127,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { err.emit(); } - // Erase the `dummy_self` (`TRAIT_OBJECT_DUMMY_SELF`) used above. + // Erase the `dummy_self` (`trait_object_dummy_self`) used above. let existential_principal = principal.map_bound(|trait_ref| { self.trait_ref_to_existential(trait_ref) }); From 076abfa0f30e2365f4dbb6cd941c7761157b644a Mon Sep 17 00:00:00 2001 From: flip1995 Date: Wed, 3 Apr 2019 15:53:36 +0200 Subject: [PATCH 21/22] Deny internal lints on two more crates - libfmt_macros - librustdoc --- src/libfmt_macros/lib.rs | 1 + src/librustdoc/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index aacd6cec565a5..2536121c7a324 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -9,6 +9,7 @@ test(attr(deny(warnings))))] #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![feature(nll)] #![feature(rustc_private)] diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 2ebb465d53dbe..6cb937d9216ac 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -1,4 +1,5 @@ #![deny(rust_2018_idioms)] +#![cfg_attr(not(stage0), deny(internal))] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/", html_playground_url = "https://play.rust-lang.org/")] From c81ce069b4ea7586d3c15df4a45fd43159e1a0fa Mon Sep 17 00:00:00 2001 From: flip1995 Date: Wed, 3 Apr 2019 16:08:04 +0200 Subject: [PATCH 22/22] Compare `Ty`s directly instead of their `TyKind`s --- src/librustc_typeck/astconv.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index ab5c2fee0ae0b..8805dade40e4c 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -591,7 +591,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { ); let is_object = self_ty.map_or(false, |ty| { - ty.sty == self.tcx().types.trait_object_dummy_self.sty + ty == self.tcx().types.trait_object_dummy_self }); let default_needs_object_self = |param: &ty::GenericParamDef| { if let GenericParamDefKind::Type { has_default, .. } = param.kind { @@ -956,7 +956,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { /// removing the dummy `Self` type (`trait_object_dummy_self`). fn trait_ref_to_existential(&self, trait_ref: ty::TraitRef<'tcx>) -> ty::ExistentialTraitRef<'tcx> { - if trait_ref.self_ty().sty != self.tcx().types.trait_object_dummy_self.sty { + if trait_ref.self_ty() != self.tcx().types.trait_object_dummy_self { bug!("trait_ref_to_existential called on {:?} with non-dummy Self", trait_ref); } ty::ExistentialTraitRef::erase_self_ty(self.tcx(), trait_ref)