Skip to content

More static symbols #74175

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

Merged
merged 4 commits into from
Jul 15, 2020
Merged
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
14 changes: 7 additions & 7 deletions src/librustc_ast/expand/allocator.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ pub enum AllocatorKind {
}

impl AllocatorKind {
pub fn fn_name(&self, base: &str) -> String {
pub fn fn_name(&self, base: Symbol) -> String {
match *self {
AllocatorKind::Global => format!("__rg_{}", base),
AllocatorKind::Default => format!("__rdl_{}", base),
@@ -26,29 +26,29 @@ pub enum AllocatorTy {
}

pub struct AllocatorMethod {
pub name: &'static str,
pub name: Symbol,
pub inputs: &'static [AllocatorTy],
pub output: AllocatorTy,
}

pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
AllocatorMethod {
name: "alloc",
name: sym::alloc,
inputs: &[AllocatorTy::Layout],
output: AllocatorTy::ResultPtr,
},
AllocatorMethod {
name: "dealloc",
name: sym::dealloc,
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
output: AllocatorTy::Unit,
},
AllocatorMethod {
name: "realloc",
name: sym::realloc,
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Usize],
output: AllocatorTy::ResultPtr,
},
AllocatorMethod {
name: "alloc_zeroed",
name: sym::alloc_zeroed,
inputs: &[AllocatorTy::Layout],
output: AllocatorTy::ResultPtr,
},
@@ -70,7 +70,7 @@ pub fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {
}
}

let name = Symbol::intern(&AllocatorKind::Global.fn_name("alloc"));
let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::alloc));
let mut f = Finder { name, spans: Vec::new() };
visit::walk_crate(&mut f, krate);
f.spans
9 changes: 6 additions & 3 deletions src/librustc_ast/util/comments.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ pub use CommentStyle::*;

use crate::ast;
use rustc_span::source_map::SourceMap;
use rustc_span::{BytePos, CharPos, FileName, Pos};
use rustc_span::{BytePos, CharPos, FileName, Pos, Symbol};

use log::debug;

@@ -52,7 +52,8 @@ pub fn is_doc_comment(s: &str) -> bool {
|| s.starts_with("/*!")
}

