Skip to content

Commit c54d4ad

Browse files
committed
avoid some Symbol to String conversions
1 parent 95e7764 commit c54d4ad

File tree

18 files changed

+50
-59
lines changed

18 files changed

+50
-59
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::ty::{
2323
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
2424
use rustc_span::hygiene::DesugaringKind;
2525
use rustc_span::symbol::sym;
26-
use rustc_span::{BytePos, Span};
26+
use rustc_span::{BytePos, Span, Symbol};
2727
use rustc_trait_selection::infer::InferCtxtExt;
2828
use rustc_trait_selection::traits::TraitEngineExt as _;
2929

@@ -1227,8 +1227,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12271227
from_closure: false,
12281228
region_name:
12291229
RegionName {
1230-
source:
1231-
RegionNameSource::AnonRegionFromUpvar(upvar_span, ref upvar_name),
1230+
source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
12321231
..
12331232
},
12341233
span,
@@ -1702,7 +1701,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17021701
borrow_span: Span,
17031702
name: &Option<String>,
17041703
upvar_span: Span,
1705-
upvar_name: &str,
1704+
upvar_name: Symbol,
17061705
escape_span: Span,
17071706
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
17081707
let tcx = self.infcx.tcx;

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::mir::{
1212
};
1313
use rustc_middle::ty::adjustment::PointerCast;
1414
use rustc_middle::ty::{self, RegionVid, TyCtxt};
15-
use rustc_span::symbol::Symbol;
15+
use rustc_span::symbol::{kw, Symbol};
1616
use rustc_span::{sym, DesugaringKind, Span};
1717

1818
use crate::region_infer::BlameConstraint;
@@ -282,7 +282,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
282282
) {
283283
if let ConstraintCategory::OpaqueType = category {
284284
let suggestable_name =
285-
if region_name.was_named() { region_name.to_string() } else { "'_".to_string() };
285+
if region_name.was_named() { region_name.name } else { kw::UnderscoreLifetime };
286286

287287
let msg = format!(
288288
"you can add a bound to the {}to make it last less than `'static` and match `{}`",

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use rustc_middle::ty::subst::InternalSubsts;
1919
use rustc_middle::ty::Region;
2020
use rustc_middle::ty::TypeVisitor;
2121
use rustc_middle::ty::{self, RegionVid, Ty};
22-
use rustc_span::symbol::sym;
23-
use rustc_span::symbol::Ident;
22+
use rustc_span::symbol::{kw, sym, Ident};
2423
use rustc_span::Span;
2524

2625
use crate::borrowck_errors;
@@ -758,7 +757,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
758757
return;
759758
};
760759

761-
let lifetime = if f.has_name() { fr_name.to_string() } else { "'_".to_string() };
760+
let lifetime = if f.has_name() { fr_name.name } else { kw::UnderscoreLifetime };
762761

763762
let arg = match param.param.pat.simple_ident() {
764763
Some(simple_ident) => format!("argument `{}`", simple_ident),
@@ -770,7 +769,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
770769
self.infcx.tcx,
771770
diag,
772771
fn_returns,
773-
lifetime,
772+
lifetime.to_string(),
774773
Some(arg),
775774
captures,
776775
Some((param.param_ty_span, param.param_ty.to_string())),

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ pub(crate) enum RegionNameSource {
3434
/// The `'static` region.
3535
Static,
3636
/// The free region corresponding to the environment of a closure.
37-
SynthesizedFreeEnvRegion(Span, String),
37+
SynthesizedFreeEnvRegion(Span, &'static str),
3838
/// The region corresponding to an argument.
3939
AnonRegionFromArgument(RegionNameHighlight),
4040
/// The region corresponding to a closure upvar.
41-
AnonRegionFromUpvar(Span, String),
41+
AnonRegionFromUpvar(Span, Symbol),
4242
/// The region corresponding to the return type of a closure.
43-
AnonRegionFromOutput(RegionNameHighlight, String),
43+
AnonRegionFromOutput(RegionNameHighlight, &'static str),
4444
/// The region from a type yielded by a generator.
4545
AnonRegionFromYieldTy(Span, String),
4646
/// An anonymous region from an async fn.
@@ -110,7 +110,7 @@ impl RegionName {
110110
}
111111
RegionNameSource::SynthesizedFreeEnvRegion(span, note) => {
112112
diag.span_label(*span, format!("lifetime `{self}` represents this closure's body"));
113-
diag.note(note);
113+
diag.note(*note);
114114
}
115115
RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::CannotMatchHirTy(
116116
span,
@@ -350,10 +350,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
350350

351351
Some(RegionName {
352352
name: region_name,
353-
source: RegionNameSource::SynthesizedFreeEnvRegion(
354-
fn_decl_span,
355-
note.to_string(),
356-
),
353+
source: RegionNameSource::SynthesizedFreeEnvRegion(fn_decl_span, note),
357354
})
358355
}
359356

@@ -678,7 +675,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
678675

679676
Some(RegionName {
680677
name: region_name,
681-
source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name.to_string()),
678+
source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
682679
})
683680
}
684681

@@ -756,7 +753,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
756753

757754
Some(RegionName {
758755
name: self.synthesize_region_name(),
759-
source: RegionNameSource::AnonRegionFromOutput(highlight, mir_description.to_string()),
756+
source: RegionNameSource::AnonRegionFromOutput(highlight, mir_description),
760757
})
761758
}
762759

compiler/rustc_errors/src/diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl Diagnostic {
496496
self
497497
}
498498

499-
pub fn note_trait_signature(&mut self, name: String, signature: String) -> &mut Self {
499+
pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
500500
self.highlighted_note(vec![
501501
(format!("`{}` from trait: `", name), Style::NoStyle),
502502
(signature, Style::Highlight),

compiler/rustc_expand/src/module.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,9 @@ pub fn default_submod_path<'a>(
218218
""
219219
};
220220

221-
let mod_name = ident.name.to_string();
222-
let default_path_str = format!("{}{}.rs", relative_prefix, mod_name);
221+
let default_path_str = format!("{}{}.rs", relative_prefix, ident.name);
223222
let secondary_path_str =
224-
format!("{}{}{}mod.rs", relative_prefix, mod_name, path::MAIN_SEPARATOR);
223+
format!("{}{}{}mod.rs", relative_prefix, ident.name, path::MAIN_SEPARATOR);
225224
let default_path = dir_path.join(&default_path_str);
226225
let secondary_path = dir_path.join(&secondary_path_str);
227226
let default_exists = sess.source_map().file_exists(&default_path);

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'_, 'tcx>, ns: Namespace) -> FmtPr
138138
if let TypeVariableOriginKind::TypeParameterDefinition(name, _) =
139139
infcx.inner.borrow_mut().type_variables().var_origin(ty_vid).kind
140140
{
141-
Some(name.to_string())
141+
Some(name)
142142
} else {
143143
None
144144
}
@@ -151,7 +151,7 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'_, 'tcx>, ns: Namespace) -> FmtPr
151151
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) =
152152
infcx.inner.borrow_mut().const_unification_table().probe_value(ct_vid).origin.kind
153153
{
154-
return Some(name.to_string());
154+
return Some(name);
155155
} else {
156156
None
157157
}

compiler/rustc_middle/src/ty/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
19581958
min_size = field_end;
19591959
}
19601960
FieldInfo {
1961-
name: name.to_string(),
1961+
name,
19621962
offset: offset.bytes(),
19631963
size: field_layout.size.bytes(),
19641964
align: field_layout.align.abi.bytes(),
@@ -1967,7 +1967,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
19671967
.collect();
19681968

19691969
VariantInfo {
1970-
name: n.map(|n| n.to_string()),
1970+
name: n,
19711971
kind: if layout.is_unsized() { SizeKind::Min } else { SizeKind::Exact },
19721972
align: layout.align.abi.bytes(),
19731973
size: if min_size.bytes() == 0 { layout.size.bytes() } else { min_size.bytes() },

compiler/rustc_middle/src/ty/print/pretty.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1030,11 +1030,11 @@ pub trait PrettyPrinter<'tcx>:
10301030
}
10311031
}
10321032

1033-
fn ty_infer_name(&self, _: ty::TyVid) -> Option<String> {
1033+
fn ty_infer_name(&self, _: ty::TyVid) -> Option<Symbol> {
10341034
None
10351035
}
10361036

1037-
fn const_infer_name(&self, _: ty::ConstVid<'tcx>) -> Option<String> {
1037+
fn const_infer_name(&self, _: ty::ConstVid<'tcx>) -> Option<Symbol> {
10381038
None
10391039
}
10401040

@@ -1550,8 +1550,8 @@ pub struct FmtPrinterData<'a, 'tcx> {
15501550

15511551
pub region_highlight_mode: RegionHighlightMode<'tcx>,
15521552

1553-
pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<String> + 'a>>,
1554-
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid<'tcx>) -> Option<String> + 'a>>,
1553+
pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<Symbol> + 'a>>,
1554+
pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid<'tcx>) -> Option<Symbol> + 'a>>,
15551555
}
15561556

15571557
impl<'a, 'tcx> Deref for FmtPrinter<'a, 'tcx> {
@@ -1841,11 +1841,11 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
18411841
}
18421842

18431843
impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
1844-
fn ty_infer_name(&self, id: ty::TyVid) -> Option<String> {
1844+
fn ty_infer_name(&self, id: ty::TyVid) -> Option<Symbol> {
18451845
self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
18461846
}
18471847

1848-
fn const_infer_name(&self, id: ty::ConstVid<'tcx>) -> Option<String> {
1848+
fn const_infer_name(&self, id: ty::ConstVid<'tcx>) -> Option<Symbol> {
18491849
self.0.const_infer_name_resolver.as_ref().and_then(|func| func(id))
18501850
}
18511851

compiler/rustc_session/src/code_stats.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use rustc_data_structures::fx::FxHashSet;
22
use rustc_data_structures::sync::Lock;
3+
use rustc_span::Symbol;
34
use rustc_target::abi::{Align, Size};
45
use std::cmp::{self, Ordering};
56

67
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
78
pub struct VariantInfo {
8-
pub name: Option<String>,
9+
pub name: Option<Symbol>,
910
pub kind: SizeKind,
1011
pub size: u64,
1112
pub align: u64,
@@ -20,7 +21,7 @@ pub enum SizeKind {
2021

2122
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
2223
pub struct FieldInfo {
23-
pub name: String,
24+
pub name: Symbol,
2425
pub offset: u64,
2526
pub size: u64,
2627
pub align: u64,
@@ -119,7 +120,7 @@ impl CodeStats {
119120
let VariantInfo { ref name, kind: _, align: _, size, ref fields } = *variant_info;
120121
let indent = if !struct_like {
121122
let name = match name.as_ref() {
122-
Some(name) => name.to_owned(),
123+
Some(name) => name.to_string(),
123124
None => i.to_string(),
124125
};
125126
println!(

compiler/rustc_typeck/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24892489
concrete type's name `{type_name}` instead if you want to \
24902490
specify its type parameters"
24912491
),
2492-
type_name.to_string(),
2492+
type_name,
24932493
Applicability::MaybeIncorrect,
24942494
);
24952495
}

compiler/rustc_typeck/src/check/compare_method.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ fn compare_self_type<'tcx>(
546546
if let Some(span) = tcx.hir().span_if_local(trait_m.def_id) {
547547
err.span_label(span, format!("trait method declared without `{self_descr}`"));
548548
} else {
549-
err.note_trait_signature(trait_m.name.to_string(), trait_m.signature(tcx));
549+
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
550550
}
551551
let reported = err.emit();
552552
return Err(reported);
@@ -566,7 +566,7 @@ fn compare_self_type<'tcx>(
566566
if let Some(span) = tcx.hir().span_if_local(trait_m.def_id) {
567567
err.span_label(span, format!("`{self_descr}` used in trait"));
568568
} else {
569-
err.note_trait_signature(trait_m.name.to_string(), trait_m.signature(tcx));
569+
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
570570
}
571571
let reported = err.emit();
572572
return Err(reported);
@@ -805,7 +805,7 @@ fn compare_number_of_method_arguments<'tcx>(
805805
),
806806
);
807807
} else {
808-
err.note_trait_signature(trait_m.name.to_string(), trait_m.signature(tcx));
808+
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
809809
}
810810
err.span_label(
811811
impl_span,

compiler/rustc_typeck/src/check/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18731873
let remaining_private_fields_len = remaining_private_fields.len();
18741874
let names = match &remaining_private_fields
18751875
.iter()
1876-
.map(|(name, _, _)| name.to_string())
1876+
.map(|(name, _, _)| name)
18771877
.collect::<Vec<_>>()[..]
18781878
{
18791879
_ if remaining_private_fields_len > 6 => String::new(),

compiler/rustc_typeck/src/check/method/suggest.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::ty::print::with_crate_prefix;
1919
use rustc_middle::ty::ToPolyTraitRef;
2020
use rustc_middle::ty::{self, DefIdTree, ToPredicate, Ty, TyCtxt, TypeVisitable};
2121
use rustc_span::symbol::{kw, sym, Ident};
22+
use rustc_span::Symbol;
2223
use rustc_span::{lev_distance, source_map, ExpnKind, FileName, MacroKind, Span};
2324
use rustc_trait_selection::traits::error_reporting::on_unimplemented::InferCtxtExt as _;
2425
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
@@ -1548,7 +1549,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15481549
Option<ObligationCause<'tcx>>,
15491550
)],
15501551
) {
1551-
let mut derives = Vec::<(String, Span, String)>::new();
1552+
let mut derives = Vec::<(String, Span, Symbol)>::new();
15521553
let mut traits = Vec::<Span>::new();
15531554
for (pred, _, _) in unsatisfied_predicates {
15541555
let ty::PredicateKind::Trait(trait_pred) = pred.kind().skip_binder() else { continue };
@@ -1581,12 +1582,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15811582
derives.push((
15821583
self_name.clone(),
15831584
self_span,
1584-
parent_diagnostic_name.to_string(),
1585+
parent_diagnostic_name,
15851586
));
15861587
}
15871588
}
15881589
}
1589-
derives.push((self_name, self_span, diagnostic_name.to_string()));
1590+
derives.push((self_name, self_span, diagnostic_name));
15901591
} else {
15911592
traits.push(self.tcx.def_span(trait_pred.def_id()));
15921593
}
@@ -1609,7 +1610,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16091610
continue;
16101611
}
16111612
}
1612-
derives_grouped.push((self_name, self_span, trait_name));
1613+
derives_grouped.push((self_name, self_span, trait_name.to_string()));
16131614
}
16141615

