Skip to content

Commit a256a66

Browse files
committed
rustc/middle: use Cow<'static, str> where applicable
1 parent c2b3aa9 commit a256a66

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

src/librustc/middle/mem_categorization.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ use hir;
8383
use syntax::ast::{self, Name};
8484
use syntax_pos::Span;
8585

86+
use std::borrow::Cow;
8687
use std::fmt;
8788
use std::hash::{Hash, Hasher};
8889
use rustc_data_structures::sync::Lrc;
@@ -1489,59 +1490,59 @@ impl<'tcx> cmt_<'tcx> {
14891490
}
14901491
}
14911492

1492-
pub fn descriptive_string(&self, tcx: TyCtxt<'_, '_, '_>) -> String {
1493+
pub fn descriptive_string(&self, tcx: TyCtxt<'_, '_, '_>) -> Cow<'static, str> {
14931494
match self.cat {
14941495
Categorization::StaticItem => {
1495-
"static item".to_string()
1496+
"static item".into()
14961497
}
14971498
Categorization::Rvalue(..) => {
1498-
"non-place".to_string()
1499+
"non-place".into()
14991500
}
15001501
Categorization::Local(vid) => {
15011502
if tcx.hir.is_argument(vid) {
1502-
"argument".to_string()
1503+
"argument"
15031504
} else {
1504-
"local variable".to_string()
1505-
}
1505+
"local variable"
1506+
}.into()
15061507
}
15071508
Categorization::Deref(_, pk) => {
15081509
match self.upvar_cat() {
15091510
Some(&Categorization::Upvar(ref var)) => {
1510-
var.to_string()
1511+
var.to_string().into()
15111512
}
15121513
Some(_) => bug!(),
15131514
None => {
15141515
match pk {
15151516
Unique => {
1516-
"`Box` content".to_string()
1517+
"`Box` content"
15171518
}
15181519
UnsafePtr(..) => {
1519-
"dereference of raw pointer".to_string()
1520+
"dereference of raw pointer"
15201521
}
15211522
BorrowedPtr(..) => {
15221523
match self.note {
1523-
NoteIndex => "indexed content".to_string(),
1524-
_ => "borrowed content".to_string(),
1524+
NoteIndex => "indexed content",
1525+
_ => "borrowed content"
15251526
}
15261527
}
1527-
}
1528+
}.into()
15281529
}
15291530
}
15301531
}
15311532
Categorization::Interior(_, InteriorField(..)) => {
1532-
"field".to_string()
1533+
"field".into()
15331534
}
15341535
Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Index)) => {
1535-
"indexed content".to_string()
1536+
"indexed content".into()
15361537
}
15371538
Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Pattern)) => {
1538-
"pattern-bound indexed content".to_string()
1539+
"pattern-bound indexed content".into()
15391540
}
15401541
Categorization::Upvar(ref var) => {
1541-
var.to_string()
1542+
var.to_string().into()
15421543
}
15431544
Categorization::Downcast(ref cmt, _) => {
1544-
cmt.descriptive_string(tcx)
1545+
cmt.descriptive_string(tcx).into()
15451546
}
15461547
}
15471548
}

