Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store a Symbol in InternedString #46972

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ impl Term {

/// Get a reference to the interned string.
#[unstable(feature = "proc_macro", issue = "38356")]
pub fn as_str(&self) -> &str {
unsafe { &*(&*self.0.as_str() as *const str) }
pub fn with_str<F: FnOnce(&str) -> R, R>(self, f: F) -> R {
self.0.with_str(f)
}
}

Expand Down Expand Up @@ -678,8 +678,11 @@ impl TokenTree {
},
TokenNode::Term(symbol) => {
let ident = ast::Ident { name: symbol.0, ctxt: self.span.0.ctxt() };
let token =
if symbol.0.as_str().starts_with("'") { Lifetime(ident) } else { Ident(ident) };
let token = if symbol.0.with_str(|str| str.starts_with("'")) {
Lifetime(ident)
} else {
Ident(ident)
};
return TokenTree::Token(self.span.0, token).into();
}
TokenNode::Literal(token) => return TokenTree::Token(self.span.0, token.0).into(),
Expand Down
2 changes: 1 addition & 1 deletion src/libproc_macro/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl Quote for usize {

impl Quote for Term {
fn quote(self) -> TokenStream {
quote!(::Term::intern((quote self.as_str())))
self.with_str(|str| quote!(::Term::intern((quote str))))
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ impl<'a> CheckAttrVisitor<'a> {
/// Check any attribute.
fn check_attribute(&self, attr: &ast::Attribute, item: &ast::Item, target: Target) {
if let Some(name) = attr.name() {
match &*name.as_str() {
name.with_str(|str| match str {
"inline" => self.check_inline(attr, item, target),
"repr" => self.check_repr(attr, item, target),
_ => (),
}
})
}
}

Expand Down Expand Up @@ -86,7 +86,7 @@ impl<'a> CheckAttrVisitor<'a> {
None => continue,
};

let (message, label) = match &*name.as_str() {
let (message, label) = match &*name.to_string() {
"C" => {
is_c = true;
if target != Target::Struct &&
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ pub struct StructField {
impl StructField {
// Still necessary in couple of places
pub fn is_positional(&self) -> bool {
let first = self.name.as_str().as_bytes()[0];
let first = self.name.with_str(|str| str.as_bytes()[0]);
first >= b'0' && first <= b'9'
}
}
Expand Down
41 changes: 22 additions & 19 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,13 @@ impl<'a> State<'a> {
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
if let Some(p) = *optional_path {
let val = p.as_str();
if val.contains("-") {
self.print_string(&val, ast::StrStyle::Cooked)?;
} else {
self.print_name(p)?;
}
val.with(|str| {
if str.contains("-") {
self.print_string(str, ast::StrStyle::Cooked)
} else {
self.print_name(p)
}
})?;
self.s.space()?;
self.s.word("as")?;
self.s.space()?;
Expand Down Expand Up @@ -623,7 +625,7 @@ impl<'a> State<'a> {
}
hir::ItemGlobalAsm(ref ga) => {
self.head(&visibility_qualified(&item.vis, "global asm"))?;
self.s.word(&ga.asm.as_str())?;
ga.asm.with_str(|str| self.s.word(str))?;
self.end()?
}
hir::ItemTy(ref ty, ref generics) => {
Expand Down Expand Up @@ -1469,20 +1471,21 @@ impl<'a> State<'a> {
hir::ExprInlineAsm(ref a, ref outputs, ref inputs) => {
self.s.word("asm!")?;
self.popen()?;
self.print_string(&a.asm.as_str(), a.asm_str_style)?;
a.asm.with_str(|str| self.print_string(str, a.asm_str_style))?;
self.word_space(":")?;

let mut out_idx = 0;
self.commasep(Inconsistent, &a.outputs, |s, out| {
let constraint = out.constraint.as_str();
let mut ch = constraint.chars();
match ch.next() {
Some('=') if out.is_rw => {
s.print_string(&format!("+{}", ch.as_str()),
ast::StrStyle::Cooked)?
out.constraint.with_str(|constraint| {
let mut ch = constraint.chars();
match ch.next() {
Some('=') if out.is_rw => {
s.print_string(&format!("+{}", ch.as_str()),
ast::StrStyle::Cooked)
}
_ => s.print_string(&constraint, ast::StrStyle::Cooked)
}
_ => s.print_string(&constraint, ast::StrStyle::Cooked)?,
}
})?;
s.popen()?;
s.print_expr(&outputs[out_idx])?;
s.pclose()?;
Expand All @@ -1494,7 +1497,7 @@ impl<'a> State<'a> {

let mut in_idx = 0;
self.commasep(Inconsistent, &a.inputs, |s, co| {
s.print_string(&co.as_str(), ast::StrStyle::Cooked)?;
co.with_str(|str| s.print_string(str, ast::StrStyle::Cooked))?;
s.popen()?;
s.print_expr(&inputs[in_idx])?;
s.pclose()?;
Expand All @@ -1505,7 +1508,7 @@ impl<'a> State<'a> {
self.word_space(":")?;

self.commasep(Inconsistent, &a.clobbers, |s, co| {
s.print_string(&co.as_str(), ast::StrStyle::Cooked)?;
co.with_str(|str| s.print_string(str, ast::StrStyle::Cooked))?;
Ok(())
})?;

Expand Down Expand Up @@ -1578,7 +1581,7 @@ impl<'a> State<'a> {
}

pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
self.s.word(&name.as_str())?;
name.with_str(|str| self.s.word(str))?;
self.ann.post(self, NodeName(&name))
}

Expand Down Expand Up @@ -1936,7 +1939,7 @@ impl<'a> State<'a> {
self.commasep(Inconsistent, &decl.inputs, |s, ty| {
s.ibox(indent_unit)?;
if let Some(name) = arg_names.get(i) {
s.s.word(&name.node.as_str())?;
name.node.with_str(|str| s.s.word(str))?;
s.s.word(":")?;
s.s.space()?;
} else if let Some(body_id) = body_id {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for InternedString {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'gcx>,
hasher: &mut StableHasher<W>) {
let s: &str = &**self;
s.hash_stable(hcx, hasher);
self.with(|str| str.hash_stable(hcx, hasher));
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/librustc/lint/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ impl<'a> LintLevelsBuilder<'a> {
"malformed lint attribute");
};
for attr in attrs {
let level = match attr.name().and_then(|name| Level::from_str(&name.as_str())) {
let level = match attr.name()
.and_then(|name| name.with_str(|str| Level::from_str(str))) {
None => continue,
Some(lvl) => lvl,
};
Expand All @@ -221,7 +222,7 @@ impl<'a> LintLevelsBuilder<'a> {
}
};
let name = word.name();
match store.check_lint_name(&name.as_str()) {
match name.with_str(|str| store.check_lint_name(str)) {
CheckLintNameResult::Ok(ids) => {
let src = LintSource::Node(name, li.span);
for id in ids {
Expand Down Expand Up @@ -256,8 +257,8 @@ impl<'a> LintLevelsBuilder<'a> {
src,
Some(li.span.into()),
&msg);
if name.as_str().chars().any(|c| c.is_uppercase()) {
let name_lower = name.as_str().to_lowercase();
if name.with_str(|str| str.chars().any(|c| c.is_uppercase())) {
let name_lower = name.with_str(|str| str.to_lowercase());
if let CheckLintNameResult::NoLint =
store.check_lint_name(&name_lower) {
db.emit();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
&format!("requested on the command line with `{} {}`",
flag, hyphen_case_lint_name));
} else {
let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
let hyphen_case_flag_val = lint_flag_val.with_str(|str| str.replace("_", "-"));
sess.diag_note_once(
&mut err,
DiagnosticMessageId::from(lint),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
name: ast::Name,
node_type: &str,
participle: &str) {
if !name.as_str().starts_with("_") {
if !name.with_str(|str| str.starts_with("_")) {
self.tcx
.lint_node(lint::builtin::DEAD_CODE,
id,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct LanguageItemCollector<'a, 'tcx: 'a> {
impl<'a, 'v, 'tcx> ItemLikeVisitor<'v> for LanguageItemCollector<'a, 'tcx> {
fn visit_item(&mut self, item: &hir::Item) {
if let Some(value) = extract(&item.attrs) {
let item_index = self.item_refs.get(&*value.as_str()).cloned();
let item_index = value.with_str(|str| self.item_refs.get(str).cloned());

if let Some(item_index) = item_index {
let def_id = self.tcx.hir.local_def_id(item.id);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/recursion_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn update_limit(sess: &Session, krate: &ast::Crate, limit: &Cell<usize>,
}

if let Some(s) = attr.value_str() {
if let Some(n) = s.as_str().parse().ok() {
if let Some(n) = s.with_str(|str| str.parse()).ok() {
limit.set(n);
return;
}
Expand Down
48 changes: 28 additions & 20 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,26 +164,29 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
if let (&Some(attr::RustcDeprecation {since: dep_since, ..}),
&attr::Stable {since: stab_since}) = (&stab.rustc_depr, &stab.level) {
// Explicit version of iter::order::lt to handle parse errors properly
for (dep_v, stab_v) in
dep_since.as_str().split(".").zip(stab_since.as_str().split(".")) {
if let (Ok(dep_v), Ok(stab_v)) = (dep_v.parse::<u64>(), stab_v.parse()) {
match dep_v.cmp(&stab_v) {
Ordering::Less => {
self.tcx.sess.span_err(item_sp, "An API can't be stabilized \
after it is deprecated");
break
dep_since.with_str(|dep_since| stab_since.with_str(|stab_since| {
for (dep_v, stab_v) in dep_since.split(".").zip(stab_since.split(".")) {
if let (Ok(dep_v), Ok(stab_v)) = (dep_v.parse::<u64>(),
stab_v.parse()) {
match dep_v.cmp(&stab_v) {
Ordering::Less => {
self.tcx.sess.span_err(item_sp,
"An API can't be stabilized \
after it is deprecated");
break
}
Ordering::Equal => continue,
Ordering::Greater => break,
}
Ordering::Equal => continue,
Ordering::Greater => break,
} else {
// Act like it isn't less because the question is now nonsensical,
// and this makes us not do anything else interesting.
self.tcx.sess.span_err(item_sp, "Invalid stability or deprecation \
version found");
break
}
} else {
// Act like it isn't less because the question is now nonsensical,
// and this makes us not do anything else interesting.
self.tcx.sess.span_err(item_sp, "Invalid stability or deprecation \
version found");
break
}
}
}))
}

let hir_id = self.tcx.hir.node_to_hir_id(id);
Expand Down Expand Up @@ -620,8 +623,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let error_id = (DiagnosticMessageId::StabilityId(issue), span_key, msg.clone());
let fresh = self.sess.one_time_diagnostics.borrow_mut().insert(error_id);
if fresh {
emit_feature_err(&self.sess.parse_sess, &feature.as_str(), span,
GateIssue::Library(Some(issue)), &msg);
feature.with_str(|str| {
emit_feature_err(&self.sess.parse_sess,
str,
span,
GateIssue::Library(Some(issue)),
&msg)
});
}
}
Some(_) => {
Expand Down Expand Up @@ -742,7 +750,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
remaining_lib_features.remove(&Symbol::intern("proc_macro"));

for &(ref stable_lang_feature, span) in &sess.features.borrow().declared_stable_lang_features {
let version = find_lang_feature_accepted_version(&stable_lang_feature.as_str())
let version = stable_lang_feature.with_str(|str| find_lang_feature_accepted_version(str))
.expect("unexpectedly couldn't find version feature was stabilized");
tcx.lint_node(lint::builtin::STABLE_FEATURES,
ast::CRATE_NODE_ID,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/weak_lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {

fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
if let Some(lang_item) = lang_items::extract(&i.attrs) {
self.register(&lang_item.as_str(), i.span);
lang_item.with_str(|str| self.register(str, i.span));
}
intravisit::walk_foreign_item(self, i)
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
CtorKind::Fictive => {
let mut struct_fmt = fmt.debug_struct("");
for (field, place) in variant_def.fields.iter().zip(places) {
struct_fmt.field(&field.name.as_str(), place);
field.name.with_str(|str| struct_fmt.field(str, place));
}
struct_fmt.finish()
}
Expand All @@ -1671,7 +1671,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
tcx.with_freevars(node_id, |freevars| {
for (freevar, place) in freevars.iter().zip(places) {
let var_name = tcx.hir.name(freevar.var_id());
struct_fmt.field(&var_name.as_str(), place);
var_name.with_str(|str| struct_fmt.field(str, place));
}
});

Expand All @@ -1689,7 +1689,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
tcx.with_freevars(node_id, |freevars| {
for (freevar, place) in freevars.iter().zip(places) {
let var_name = tcx.hir.name(freevar.var_id());
struct_fmt.field(&var_name.as_str(), place);
var_name.with_str(|str| struct_fmt.field(str, place));
}
struct_fmt.field("$state", &places[freevars.len()]);
for i in (freevars.len() + 1)..places.len() {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
//
// Currently I'm leaving it for what I need for `try`.
if self.tcx.trait_of_item(item) == Some(trait_ref.def_id) {
method = self.tcx.item_name(item);
method = self.tcx.item_name(item).to_string();
flags.push(("from_method", None));
flags.push(("from_method", Some(&*method)));
}
}

if let Some(k) = obligation.cause.span.compiler_desugaring_kind() {
desugaring = k.as_symbol().as_str();
desugaring = k.as_symbol().to_string();
flags.push(("from_desugaring", None));
flags.push(("from_desugaring", Some(&*desugaring)));
}
Expand Down
Loading