Use LocalInternedString inside of AbsolutePathPrinter

This commit is contained in:
flip1995
2019-03-16 11:45:29 +01:00
parent dae5c9c685
commit 254fad95de
2 changed files with 14 additions and 11 deletions

View File

@@ -245,7 +245,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
let def = self.tables.qpath_def(qpath, callee.hir_id); let def = self.tables.qpath_def(qpath, callee.hir_id);
if let Some(def_id) = def.opt_def_id(); if let Some(def_id) = def.opt_def_id();
let def_path = get_def_path(self.tcx, def_id); let def_path = get_def_path(self.tcx, def_id);
if let &["core", "num", impl_ty, "max_value"] = &def_path.iter().map(|s| s.as_str()).collect::<Vec<_>>()[..]; if let &["core", "num", impl_ty, "max_value"] = &def_path[..];
then { then {
let value = match impl_ty { let value = match impl_ty {
"<impl i8>" => i8::max_value() as u128, "<impl i8>" => i8::max_value() as u128,

View File

@@ -43,7 +43,7 @@ use rustc_errors::Applicability;
use syntax::ast::{self, LitKind}; use syntax::ast::{self, LitKind};
use syntax::attr; use syntax::attr;
use syntax::source_map::{Span, DUMMY_SP}; use syntax::source_map::{Span, DUMMY_SP};
use syntax::symbol::{keywords, Symbol}; use syntax::symbol::{keywords, Symbol, LocalInternedString};
use crate::reexport::*; use crate::reexport::*;
@@ -107,7 +107,7 @@ use rustc::ty::print::Printer;
impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
type Error = !; type Error = !;
type Path = Vec<String>; type Path = Vec<LocalInternedString>;
type Region = (); type Region = ();
type Type = (); type Type = ();
type DynExistential = (); type DynExistential = ();
@@ -141,8 +141,9 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
self, self,
cnum: CrateNum, cnum: CrateNum,
) -> Result<Self::Path, Self::Error> { ) -> Result<Self::Path, Self::Error> {
Ok(vec![self.tcx.original_crate_name(cnum).to_string()]) Ok(vec![self.tcx.original_crate_name(cnum).as_str()])
} }
fn path_qualified( fn path_qualified(
self, self,
self_ty: Ty<'tcx>, self_ty: Ty<'tcx>,
@@ -150,8 +151,8 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
) -> Result<Self::Path, Self::Error> { ) -> Result<Self::Path, Self::Error> {
// This shouldn't ever be needed, but just in case: // This shouldn't ever be needed, but just in case:
Ok(vec![match trait_ref { Ok(vec![match trait_ref {
Some(trait_ref) => format!("{:?}", trait_ref), Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(),
None => format!("<{}>", self_ty), None => Symbol::intern(&format!("<{}>", self_ty)).as_str(),
}]) }])
} }
@@ -167,22 +168,24 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
// This shouldn't ever be needed, but just in case: // This shouldn't ever be needed, but just in case:
path.push(match trait_ref { path.push(match trait_ref {
Some(trait_ref) => { Some(trait_ref) => {
format!("<impl {} for {}>", trait_ref, self_ty) Symbol::intern(&format!("<impl {} for {}>", trait_ref, self_ty)).as_str()
} }
None => format!("<impl {}>", self_ty), None => Symbol::intern(&format!("<impl {}>", self_ty)).as_str(),
}); });
Ok(path) Ok(path)
} }
fn path_append( fn path_append(
self, self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>, print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData, disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> { ) -> Result<Self::Path, Self::Error> {
let mut path = print_prefix(self)?; let mut path = print_prefix(self)?;
path.push(disambiguated_data.data.as_interned_str().to_string()); path.push(disambiguated_data.data.as_interned_str().as_str());
Ok(path) Ok(path)
} }
fn path_generic_args( fn path_generic_args(
self, self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>, print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
@@ -215,8 +218,8 @@ pub fn match_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, path
/// // The given `def_id` is that of an `Option` type /// // The given `def_id` is that of an `Option` type
/// }; /// };
/// ``` /// ```
pub fn get_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Vec<String> { pub fn get_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Vec<&'static str> {
AbsolutePathPrinter { tcx }.print_def_path(def_id, &[]).unwrap() AbsolutePathPrinter { tcx }.print_def_path(def_id, &[]).unwrap().iter().map(LocalInternedString::get).collect()
} }
/// Checks if type is struct, enum or union type with the given def path. /// Checks if type is struct, enum or union type with the given def path.