16151616
let len = traits.len();

compiler/rustc_typeck/src/impl_wf_check.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::def::DefKind;
1717
use rustc_hir::def_id::LocalDefId;
1818
use rustc_middle::ty::query::Providers;
1919
use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
20-
use rustc_span::Span;
20+
use rustc_span::{Span, Symbol};
2121

2222
use std::collections::hash_map::Entry::{Occupied, Vacant};
2323

@@ -123,12 +123,7 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
123123
ty::GenericParamDefKind::Type { .. } => {
124124
let param_ty = ty::ParamTy::for_def(param);
125125
if !input_parameters.contains(&cgp::Parameter::from(param_ty)) {
126-
report_unused_parameter(
127-
tcx,
128-
tcx.def_span(param.def_id),
129-
"type",
130-
&param_ty.to_string(),
131-
);
126+
report_unused_parameter(tcx, tcx.def_span(param.def_id), "type", param_ty.name);
132127
}
133128
}
134129
ty::GenericParamDefKind::Lifetime => {
@@ -140,7 +135,7 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
140135
tcx,
141136
tcx.def_span(param.def_id),
142137
"lifetime",
143-
&param.name.to_string(),
138+
param.name,
144139
);
145140
}
146141
}
@@ -151,7 +146,7 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
151146
tcx,
152147
tcx.def_span(param.def_id),
153148
"const",
154-
&param_ct.to_string(),
149+
param_ct.name,
155150
);
156151
}
157152
}
@@ -178,7 +173,7 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
178173
// used elsewhere are not projected back out.
179174
}
180175

181-
fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: &str) {
176+
fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: Symbol) {
182177
let mut err = struct_span_err!(
183178
tcx.sess,
184179
span,

0 commit comments

Comments
 (0)