Skip to content

Commit 8cddcd3

Browse files
committed
Remove SymbolStr.
By changing `as_str()` to take `&self` instead of `self`, we can just return `&str`. We're still lying about lifetimes, but it's a smaller lie than before, where `SymbolStr` contained a (fake) `&'static str`!
1 parent 22f8bde commit 8cddcd3

File tree

34 files changed

+125
-182
lines changed

34 files changed

+125
-182
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/constant.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
369369
TodoItem::Static(def_id) => {
370370
//println!("static {:?}", def_id);
371371

372-
let section_name = tcx.codegen_fn_attrs(def_id).link_section.map(|s| s.as_str());
372+
let section_name = tcx.codegen_fn_attrs(def_id).link_section;
373373

374374
let alloc = tcx.eval_static_initializer(def_id).unwrap();
375375

@@ -388,6 +388,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
388388

389389
if let Some(section_name) = section_name {
390390
let (segment_name, section_name) = if tcx.sess.target.is_like_osx {
391+
let section_name = section_name.as_str();
391392
if let Some(names) = section_name.split_once(',') {
392393
names
393394
} else {
@@ -397,7 +398,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
397398
));
398399
}
399400
} else {
400-
("", &*section_name)
401+
("", section_name.as_str())
401402
};
402403
data_ctx.set_segment_section(segment_name, section_name);
403404
}

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2087,8 +2087,8 @@ fn prepare_enum_metadata(
20872087
let item_name;
20882088
let discriminant_name = match enum_type.kind() {
20892089
ty::Adt(..) => {
2090-
item_name = tcx.item_name(enum_def_id).as_str();
2091-
&*item_name
2090+
item_name = tcx.item_name(enum_def_id);
2091+
item_name.as_str()
20922092
}
20932093
ty::Generator(..) => enum_name.as_str(),
20942094
_ => bug!(),
@@ -2563,7 +2563,8 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global
25632563
let is_local_to_unit = is_node_local_to_unit(cx, def_id);
25642564
let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx, ty::ParamEnv::reveal_all());
25652565
let type_metadata = type_metadata(cx, variable_type, span);
2566-
let var_name = tcx.item_name(def_id).as_str();
2566+
let var_name = tcx.item_name(def_id);
2567+
let var_name = var_name.as_str();
25672568
let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name;
25682569
// When empty, linkage_name field is omitted,
25692570
// which is what we want for no_mangle statics

Diff for: compiler/rustc_expand/src/module.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ fn mod_file_path_from_attr(
170170
) -> Option<PathBuf> {
171171
// Extract path string from first `#[path = "path_string"]` attribute.
172172
let first_path = attrs.iter().find(|at| at.has_name(sym::path))?;
173-
let path_string = match first_path.value_str() {
174-
Some(s) => s.as_str(),
173+
let path_sym = match first_path.value_str() {
174+
Some(s) => s,
175175
None => {
176176
// This check is here mainly to catch attempting to use a macro,
177177
// such as #[path = concat!(...)]. This isn't currently supported
@@ -189,14 +189,16 @@ fn mod_file_path_from_attr(
189189
}
190190
};
191191

192+
let path_str = path_sym.as_str();
193+
192194
// On windows, the base path might have the form
193195
// `\\?\foo\bar` in which case it does not tolerate
194196
// mixed `/` and `\` separators, so canonicalize
195197
// `/` to `\`.
196198
#[cfg(windows)]
197-
let path_string = path_string.replace("/", "\\");
199+
let path_str = path_str.replace("/", "\\");
198200

199-
Some(dir_path.join(&*path_string))
201+
Some(dir_path.join(path_str))
200202
}
201203

202204
/// Returns a path to a module.

Diff for: compiler/rustc_interface/src/passes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1049,8 +1049,8 @@ fn encode_and_write_metadata(
10491049

10501050
let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata);
10511051
if need_metadata_file {
1052-
let crate_name = &tcx.crate_name(LOCAL_CRATE).as_str();
1053-
let out_filename = filename_for_metadata(tcx.sess, crate_name, outputs);
1052+
let crate_name = tcx.crate_name(LOCAL_CRATE);
1053+
let out_filename = filename_for_metadata(tcx.sess, crate_name.as_str(), outputs);
10541054
// To avoid races with another rustc process scanning the output directory,
10551055
// we need to write the file somewhere else and atomically move it to its
10561056
// final destination, with an `fs::rename` call. In order for the rename to

Diff for: compiler/rustc_lint/src/non_fmt_panic.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::subst::InternalSubsts;
99
use rustc_parse_format::{ParseMode, Parser, Piece};
1010
use rustc_session::lint::FutureIncompatibilityReason;
1111
use rustc_span::edition::Edition;
12-
use rustc_span::{hygiene, sym, symbol::kw, symbol::SymbolStr, InnerSpan, Span, Symbol};
12+
use rustc_span::{hygiene, sym, symbol::kw, InnerSpan, Span, Symbol};
1313
use rustc_trait_selection::infer::InferCtxtExt;
1414

1515
declare_lint! {
@@ -78,7 +78,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
7878

7979
// The argument is *not* a string literal.
8080

81-
let (span, panic, symbol_str) = panic_call(cx, f);
81+
let (span, panic, symbol) = panic_call(cx, f);
8282

8383
if in_external_macro(cx.sess(), span) {
8484
// Nothing that can be done about it in the current crate.
@@ -103,7 +103,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
103103

104104
cx.struct_span_lint(NON_FMT_PANICS, arg_span, |lint| {
105105
let mut l = lint.build("panic message is not a string literal");
106-
l.note(&format!("this usage of {}!() is deprecated; it will be a hard error in Rust 2021", symbol_str));
106+
l.note(&format!("this usage of {}!() is deprecated; it will be a hard error in Rust 2021", symbol));
107107
l.note("for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>");
108108
if !is_arg_inside_call(arg_span, span) {
109109
// No clue where this argument is coming from.
@@ -112,7 +112,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
112112
}
113113
if arg_macro.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::format_macro, id)) {
114114
// A case of `panic!(format!(..))`.
115-
l.note(format!("the {}!() macro supports formatting, so there's no need for the format!() macro here", symbol_str).as_str());
115+
l.note(format!("the {}!() macro supports formatting, so there's no need for the format!() macro here", symbol).as_str());
116116
if let Some((open, close, _)) = find_delimiters(cx, arg_span) {
117117
l.multipart_suggestion(
118118
"remove the `format!(..)` macro call",
@@ -301,7 +301,7 @@ fn find_delimiters<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<(Span, Sp
301301
))
302302
}
303303

304-
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol, SymbolStr) {
304+
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol, Symbol) {
305305
let mut expn = f.span.ctxt().outer_expn_data();
306306

307307
let mut panic_macro = kw::Empty;
@@ -328,7 +328,7 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
328328

329329
let macro_symbol =
330330
if let hygiene::ExpnKind::Macro(_, symbol) = expn.kind { symbol } else { sym::panic };
331-
(expn.call_site, panic_macro, macro_symbol.as_str())
331+
(expn.call_site, panic_macro, macro_symbol)
332332
}
333333

334334
fn is_arg_inside_call(arg: Span, call: Span) -> bool {

Diff for: compiler/rustc_metadata/src/locator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,8 @@ impl CrateError {
976976
let candidates = libraries
977977
.iter()
978978
.map(|lib| {
979-
let crate_name = &lib.metadata.get_root().name().as_str();
979+
let crate_name = lib.metadata.get_root().name();
980+
let crate_name = crate_name.as_str();
980981
let mut paths = lib.source.paths();
981982

982983
// This `unwrap()` should be okay because there has to be at least one

Diff for: compiler/rustc_middle/src/middle/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub mod lib_features {
2121
.map(|(f, s)| (*f, Some(*s)))
2222
.chain(self.unstable.iter().map(|f| (*f, None)))
2323
.collect();
24-
all_features.sort_unstable_by_key(|f| f.0.as_str());
24+
all_features.sort_unstable_by(|a, b| a.0.as_str().partial_cmp(b.0.as_str()).unwrap());
2525
all_features
2626
}
2727
}

Diff for: compiler/rustc_middle/src/middle/stability.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ pub fn report_unstable(
131131
/// deprecated (i.e., whether X is not greater than the current rustc version).
132132
pub fn deprecation_in_effect(depr: &Deprecation) -> bool {
133133
let is_since_rustc_version = depr.is_since_rustc_version;
134-
let since = depr.since.map(Symbol::as_str);
135-
let since = since.as_deref();
134+
let since = depr.since.as_ref().map(Symbol::as_str);
136135

137136
fn parse_version(ver: &str) -> Vec<u32> {
138137
// We ignore non-integer components of the version (e.g., "nightly").
@@ -197,7 +196,7 @@ fn deprecation_message(
197196
let message = if is_in_effect {
198197
format!("use of deprecated {} `{}`", kind, path)
199198
} else {
200-
let since = since.map(Symbol::as_str);
199+
let since = since.as_ref().map(Symbol::as_str);
201200

202201
if since.as_deref() == Some("TBD") {
203202
format!("use of {} `{}` that will be deprecated in a future Rust version", kind, path)

Diff for: compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,8 @@ impl<'tcx> TyCtxt<'tcx> {
12161216
}
12171217

12181218
pub fn consider_optimizing<T: Fn() -> String>(self, msg: T) -> bool {
1219-
let cname = self.crate_name(LOCAL_CRATE).as_str();
1220-
self.sess.consider_optimizing(&cname, msg)
1219+
let cname = self.crate_name(LOCAL_CRATE);
1220+
self.sess.consider_optimizing(cname.as_str(), msg)
12211221
}
12221222

12231223
/// Obtain all lang items of this crate and all dependencies (recursively)

Diff for: compiler/rustc_monomorphize/src/partitioning/merging.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cmp;
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::def_id::LOCAL_CRATE;
55
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder};
6-
use rustc_span::symbol::{Symbol, SymbolStr};
6+
use rustc_span::symbol::Symbol;
77

88
use super::PartitioningCx;
99
use crate::partitioning::PreInliningPartitioning;
@@ -24,11 +24,11 @@ pub fn merge_codegen_units<'tcx>(
2424
// smallest into each other) we're sure to start off with a deterministic
2525
// order (sorted by name). This'll mean that if two cgus have the same size
2626
// the stable sort below will keep everything nice and deterministic.
27-
codegen_units.sort_by_cached_key(|cgu| cgu.name().as_str());
27+
codegen_units.sort_by(|a, b| a.name().as_str().partial_cmp(b.name().as_str()).unwrap());
2828

2929
// This map keeps track of what got merged into what.
30-
let mut cgu_contents: FxHashMap<Symbol, Vec<SymbolStr>> =
31-
codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name().as_str()])).collect();
30+
let mut cgu_contents: FxHashMap<Symbol, Vec<Symbol>> =
31+
codegen_units.iter().map(|cgu| (cgu.name(), vec![cgu.name()])).collect();
3232

3333
// Merge the two smallest codegen units until the target size is reached.
3434
while codegen_units.len() > cx.target_cgu_count {
@@ -69,7 +69,7 @@ pub fn merge_codegen_units<'tcx>(
6969
// were actually modified by merging.
7070
.filter(|(_, cgu_contents)| cgu_contents.len() > 1)
7171
.map(|(current_cgu_name, cgu_contents)| {
72-
let mut cgu_contents: Vec<&str> = cgu_contents.iter().map(|s| &s[..]).collect();
72+
let mut cgu_contents: Vec<&str> = cgu_contents.iter().map(|s| s.as_str()).collect();
7373

7474
// Sort the names, so things are deterministic and easy to
7575
// predict.

Diff for: compiler/rustc_monomorphize/src/partitioning/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn partition<'tcx>(
208208
internalization_candidates: _,
209209
} = post_inlining;
210210

211-
result.sort_by_cached_key(|cgu| cgu.name().as_str());
211+
result.sort_by(|a, b| a.name().as_str().partial_cmp(b.name().as_str()).unwrap());
212212

213213
result
214214
}

Diff for: compiler/rustc_parse/src/parser/expr.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,8 @@ impl<'a> Parser<'a> {
16791679
);
16801680
}
16811681
LitError::InvalidIntSuffix => {
1682-
let suf = suffix.expect("suffix error with no suffix").as_str();
1682+
let suf = suffix.expect("suffix error with no suffix");
1683+
let suf = suf.as_str();
16831684
if looks_like_width_suffix(&['i', 'u'], &suf) {
16841685
// If it looks like a width, try to be helpful.
16851686
let msg = format!("invalid width `{}` for integer literal", &suf[1..]);
@@ -1695,7 +1696,8 @@ impl<'a> Parser<'a> {
16951696
}
16961697
}
16971698
LitError::InvalidFloatSuffix => {
1698-
let suf = suffix.expect("suffix error with no suffix").as_str();
1699+
let suf = suffix.expect("suffix error with no suffix");
1700+
let suf = suf.as_str();
16991701
if looks_like_width_suffix(&['f'], &suf) {
17001702
// If it looks like a width, try to be helpful.
17011703
let msg = format!("invalid width `{}` for float literal", &suf[1..]);

Diff for: compiler/rustc_query_impl/src/profiling_support.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ impl<'p, 'c, 'tcx> QueryKeyStringBuilder<'p, 'c, 'tcx> {
6161

6262
match def_key.disambiguated_data.data {
6363
DefPathData::CrateRoot => {
64-
crate_name = self.tcx.crate_name(def_id.krate).as_str();
65-
name = &*crate_name;
64+
crate_name = self.tcx.crate_name(def_id.krate);
65+
name = crate_name.as_str();
6666
dis = "";
6767
end_index = 3;
6868
}

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::cmp::Reverse;
21
use std::ptr;
32

43
use rustc_ast::{self as ast, Path};
@@ -784,7 +783,7 @@ impl<'a> Resolver<'a> {
784783
});
785784

786785
// Make sure error reporting is deterministic.
787-
suggestions.sort_by_cached_key(|suggestion| suggestion.candidate.as_str());
786+
suggestions.sort_by(|a, b| a.candidate.as_str().partial_cmp(b.candidate.as_str()).unwrap());
788787

789788
match find_best_match_for_name(
790789
&suggestions.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),
@@ -1481,12 +1480,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14811480
return None;
14821481
}
14831482

1484-
// Sort extern crate names in reverse order to get
1483+
// Sort extern crate names in *reverse* order to get
14851484
// 1) some consistent ordering for emitted diagnostics, and
14861485
// 2) `std` suggestions before `core` suggestions.
14871486
let mut extern_crate_names =
14881487
self.r.extern_prelude.iter().map(|(ident, _)| ident.name).collect::<Vec<_>>();
1489-
extern_crate_names.sort_by_key(|name| Reverse(name.as_str()));
1488+
extern_crate_names.sort_by(|a, b| b.as_str().partial_cmp(a.as_str()).unwrap());
14901489

14911490
for name in extern_crate_names.into_iter() {
14921491
// Replace first ident with a crate name and check if that is valid.

Diff for: compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
13531353

13541354
let name = path[path.len() - 1].ident.name;
13551355
// Make sure error reporting is deterministic.
1356-
names.sort_by_cached_key(|suggestion| suggestion.candidate.as_str());
1356+
names.sort_by(|a, b| a.candidate.as_str().partial_cmp(b.candidate.as_str()).unwrap());
13571357

13581358
match find_best_match_for_name(
13591359
&names.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),

Diff for: compiler/rustc_resolve/src/late/lifetimes.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -689,11 +689,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
689689
hir_id: hir::HirId,
690690
) {
691691
let name = match fk {
692-
intravisit::FnKind::ItemFn(id, _, _, _) => id.as_str(),
693-
intravisit::FnKind::Method(id, _, _) => id.as_str(),
694-
intravisit::FnKind::Closure => Symbol::intern("closure").as_str(),
692+
intravisit::FnKind::ItemFn(id, _, _, _) => id.name,
693+
intravisit::FnKind::Method(id, _, _) => id.name,
694+
intravisit::FnKind::Closure => sym::closure,
695695
};
696-
let name: &str = &name;
696+
let name: &str = name.as_str();
697697
let span = span!(Level::DEBUG, "visit_fn", name);
698698
let _enter = span.enter();
699699
match fk {

0 commit comments

Comments
 (0)