rustc/middle: use Cow<'static, str> where applicable

This commit is contained in:
ljedrz 2018-10-02 18:16:01 +02:00
parent c2b3aa9939
commit a256a66305
3 changed files with 41 additions and 38 deletions

View File

@ -83,6 +83,7 @@
use syntax::ast::{self, Name};
use syntax_pos::Span;
use std::borrow::Cow;
use std::fmt;
use std::hash::{Hash, Hasher};
use rustc_data_structures::sync::Lrc;
@ -1489,59 +1490,59 @@ pub fn upvar_cat(&self) -> Option<&Categorization<'tcx>> {
}
}
pub fn descriptive_string(&self, tcx: TyCtxt<'_, '_, '_>) -> String {
pub fn descriptive_string(&self, tcx: TyCtxt<'_, '_, '_>) -> Cow<'static, str> {
match self.cat {
Categorization::StaticItem => {
"static item".to_string()
"static item".into()
}
Categorization::Rvalue(..) => {
"non-place".to_string()
"non-place".into()
}
Categorization::Local(vid) => {
if tcx.hir.is_argument(vid) {
"argument".to_string()
"argument"
} else {
"local variable".to_string()
}
"local variable"
}.into()
}
Categorization::Deref(_, pk) => {
match self.upvar_cat() {
Some(&Categorization::Upvar(ref var)) => {
var.to_string()
var.to_string().into()
}
Some(_) => bug!(),
None => {
match pk {
Unique => {
"`Box` content".to_string()
"`Box` content"
}
UnsafePtr(..) => {
"dereference of raw pointer".to_string()
"dereference of raw pointer"
}
BorrowedPtr(..) => {
match self.note {
NoteIndex => "indexed content".to_string(),
_ => "borrowed content".to_string(),
NoteIndex => "indexed content",
_ => "borrowed content"
}
}
}
}.into()
}
}
}
Categorization::Interior(_, InteriorField(..)) => {
"field".to_string()
"field".into()
}
Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Index)) => {
"indexed content".to_string()
"indexed content".into()
}
Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Pattern)) => {
"pattern-bound indexed content".to_string()
"pattern-bound indexed content".into()
}
Categorization::Upvar(ref var) => {
var.to_string()
var.to_string().into()
}
Categorization::Downcast(ref cmt, _) => {
cmt.descriptive_string(tcx)
cmt.descriptive_string(tcx).into()
}
}
}

View File

@ -25,6 +25,7 @@
use rustc::lint;
use rustc_data_structures::sync::Lrc;
use session::Session;
use std::borrow::Cow;
use std::cell::Cell;
use std::mem::replace;
use syntax::ast;
@ -1250,8 +1251,8 @@ fn compute_object_lifetime_defaults(
let object_lifetime_default_reprs: String = result
.iter()
.map(|set| match *set {
Set1::Empty => "BaseDefault".to_string(),
Set1::One(Region::Static) => "'static".to_string(),
Set1::Empty => "BaseDefault".into(),
Set1::One(Region::Static) => "'static".into(),
Set1::One(Region::EarlyBound(mut i, _, _)) => {
generics.params.iter().find_map(|param| match param.kind {
GenericParamKind::Lifetime { .. } => {
@ -1265,9 +1266,9 @@ fn compute_object_lifetime_defaults(
}).unwrap()
}
Set1::One(_) => bug!(),
Set1::Many => "Ambiguous".to_string(),
Set1::Many => "Ambiguous".into(),
})
.collect::<Vec<String>>()
.collect::<Vec<Cow<'static, str>>>()
.join(",");
tcx.sess.span_err(item.span, &object_lifetime_default_reprs);
}
@ -2652,10 +2653,10 @@ pub fn report_missing_lifetime_specifiers(
if count > 1 { "s" } else { "" }
);
let msg = if count > 1 {
format!("expected {} lifetime parameters", count)
let msg: Cow<'static, str> = if count > 1 {
format!("expected {} lifetime parameters", count).into()
} else {
"expected lifetime parameter".to_string()
"expected lifetime parameter".into()
};
err.span_label(span, msg);

View File

@ -38,6 +38,7 @@
use rustc_mir::util::suggest_ref_mut;
use rustc::util::nodemap::FxHashSet;
use std::borrow::Cow;
use std::cell::{Cell, RefCell};
use std::fmt;
use std::rc::Rc;
@ -808,34 +809,34 @@ fn report_bckerr(&self, err: &BckError<'a, 'tcx>) {
match err.code {
err_mutbl => {
let descr = match err.cmt.note {
let descr: Cow<'static, str> = match err.cmt.note {
mc::NoteClosureEnv(_) | mc::NoteUpvarRef(_) => {
self.cmt_to_string(&err.cmt)
self.cmt_to_cow_str(&err.cmt)
}
_ => match opt_loan_path_is_field(&err.cmt) {
(None, true) => {
format!("{} of {} binding",
self.cmt_to_string(&err.cmt),
err.cmt.mutbl.to_user_str())
self.cmt_to_cow_str(&err.cmt),
err.cmt.mutbl.to_user_str()).into()
}
(None, false) => {
format!("{} {}",
err.cmt.mutbl.to_user_str(),
self.cmt_to_string(&err.cmt))
self.cmt_to_cow_str(&err.cmt)).into()
}
(Some(lp), true) => {
format!("{} `{}` of {} binding",
self.cmt_to_string(&err.cmt),
self.cmt_to_cow_str(&err.cmt),
self.loan_path_to_string(&lp),
err.cmt.mutbl.to_user_str())
err.cmt.mutbl.to_user_str()).into()
}
(Some(lp), false) => {
format!("{} {} `{}`",
err.cmt.mutbl.to_user_str(),
self.cmt_to_string(&err.cmt),
self.loan_path_to_string(&lp))
self.cmt_to_cow_str(&err.cmt),
self.loan_path_to_string(&lp)).into()
}
}
};
@ -1058,11 +1059,11 @@ fn report_bckerr(&self, err: &BckError<'a, 'tcx>) {
err_borrowed_pointer_too_short(loan_scope, ptr_scope) => {
let descr = self.cmt_to_path_or_string(err.cmt);
let mut db = self.lifetime_too_short_for_reborrow(error_span, &descr, Origin::Ast);
let descr = match opt_loan_path(&err.cmt) {
let descr: Cow<'static, str> = match opt_loan_path(&err.cmt) {
Some(lp) => {
format!("`{}`", self.loan_path_to_string(&lp))
format!("`{}`", self.loan_path_to_string(&lp)).into()
}
None => self.cmt_to_string(&err.cmt),
None => self.cmt_to_cow_str(&err.cmt)
};
self.tcx.note_and_explain_region(
&self.region_scope_tree,
@ -1477,14 +1478,14 @@ pub fn loan_path_to_string(&self, loan_path: &LoanPath<'tcx>) -> String {
result
}
pub fn cmt_to_string(&self, cmt: &mc::cmt_<'tcx>) -> String {
pub fn cmt_to_cow_str(&self, cmt: &mc::cmt_<'tcx>) -> Cow<'static, str> {
cmt.descriptive_string(self.tcx)
}
pub fn cmt_to_path_or_string(&self, cmt: &mc::cmt_<'tcx>) -> String {
match opt_loan_path(cmt) {
Some(lp) => format!("`{}`", self.loan_path_to_string(&lp)),
None => self.cmt_to_string(cmt),
None => self.cmt_to_cow_str(cmt).into_owned(),
}
}
}