src/librustc/middle/resolve_lifetime.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use errors::DiagnosticBuilder;
2525
use rustc::lint;
2626
use rustc_data_structures::sync::Lrc;
2727
use session::Session;
28+
use std::borrow::Cow;
2829
use std::cell::Cell;
2930
use std::mem::replace;
3031
use syntax::ast;
@@ -1250,8 +1251,8 @@ fn compute_object_lifetime_defaults(
12501251
let object_lifetime_default_reprs: String = result
12511252
.iter()
12521253
.map(|set| match *set {
1253-
Set1::Empty => "BaseDefault".to_string(),
1254-
Set1::One(Region::Static) => "'static".to_string(),
1254+
Set1::Empty => "BaseDefault".into(),
1255+
Set1::One(Region::Static) => "'static".into(),
12551256
Set1::One(Region::EarlyBound(mut i, _, _)) => {
12561257
generics.params.iter().find_map(|param| match param.kind {
12571258
GenericParamKind::Lifetime { .. } => {
@@ -1265,9 +1266,9 @@ fn compute_object_lifetime_defaults(
12651266
}).unwrap()
12661267
}
12671268
Set1::One(_) => bug!(),
1268-
Set1::Many => "Ambiguous".to_string(),
1269+
Set1::Many => "Ambiguous".into(),
12691270
})
1270-
.collect::<Vec<String>>()
1271+
.collect::<Vec<Cow<'static, str>>>()
12711272
.join(",");
12721273
tcx.sess.span_err(item.span, &object_lifetime_default_reprs);
12731274
}
@@ -2652,10 +2653,10 @@ pub fn report_missing_lifetime_specifiers(
26522653
if count > 1 { "s" } else { "" }
26532654
);
26542655

2655-
let msg = if count > 1 {
2656-
format!("expected {} lifetime parameters", count)
2656+
let msg: Cow<'static, str> = if count > 1 {
2657+
format!("expected {} lifetime parameters", count).into()
26572658
} else {
2658-
"expected lifetime parameter".to_string()
2659+
"expected lifetime parameter".into()
26592660
};
26602661

26612662
err.span_label(span, msg);

src/librustc_borrowck/borrowck/mod.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_mir::util::borrowck_errors::{BorrowckErrors, Origin};
3838
use rustc_mir::util::suggest_ref_mut;
3939
use rustc::util::nodemap::FxHashSet;
4040

41+
use std::borrow::Cow;
4142
use std::cell::{Cell, RefCell};
4243
use std::fmt;
4344
use std::rc::Rc;
@@ -808,34 +809,34 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
808809

809810
match err.code {
810811
err_mutbl => {
811-
let descr = match err.cmt.note {
812+
let descr: Cow<'static, str> = match err.cmt.note {
812813
mc::NoteClosureEnv(_) | mc::NoteUpvarRef(_) => {
813-
self.cmt_to_string(&err.cmt)
814+
self.cmt_to_cow_str(&err.cmt)
814815
}
815816
_ => match opt_loan_path_is_field(&err.cmt) {
816817
(None, true) => {
817818
format!("{} of {} binding",
818-
self.cmt_to_string(&err.cmt),
819-
err.cmt.mutbl.to_user_str())
819+
self.cmt_to_cow_str(&err.cmt),
820+
err.cmt.mutbl.to_user_str()).into()
820821

821822
}
822823
(None, false) => {
823824
format!("{} {}",
824825
err.cmt.mutbl.to_user_str(),
825-
self.cmt_to_string(&err.cmt))
826+
self.cmt_to_cow_str(&err.cmt)).into()
826827

827828
}
828829
(Some(lp), true) => {
829830
format!("{} `{}` of {} binding",
830-
self.cmt_to_string(&err.cmt),
831+
self.cmt_to_cow_str(&err.cmt),
831832
self.loan_path_to_string(&lp),
832-
err.cmt.mutbl.to_user_str())
833+
err.cmt.mutbl.to_user_str()).into()
833834
}
834835
(Some(lp), false) => {
835836
format!("{} {} `{}`",
836837
err.cmt.mutbl.to_user_str(),
837-
self.cmt_to_string(&err.cmt),
838-
self.loan_path_to_string(&lp))
838+
self.cmt_to_cow_str(&err.cmt),
839+
self.loan_path_to_string(&lp)).into()
839840
}
840841
}
841842
};
@@ -1058,11 +1059,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10581059
err_borrowed_pointer_too_short(loan_scope, ptr_scope) => {
10591060
let descr = self.cmt_to_path_or_string(err.cmt);
10601061
let mut db = self.lifetime_too_short_for_reborrow(error_span, &descr, Origin::Ast);
1061-
let descr = match opt_loan_path(&err.cmt) {
1062+
let descr: Cow<'static, str> = match opt_loan_path(&err.cmt) {
10621063
Some(lp) => {
1063-
format!("`{}`", self.loan_path_to_string(&lp))
1064+
format!("`{}`", self.loan_path_to_string(&lp)).into()
10641065
}
1065-
None => self.cmt_to_string(&err.cmt),
1066+
None => self.cmt_to_cow_str(&err.cmt)
10661067
};
10671068
self.tcx.note_and_explain_region(
10681069
&self.region_scope_tree,
@@ -1477,14 +1478,14 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
14771478
result
14781479
}
14791480

1480-
pub fn cmt_to_string(&self, cmt: &mc::cmt_<'tcx>) -> String {
1481+
pub fn cmt_to_cow_str(&self, cmt: &mc::cmt_<'tcx>) -> Cow<'static, str> {
14811482
cmt.descriptive_string(self.tcx)
14821483
}
14831484

14841485
pub fn cmt_to_path_or_string(&self, cmt: &mc::cmt_<'tcx>) -> String {
14851486
match opt_loan_path(cmt) {
14861487
Some(lp) => format!("`{}`", self.loan_path_to_string(&lp)),
1487-
None => self.cmt_to_string(cmt),
1488+
None => self.cmt_to_cow_str(cmt).into_owned(),
14881489
}
14891490
}
14901491
}

0 commit comments

Comments
 (0)