Skip to content

Commit 30f6ee7

Browse files
committed
Move LateContext methods to librustc_lint.
1 parent bdee6b8 commit 30f6ee7

File tree

4 files changed

+181
-149
lines changed

4 files changed

+181
-149
lines changed

src/librustc/lint/context.rs

Lines changed: 1 addition & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
use self::TargetLint::*;
1818

1919
use crate::hir;
20-
use crate::hir::def_id::{CrateNum, DefId};
21-
use crate::hir::map::{definitions::DisambiguatedDefPathData, DefPathData};
2220
use crate::lint::builtin::BuiltinLintDiagnostics;
2321
use crate::lint::levels::{LintLevelSets, LintLevelsBuilder};
2422
use crate::lint::{EarlyLintPassObject, LateLintPassObject};
2523
use crate::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
2624
use crate::middle::privacy::AccessLevels;
2725
use crate::session::Session;
2826
use crate::ty::layout::{LayoutError, LayoutOf, TyLayout};
29-
use crate::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
27+
use crate::ty::{self, Ty, TyCtxt};
3028
use crate::util::nodemap::FxHashMap;
3129

3230
use errors::DiagnosticBuilder;
@@ -619,150 +617,6 @@ impl LintContext for EarlyContext<'_> {
619617
}
620618
}
621619

622-
impl<'a, 'tcx> LateContext<'a, 'tcx> {
623-
pub fn current_lint_root(&self) -> hir::HirId {
624-
self.last_node_with_lint_attrs
625-
}
626-
627-
/// Check if a `DefId`'s path matches the given absolute type path usage.
628-
///
629-
/// Anonymous scopes such as `extern` imports are matched with `kw::Invalid`;
630-
/// inherent `impl` blocks are matched with the name of the type.
631-
///
632-
/// # Examples
633-
///
634-
/// ```rust,ignore (no context or def id available)
635-
/// if cx.match_def_path(def_id, &[sym::core, sym::option, sym::Option]) {
636-
/// // The given `def_id` is that of an `Option` type
637-
/// }
638-
/// ```
639-
pub fn match_def_path(&self, def_id: DefId, path: &[Symbol]) -> bool {
640-
let names = self.get_def_path(def_id);
641-
642-
names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| a == b)
643-
}
644-
645-
/// Gets the absolute path of `def_id` as a vector of `Symbol`.
646-
///
647-
/// # Examples
648-
///
649-
/// ```rust,ignore (no context or def id available)
650-
/// let def_path = cx.get_def_path(def_id);
651-
/// if let &[sym::core, sym::option, sym::Option] = &def_path[..] {
652-
/// // The given `def_id` is that of an `Option` type
653-
/// }
654-
/// ```
655-
pub fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
656-
pub struct AbsolutePathPrinter<'tcx> {
657-
pub tcx: TyCtxt<'tcx>,
658-
}
659-
660-
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
661-
type Error = !;
662-
663-
type Path = Vec<Symbol>;
664-
type Region = ();
665-
type Type = ();
666-
type DynExistential = ();
667-
type Const = ();
668-
669-
fn tcx(&self) -> TyCtxt<'tcx> {
670-
self.tcx
671-
}
672-
673-
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
674-
Ok(())
675-
}
676-
677-
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
678-
Ok(())
679-
}
680-
681-
fn print_dyn_existential(
682-
self,
683-
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
684-
) -> Result<Self::DynExistential, Self::Error> {
685-
Ok(())
686-
}
687-
688-
fn print_const(self, _ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
689-
Ok(())
690-
}
691-
692-
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
693-
Ok(vec![self.tcx.original_crate_name(cnum)])
694-
}
695-
696-
fn path_qualified(
697-
self,
698-
self_ty: Ty<'tcx>,
699-
trait_ref: Option<ty::TraitRef<'tcx>>,
700-
) -> Result<Self::Path, Self::Error> {
701-
if trait_ref.is_none() {
702-
if let ty::Adt(def, substs) = self_ty.kind {
703-
return self.print_def_path(def.did, substs);
704-
}
705-
}
706-
707-
// This shouldn't ever be needed, but just in case:
708-
Ok(vec![match trait_ref {
709-
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)),
710-
None => Symbol::intern(&format!("<{}>", self_ty)),
711-
}])
712-
}
713-
714-
fn path_append_impl(
715-
self,
716-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
717-
_disambiguated_data: &DisambiguatedDefPathData,
718-
self_ty: Ty<'tcx>,
719-
trait_ref: Option<ty::TraitRef<'tcx>>,
720-
) -> Result<Self::Path, Self::Error> {
721-
let mut path = print_prefix(self)?;
722-
723-
// This shouldn't ever be needed, but just in case:
724-
path.push(match trait_ref {
725-
Some(trait_ref) => Symbol::intern(&format!(
726-
"<impl {} for {}>",
727-
trait_ref.print_only_trait_path(),
728-
self_ty
729-
)),
730-
None => Symbol::intern(&format!("<impl {}>", self_ty)),
731-
});
732-
733-
Ok(path)
734-
}
735-
736-
fn path_append(
737-
self,
738-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
739-
disambiguated_data: &DisambiguatedDefPathData,
740-
) -> Result<Self::Path, Self::Error> {
741-
let mut path = print_prefix(self)?;
742-
743-
// Skip `::{{constructor}}` on tuple/unit structs.
744-
match disambiguated_data.data {
745-
DefPathData::Ctor => return Ok(path),
746-
_ => {}
747-
}
748-
749-
path.push(disambiguated_data.data.as_symbol());
750-
Ok(path)
751-
}
752-
753-
fn path_generic_args(
754-
self,
755-
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
756-
_args: &[GenericArg<'tcx>],
757-
) -> Result<Self::Path, Self::Error> {
758-
print_prefix(self)
759-
}
760-
}
761-
762-
AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap()
763-
}
764-
}
765-
766620
impl<'a, 'tcx> LayoutOf for LateContext<'a, 'tcx> {
767621
type Ty = Ty<'tcx>;
768622
type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use syntax::visit::FnKind;
5151

5252
use rustc::hir::{self, GenericParamKind, PatKind};
5353

54+
use crate::late::LateContextExt;
5455
use crate::nonstandard_style::{method_context, MethodLateContext};
5556

5657
use log::debug;

src/librustc_lint/late.rs

Lines changed: 178 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,199 @@
1515
//! for all lint attributes.
1616
1717
use rustc::hir;
18-
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
18+
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1919
use rustc::hir::intravisit as hir_visit;
2020
use rustc::hir::intravisit::Visitor;
21+
use rustc::hir::map::{definitions::DisambiguatedDefPathData, DefPathData};
2122
use rustc::lint::LateContext;
2223
use rustc::lint::LintPass;
2324
use rustc::lint::{LateLintPass, LateLintPassObject};
24-
use rustc::ty::{self, TyCtxt};
25+
use rustc::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt};
2526
use rustc::util::common::time;
2627