pub fn doc_comment_style(comment: &str) -> ast::AttrStyle {
pub fn doc_comment_style(comment: Symbol) -> ast::AttrStyle {
let comment = &comment.as_str();
assert!(is_doc_comment(comment));
if comment.starts_with("//!") || comment.starts_with("/*!") {
ast::AttrStyle::Inner
@@ -61,7 +62,9 @@ pub fn doc_comment_style(comment: &str) -> ast::AttrStyle {
}
}

pub fn strip_doc_comment_decoration(comment: &str) -> String {
pub fn strip_doc_comment_decoration(comment: Symbol) -> String {
let comment = &comment.as_str();

/// remove whitespace-only lines from the start/end of lines
fn vertical_trim(lines: Vec<String>) -> Vec<String> {
let mut i = 0;
63 changes: 37 additions & 26 deletions src/librustc_ast/util/comments/tests.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
use super::*;
use crate::with_default_session_globals;

#[test]
fn test_block_doc_comment_1() {
let comment = "/**\n * Test \n ** Test\n * Test\n*/";
let stripped = strip_doc_comment_decoration(comment);
assert_eq!(stripped, " Test \n* Test\n Test");
with_default_session_globals(|| {
let comment = "/**\n * Test \n ** Test\n * Test\n*/";
let stripped = strip_doc_comment_decoration(Symbol::intern(comment));
assert_eq!(stripped, " Test \n* Test\n Test");
})
}

#[test]
fn test_block_doc_comment_2() {
let comment = "/**\n * Test\n * Test\n*/";
let stripped = strip_doc_comment_decoration(comment);
assert_eq!(stripped, " Test\n Test");
with_default_session_globals(|| {
let comment = "/**\n * Test\n * Test\n*/";
let stripped = strip_doc_comment_decoration(Symbol::intern(comment));
assert_eq!(stripped, " Test\n Test");
})
}

#[test]
fn test_block_doc_comment_3() {
let comment = "/**\n let a: *i32;\n *a = 5;\n*/";
let stripped = strip_doc_comment_decoration(comment);
assert_eq!(stripped, " let a: *i32;\n *a = 5;");
with_default_session_globals(|| {
let comment = "/**\n let a: *i32;\n *a = 5;\n*/";
let stripped = strip_doc_comment_decoration(Symbol::intern(comment));
assert_eq!(stripped, " let a: *i32;\n *a = 5;");
})
}

#[test]
fn test_block_doc_comment_4() {
let comment = "/*******************\n test\n *********************/";
let stripped = strip_doc_comment_decoration(comment);
assert_eq!(stripped, " test");
with_default_session_globals(|| {
let comment = "/*******************\n test\n *********************/";
let stripped = strip_doc_comment_decoration(Symbol::intern(comment));
assert_eq!(stripped, " test");
})
}

#[test]
fn test_line_doc_comment() {
let stripped = strip_doc_comment_decoration("/// test");
assert_eq!(stripped, " test");
let stripped = strip_doc_comment_decoration("///! test");
assert_eq!(stripped, " test");
let stripped = strip_doc_comment_decoration("// test");
assert_eq!(stripped, " test");
let stripped = strip_doc_comment_decoration("// test");
assert_eq!(stripped, " test");
let stripped = strip_doc_comment_decoration("///test");
assert_eq!(stripped, "test");
let stripped = strip_doc_comment_decoration("///!test");
assert_eq!(stripped, "test");
let stripped = strip_doc_comment_decoration("//test");
assert_eq!(stripped, "test");
with_default_session_globals(|| {
let stripped = strip_doc_comment_decoration(Symbol::intern("/// test"));
assert_eq!(stripped, " test");
let stripped = strip_doc_comment_decoration(Symbol::intern("///! test"));
assert_eq!(stripped, " test");
let stripped = strip_doc_comment_decoration(Symbol::intern("// test"));
assert_eq!(stripped, " test");
let stripped = strip_doc_comment_decoration(Symbol::intern("// test"));
assert_eq!(stripped, " test");
let stripped = strip_doc_comment_decoration(Symbol::intern("///test"));
assert_eq!(stripped, "test");
let stripped = strip_doc_comment_decoration(Symbol::intern("///!test"));
assert_eq!(stripped, "test");
let stripped = strip_doc_comment_decoration(Symbol::intern("//test"));
assert_eq!(stripped, "test");
})
}
3 changes: 2 additions & 1 deletion src/librustc_ast/util/lev_distance.rs
Original file line number Diff line number Diff line change
@@ -47,12 +47,13 @@ pub fn lev_distance(a: &str, b: &str) -> usize {
/// a lower(upper)case letters mismatch.
pub fn find_best_match_for_name<'a, T>(
iter_names: T,
lookup: &str,
lookup: Symbol,
dist: Option<usize>,
) -> Option<Symbol>
where
T: Iterator<Item = &'a Symbol>,
{
let lookup = &lookup.as_str();
let max_dist = dist.map_or_else(|| cmp::max(lookup.len(), 3) / 3, |d| d);
let name_vec: Vec<&Symbol> = iter_names.collect();

15 changes: 9 additions & 6 deletions src/librustc_ast/util/lev_distance/tests.rs
Original file line number Diff line number Diff line change
@@ -25,31 +25,34 @@ fn test_find_best_match_for_name() {
with_default_session_globals(|| {
let input = vec![Symbol::intern("aaab"), Symbol::intern("aaabc")];
assert_eq!(
find_best_match_for_name(input.iter(), "aaaa", None),
find_best_match_for_name(input.iter(), Symbol::intern("aaaa"), None),
Some(Symbol::intern("aaab"))
);

assert_eq!(find_best_match_for_name(input.iter(), "1111111111", None), None);
assert_eq!(
find_best_match_for_name(input.iter(), Symbol::intern("1111111111"), None),
None
);

let input = vec![Symbol::intern("aAAA")];
assert_eq!(
find_best_match_for_name(input.iter(), "AAAA", None),
find_best_match_for_name(input.iter(), Symbol::intern("AAAA"), None),
Some(Symbol::intern("aAAA"))
);

let input = vec![Symbol::intern("AAAA")];
// Returns None because `lev_distance > max_dist / 3`
assert_eq!(find_best_match_for_name(input.iter(), "aaaa", None), None);
assert_eq!(find_best_match_for_name(input.iter(), Symbol::intern("aaaa"), None), None);

let input = vec![Symbol::intern("AAAA")];
assert_eq!(
find_best_match_for_name(input.iter(), "aaaa", Some(4)),
find_best_match_for_name(input.iter(), Symbol::intern("aaaa"), Some(4)),
Some(Symbol::intern("AAAA"))
);

let input = vec![Symbol::intern("a_longer_variable_name")];
assert_eq!(
find_best_match_for_name(input.iter(), "a_variable_longer_name", None),
find_best_match_for_name(input.iter(), Symbol::intern("a_variable_longer_name"), None),
Some(Symbol::intern("a_longer_variable_name"))
);
})
14 changes: 9 additions & 5 deletions src/librustc_ast_pretty/pprust.rs
Original file line number Diff line number Diff line change
@@ -522,6 +522,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.word(st)
}

fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
self.print_string(&sym.as_str(), style);
}

fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) {
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
}
@@ -2050,7 +2054,7 @@ impl<'a> State<'a> {
let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r
{
InlineAsmRegOrRegClass::Reg(r) => {
s.print_string(&r.as_str(), ast::StrStyle::Cooked)
s.print_symbol(*r, ast::StrStyle::Cooked)
}
InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()),
};
@@ -2144,7 +2148,7 @@ impl<'a> State<'a> {
ast::ExprKind::LlvmInlineAsm(ref a) => {
self.s.word("llvm_asm!");
self.popen();
self.print_string(&a.asm.as_str(), a.asm_str_style);
self.print_symbol(a.asm, a.asm_str_style);
self.word_space(":");

self.commasep(Inconsistent, &a.outputs, |s, out| {
@@ -2164,16 +2168,16 @@ impl<'a> State<'a> {
self.word_space(":");

self.commasep(Inconsistent, &a.inputs, |s, &(co, ref o)| {
s.print_string(&co.as_str(), ast::StrStyle::Cooked);
s.print_symbol(co, ast::StrStyle::Cooked);
s.popen();
s.print_expr(o);
s.pclose();
});
self.s.space();
self.word_space(":");

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

let mut options = vec![];
8 changes: 4 additions & 4 deletions src/librustc_attr/builtin.rs
Original file line number Diff line number Diff line change
@@ -1041,10 +1041,10 @@ pub fn find_transparency(
break;
} else if let Some(value) = attr.value_str() {
transparency = Some((
match &*value.as_str() {
"transparent" => Transparency::Transparent,
"semitransparent" => Transparency::SemiTransparent,
"opaque" => Transparency::Opaque,
match value {
sym::transparent => Transparency::Transparent,
sym::semitransparent => Transparency::SemiTransparent,
sym::opaque => Transparency::Opaque,
_ => {
error = Some(TransparencyError::UnknownTransparency(value, attr.span));
continue;
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/deriving/clone.rs
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@ pub fn expand_deriving_clone(
is_unsafe: false,
supports_unions: true,
methods: vec![MethodDef {
name: "clone",
name: sym::clone,
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: Vec::new(),
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ pub fn expand_deriving_eq(
is_unsafe: false,
supports_unions: true,
methods: vec![MethodDef {
name: "assert_receiver_is_total_eq",
name: sym::assert_receiver_is_total_eq,
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec![],
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ pub fn expand_deriving_ord(
is_unsafe: false,
supports_unions: false,
methods: vec![MethodDef {
name: "cmp",
name: sym::cmp,
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec![(borrowed_self(), "other")],
4 changes: 2 additions & 2 deletions src/librustc_builtin_macros/deriving/cmp/partial_eq.rs
Original file line number Diff line number Diff line change
@@ -92,9 +92,9 @@ pub fn expand_deriving_partial_eq(
// avoid defining `ne` if we can
// c-like enums, enums without any fields and structs without fields
// can safely define only `eq`.
let mut methods = vec![md!("eq", cs_eq)];
let mut methods = vec![md!(sym::eq, cs_eq)];
if !is_type_without_fields(item) {
methods.push(md!("ne", cs_ne));
methods.push(md!(sym::ne, cs_ne));
}

let trait_def = TraitDef {
24 changes: 12 additions & 12 deletions src/librustc_builtin_macros/deriving/cmp/partial_ord.rs
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ pub fn expand_deriving_partial_ord(
let attrs = vec![cx.attribute(inline)];

let partial_cmp_def = MethodDef {
name: "partial_cmp",
name: sym::partial_cmp,
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec![(borrowed_self(), "other")],
@@ -70,10 +70,10 @@ pub fn expand_deriving_partial_ord(
} else {
vec![
partial_cmp_def,
md!("lt", true, false),
md!("le", true, true),
md!("gt", false, false),
md!("ge", false, true),
md!(sym::lt, true, false),
md!(sym::le, true, true),
md!(sym::gt, false, false),
md!(sym::ge, false, true),
]
};

@@ -108,14 +108,14 @@ pub fn some_ordering_collapsed(
) -> P<ast::Expr> {
let lft = cx.expr_ident(span, self_arg_tags[0]);
let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1]));
let op_str = match op {
PartialCmpOp => "partial_cmp",
LtOp => "lt",
LeOp => "le",
GtOp => "gt",
GeOp => "ge",
let op_sym = match op {
PartialCmpOp => sym::partial_cmp,
LtOp => sym::lt,
LeOp => sym::le,
GtOp => sym::gt,
GeOp => sym::ge,
};
cx.expr_method_call(span, lft, cx.ident_of(op_str, span), vec![rgt])
cx.expr_method_call(span, lft, Ident::new(op_sym, span), vec![rgt])
}

pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/deriving/debug.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ pub fn expand_deriving_debug(
is_unsafe: false,
supports_unions: false,
methods: vec![MethodDef {
name: "fmt",
name: sym::fmt,
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: vec![(fmtr, "f")],
4 changes: 2 additions & 2 deletions src/librustc_builtin_macros/deriving/decodable.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ use rustc_ast::ast;
use rustc_ast::ast::{Expr, MetaItem, Mutability};
use rustc_ast::ptr::P;
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::Symbol;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;

pub fn expand_deriving_rustc_decodable(
@@ -30,7 +30,7 @@ pub fn expand_deriving_rustc_decodable(
is_unsafe: false,
supports_unions: false,
methods: vec![MethodDef {
name: "decode",
name: sym::decode,
generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec![(
Loading