2728
use rustc_data_structures::sync::{join, par_iter, ParallelIterator};
29+
use rustc_span::symbol::Symbol;
2830
use rustc_span::Span;
2931
use std::slice;
3032
use syntax::ast;
3133

3234
use log::debug;
3335
use syntax::walk_list;
3436

37+
pub(crate) trait LateContextExt {
38+
fn current_lint_root(&self) -> hir::HirId;
39+
40+
/// Check if a `DefId`'s path matches the given absolute type path usage.
41+
///
42+
/// Anonymous scopes such as `extern` imports are matched with `kw::Invalid`;
43+
/// inherent `impl` blocks are matched with the name of the type.
44+
///
45+
/// # Examples
46+
///
47+
/// ```rust,ignore (no context or def id available)
48+
/// if cx.match_def_path(def_id, &[sym::core, sym::option, sym::Option]) {
49+
/// // The given `def_id` is that of an `Option` type
50+
/// }
51+
/// ```
52+
fn match_def_path(&self, def_id: DefId, path: &[Symbol]) -> bool;
53+
54+
/// Gets the absolute path of `def_id` as a vector of `Symbol`.
55+
///
56+
/// # Examples
57+
///
58+
/// ```rust,ignore (no context or def id available)
59+
/// let def_path = cx.get_def_path(def_id);
60+
/// if let &[sym::core, sym::option, sym::Option] = &def_path[..] {
61+
/// // The given `def_id` is that of an `Option` type
62+
/// }
63+
/// ```
64+
fn get_def_path(&self, def_id: DefId) -> Vec<Symbol>;
65+
}
66+
67+
impl<'a, 'tcx> LateContextExt for LateContext<'a, 'tcx> {
68+
fn current_lint_root(&self) -> hir::HirId {
69+
self.last_node_with_lint_attrs
70+
}
71+
72+
/// Check if a `DefId`'s path matches the given absolute type path usage.
73+
///
74+
/// Anonymous scopes such as `extern` imports are matched with `kw::Invalid`;
75+
/// inherent `impl` blocks are matched with the name of the type.
76+
///
77+
/// # Examples
78+
///
79+
/// ```rust,ignore (no context or def id available)
80+
/// if cx.match_def_path(def_id, &[sym::core, sym::option, sym::Option]) {
81+
/// // The given `def_id` is that of an `Option` type
82+
/// }
83+
/// ```
84+
fn match_def_path(&self, def_id: DefId, path: &[Symbol]) -> bool {
85+
let names = self.get_def_path(def_id);
86+
87+
names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| a == b)
88+
}
89+
90+
/// Gets the absolute path of `def_id` as a vector of `Symbol`.
91+
///
92+
/// # Examples
93+
///
94+
/// ```rust,ignore (no context or def id available)
95+
/// let def_path = cx.get_def_path(def_id);
96+
/// if let &[sym::core, sym::option, sym::Option] = &def_path[..] {
97+
/// // The given `def_id` is that of an `Option` type
98+
/// }
99+
/// ```
100+
fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
101+
pub struct AbsolutePathPrinter<'tcx> {
102+
pub tcx: TyCtxt<'tcx>,
103+
}
104+
105+
impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
106+
type Error = !;
107+
108+
type Path = Vec<Symbol>;
109+
type Region = ();
110+
type Type = ();
111+
type DynExistential = ();
112+
type Const = ();
113+
114+
fn tcx(&self) -> TyCtxt<'tcx> {
115+
self.tcx
116+
}
117+
118+
fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
119+
Ok(())
120+
}
121+
122+
fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
123+
Ok(())
124+
}
125+
126+
fn print_dyn_existential(
127+
self,
128+
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
129+
) -> Result<Self::DynExistential, Self::Error> {
130+
Ok(())
131+
}
132+
133+
fn print_const(self, _ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
134+
Ok(())
135+
}
136+
137+
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
138+
Ok(vec![self.tcx.original_crate_name(cnum)])
139+
}
140+
141+
fn path_qualified(
142+
self,
143+
self_ty: Ty<'tcx>,
144+
trait_ref: Option<ty::TraitRef<'tcx>>,
145+
) -> Result<Self::Path, Self::Error> {
146+
if trait_ref.is_none() {
147+
if let ty::Adt(def, substs) = self_ty.kind {
148+
return self.print_def_path(def.did, substs);
149+
}
150+
}
151+
152+
// This shouldn't ever be needed, but just in case:
153+
Ok(vec![match trait_ref {
154+
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)),
155+
None => Symbol::intern(&format!("<{}>", self_ty)),
156+
}])
157+
}
158+
159+
fn path_append_impl(
160+
self,
161+
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
162+
_disambiguated_data: &DisambiguatedDefPathData,
163+
self_ty: Ty<'tcx>,
164+
trait_ref: Option<ty::TraitRef<'tcx>>,
165+
) -> Result<Self::Path, Self::Error> {
166+
let mut path = print_prefix(self)?;
167+
168+
// This shouldn't ever be needed, but just in case:
169+
path.push(match trait_ref {
170+
Some(trait_ref) => Symbol::intern(&format!(
171+
"<impl {} for {}>",
172+
trait_ref.print_only_trait_path(),
173+
self_ty
174+
)),
175+
None => Symbol::intern(&format!("<impl {}>", self_ty)),
176+
});
177+
178+
Ok(path)
179+
}
180+
181+
fn path_append(
182+
self,
183+
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
184+
disambiguated_data: &DisambiguatedDefPathData,
185+
) -> Result<Self::Path, Self::Error> {
186+
let mut path = print_prefix(self)?;
187+
188+
// Skip `::{{constructor}}` on tuple/unit structs.
189+
match disambiguated_data.data {
190+
DefPathData::Ctor => return Ok(path),
191+
_ => {}
192+
}
193+
194+
path.push(disambiguated_data.data.as_symbol());
195+
Ok(path)
196+
}
197+
198+
fn path_generic_args(
199+
self,
200+
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
201+
_args: &[GenericArg<'tcx>],
202+
) -> Result<Self::Path, Self::Error> {
203+
print_prefix(self)
204+
}
205+
}
206+
207+
AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap()
208+
}
209+
}
210+
35211
macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
36212
$cx.pass.$f(&$cx.context, $($args),*);
37213
}) }

src/librustc_lint/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(bool_to_option)]
1515
#![feature(box_patterns)]
1616
#![feature(box_syntax)]
17+
#![feature(never_type)]
1718
#![feature(nll)]
1819
#![recursion_limit = "256"]
1920

0 commit comments

Comments
 (0)