Skip to content

Commit

Permalink
Auto merge of rust-lang#17584 - Veykril:landing-page, r=Veykril
Browse files Browse the repository at this point in the history
Implement symbol interning infra

Will fix rust-lang/rust-analyzer#15590

My unsafe-fu is not the best but it does satisfy miri.

There is still some follow up work to do, notably a lot of places using strings instead of symbols/names, most notably the token tree.
  • Loading branch information
bors committed Jul 15, 2024
2 parents fcbf77f + d95b2f3 commit 305bd5d
Show file tree
Hide file tree
Showing 86 changed files with 1,418 additions and 792 deletions.
2 changes: 2 additions & 0 deletions src/tools/rust-analyzer/.typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extend-ignore-re = [
"\\w*\\.{3,4}\\w*",
'"flate2"',
"raison d'être",
"inout",
"optin"
]

[default.extend-words]
Expand Down
7 changes: 7 additions & 0 deletions src/tools/rust-analyzer/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ dependencies = [
"dashmap",
"hashbrown",
"rustc-hash",
"sptr",
"triomphe",
]

Expand Down Expand Up @@ -1885,6 +1886,12 @@ dependencies = [
"vfs",
]

[[package]]
name = "sptr"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b9b39299b249ad65f3b7e96443bad61c02ca5cd3589f46cb6d610a0fd6c0d6a"

[[package]]
name = "stable_deref_trait"
version = "1.2.0"
Expand Down
15 changes: 12 additions & 3 deletions src/tools/rust-analyzer/crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use hir_expand::{
attrs::{collect_attrs, Attr, AttrId, RawAttrs},
HirFileId, InFile,
};
use intern::{sym, Symbol};
use la_arena::{ArenaMap, Idx, RawIdx};
use mbe::DelimiterKind;
use syntax::{
Expand Down Expand Up @@ -152,7 +153,7 @@ impl Attrs {
}

pub fn lang_item(&self) -> Option<LangItem> {
self.by_key("lang").string_value().and_then(LangItem::from_str)
self.by_key("lang").string_value().and_then(|it| LangItem::from_symbol(&Symbol::intern(it)))
}

pub fn has_doc_hidden(&self) -> bool {
Expand Down Expand Up @@ -199,8 +200,12 @@ impl Attrs {
.segments()
.iter()
.rev()
.zip(["core", "prelude", "v1", "test"].iter().rev())
.all(|it| it.0.as_str() == Some(it.1))
.zip(
[sym::core.clone(), sym::prelude.clone(), sym::v1.clone(), sym::test.clone()]
.iter()
.rev(),
)
.all(|it| it.0 == it.1)
})
}

Expand Down Expand Up @@ -568,6 +573,10 @@ impl<'attr> AttrQuery<'attr> {
self.attrs().find_map(|attr| attr.string_value())
}

pub fn string_value_with_span(self) -> Option<(&'attr str, span::Span)> {
self.attrs().find_map(|attr| attr.string_value_with_span())
}

pub fn string_value_unescape(self) -> Option<Cow<'attr, str>> {
self.attrs().find_map(|attr| attr.string_value_unescape())
}
Expand Down
114 changes: 67 additions & 47 deletions src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::mem;

use base_db::CrateId;
use hir_expand::{
name::{name, AsName, Name},
name::{AsName, Name},
ExpandError, InFile,
};
use intern::Interned;
use intern::{sym, Interned};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use span::AstIdMap;
Expand Down Expand Up @@ -187,8 +187,10 @@ impl ExprCollector<'_> {
{
let is_mutable =
self_param.mut_token().is_some() && self_param.amp_token().is_none();
let binding_id: la_arena::Idx<Binding> =
self.alloc_binding(name![self], BindingAnnotation::new(is_mutable, false));
let binding_id: la_arena::Idx<Binding> = self.alloc_binding(
Name::new_symbol_root(sym::self_.clone()),
BindingAnnotation::new(is_mutable, false),
);
self.body.self_param = Some(binding_id);
self.source_map.self_param = Some(self.expander.in_file(AstPtr::new(&self_param)));
}
Expand Down Expand Up @@ -1588,18 +1590,22 @@ impl ExprCollector<'_> {
});
let mut mappings = vec![];
let fmt = match template.and_then(|it| self.expand_macros_to_string(it)) {
Some((s, is_direct_literal)) => format_args::parse(
&s,
fmt_snippet,
args,
is_direct_literal,
|name| self.alloc_expr_desugared(Expr::Path(Path::from(name))),
|name, span| {
if let Some(span) = span {
mappings.push((span, name))
}
},
),
Some((s, is_direct_literal)) => {
let call_ctx = self.expander.syntax_context();
format_args::parse(
&s,
fmt_snippet,
args,
is_direct_literal,
|name| self.alloc_expr_desugared(Expr::Path(Path::from(name))),
|name, span| {
if let Some(span) = span {
mappings.push((span, name))
}
},
call_ctx,
)
}
None => FormatArgs {
template: Default::default(),
arguments: args.finish(),
Expand Down Expand Up @@ -1723,14 +1729,18 @@ impl ExprCollector<'_> {
// unsafe { ::core::fmt::UnsafeArg::new() }
// )

let Some(new_v1_formatted) =
LangItem::FormatArguments.ty_rel_path(self.db, self.krate, name![new_v1_formatted])
else {
let Some(new_v1_formatted) = LangItem::FormatArguments.ty_rel_path(
self.db,
self.krate,
Name::new_symbol_root(sym::new_v1_formatted.clone()),
) else {
return self.missing_expr();
};
let Some(unsafe_arg_new) =
LangItem::FormatUnsafeArg.ty_rel_path(self.db, self.krate, name![new])
else {
let Some(unsafe_arg_new) = LangItem::FormatUnsafeArg.ty_rel_path(
self.db,
self.krate,
Name::new_symbol_root(sym::new.clone()),
) else {
return self.missing_expr();
};
let new_v1_formatted = self.alloc_expr_desugared(Expr::Path(new_v1_formatted));
Expand Down Expand Up @@ -1812,10 +1822,10 @@ impl ExprCollector<'_> {
self.db,
self.krate,
match alignment {
Some(FormatAlignment::Left) => name![Left],
Some(FormatAlignment::Right) => name![Right],
Some(FormatAlignment::Center) => name![Center],
None => name![Unknown],
Some(FormatAlignment::Left) => Name::new_symbol_root(sym::Left.clone()),
Some(FormatAlignment::Right) => Name::new_symbol_root(sym::Right.clone()),
Some(FormatAlignment::Center) => Name::new_symbol_root(sym::Center.clone()),
None => Name::new_symbol_root(sym::Unknown.clone()),
},
);
match align {
Expand All @@ -1838,8 +1848,11 @@ impl ExprCollector<'_> {
let width = self.make_count(width, argmap);

let format_placeholder_new = {
let format_placeholder_new =
LangItem::FormatPlaceholder.ty_rel_path(self.db, self.krate, name![new]);
let format_placeholder_new = LangItem::FormatPlaceholder.ty_rel_path(
self.db,
self.krate,
Name::new_symbol_root(sym::new.clone()),
);
match format_placeholder_new {
Some(path) => self.alloc_expr_desugared(Expr::Path(path)),
None => self.missing_expr(),
Expand Down Expand Up @@ -1883,11 +1896,14 @@ impl ExprCollector<'_> {
*n as u128,
Some(BuiltinUint::Usize),
)));
let count_is =
match LangItem::FormatCount.ty_rel_path(self.db, self.krate, name![Is]) {
Some(count_is) => self.alloc_expr_desugared(Expr::Path(count_is)),
None => self.missing_expr(),
};
let count_is = match LangItem::FormatCount.ty_rel_path(
self.db,
self.krate,
Name::new_symbol_root(sym::Is.clone()),
) {
Some(count_is) => self.alloc_expr_desugared(Expr::Path(count_is)),
None => self.missing_expr(),
};
self.alloc_expr_desugared(Expr::Call {
callee: count_is,
args: Box::new([args]),
Expand All @@ -1905,7 +1921,7 @@ impl ExprCollector<'_> {
let count_param = match LangItem::FormatCount.ty_rel_path(
self.db,
self.krate,
name![Param],
Name::new_symbol_root(sym::Param.clone()),
) {
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
None => self.missing_expr(),
Expand All @@ -1921,7 +1937,11 @@ impl ExprCollector<'_> {
self.missing_expr()
}
}
None => match LangItem::FormatCount.ty_rel_path(self.db, self.krate, name![Implied]) {
None => match LangItem::FormatCount.ty_rel_path(
self.db,
self.krate,
Name::new_symbol_root(sym::Implied.clone()),
) {
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
None => self.missing_expr(),
},
Expand All @@ -1942,18 +1962,18 @@ impl ExprCollector<'_> {
let new_fn = match LangItem::FormatArgument.ty_rel_path(
self.db,
self.krate,
match ty {
Format(Display) => name![new_display],
Format(Debug) => name![new_debug],
Format(LowerExp) => name![new_lower_exp],
Format(UpperExp) => name![new_upper_exp],
Format(Octal) => name![new_octal],
Format(Pointer) => name![new_pointer],
Format(Binary) => name![new_binary],
Format(LowerHex) => name![new_lower_hex],
Format(UpperHex) => name![new_upper_hex],
Usize => name![from_usize],
},
Name::new_symbol_root(match ty {
Format(Display) => sym::new_display.clone(),
Format(Debug) => sym::new_debug.clone(),
Format(LowerExp) => sym::new_lower_exp.clone(),
Format(UpperExp) => sym::new_upper_exp.clone(),
Format(Octal) => sym::new_octal.clone(),
Format(Pointer) => sym::new_pointer.clone(),
Format(Binary) => sym::new_binary.clone(),
Format(LowerHex) => sym::new_lower_hex.clone(),
Format(UpperHex) => sym::new_upper_hex.clone(),
Usize => sym::from_usize.clone(),
}),
) {
Some(new_fn) => self.alloc_expr_desugared(Expr::Path(new_fn)),
None => self.missing_expr(),
Expand Down
95 changes: 50 additions & 45 deletions src/tools/rust-analyzer/crates/hir-def/src/builtin_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use std::fmt;

use hir_expand::name::{name, AsName, Name};
use hir_expand::name::{AsName, Name};
use intern::sym;
/// Different signed int types.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BuiltinInt {
Expand Down Expand Up @@ -48,63 +49,67 @@ pub enum BuiltinType {

impl BuiltinType {
#[rustfmt::skip]
pub const ALL: &'static [(Name, BuiltinType)] = &[
(name![char], BuiltinType::Char),
(name![bool], BuiltinType::Bool),
(name![str], BuiltinType::Str),

(name![isize], BuiltinType::Int(BuiltinInt::Isize)),
(name![i8], BuiltinType::Int(BuiltinInt::I8)),
(name![i16], BuiltinType::Int(BuiltinInt::I16)),
(name![i32], BuiltinType::Int(BuiltinInt::I32)),
(name![i64], BuiltinType::Int(BuiltinInt::I64)),
(name![i128], BuiltinType::Int(BuiltinInt::I128)),

(name![usize], BuiltinType::Uint(BuiltinUint::Usize)),
(name![u8], BuiltinType::Uint(BuiltinUint::U8)),
(name![u16], BuiltinType::Uint(BuiltinUint::U16)),
(name![u32], BuiltinType::Uint(BuiltinUint::U32)),
(name![u64], BuiltinType::Uint(BuiltinUint::U64)),
(name![u128], BuiltinType::Uint(BuiltinUint::U128)),

(name![f16], BuiltinType::Float(BuiltinFloat::F16)),
(name![f32], BuiltinType::Float(BuiltinFloat::F32)),
(name![f64], BuiltinType::Float(BuiltinFloat::F64)),
(name![f128], BuiltinType::Float(BuiltinFloat::F128)),
];
pub fn all_builtin_types() -> [(Name, BuiltinType); 19] {
[
(Name::new_symbol_root(sym::char.clone()), BuiltinType::Char),
(Name::new_symbol_root(sym::bool.clone()), BuiltinType::Bool),
(Name::new_symbol_root(sym::str.clone()), BuiltinType::Str),

(Name::new_symbol_root(sym::isize.clone()), BuiltinType::Int(BuiltinInt::Isize)),
(Name::new_symbol_root(sym::i8.clone()), BuiltinType::Int(BuiltinInt::I8)),
(Name::new_symbol_root(sym::i16.clone()), BuiltinType::Int(BuiltinInt::I16)),
(Name::new_symbol_root(sym::i32.clone()), BuiltinType::Int(BuiltinInt::I32)),
(Name::new_symbol_root(sym::i64.clone()), BuiltinType::Int(BuiltinInt::I64)),
(Name::new_symbol_root(sym::i128.clone()), BuiltinType::Int(BuiltinInt::I128)),

(Name::new_symbol_root(sym::usize.clone()), BuiltinType::Uint(BuiltinUint::Usize)),
(Name::new_symbol_root(sym::u8.clone()), BuiltinType::Uint(BuiltinUint::U8)),
(Name::new_symbol_root(sym::u16.clone()), BuiltinType::Uint(BuiltinUint::U16)),
(Name::new_symbol_root(sym::u32.clone()), BuiltinType::Uint(BuiltinUint::U32)),
(Name::new_symbol_root(sym::u64.clone()), BuiltinType::Uint(BuiltinUint::U64)),
(Name::new_symbol_root(sym::u128.clone()), BuiltinType::Uint(BuiltinUint::U128)),

(Name::new_symbol_root(sym::f16.clone()), BuiltinType::Float(BuiltinFloat::F16)),
(Name::new_symbol_root(sym::f32.clone()), BuiltinType::Float(BuiltinFloat::F32)),
(Name::new_symbol_root(sym::f64.clone()), BuiltinType::Float(BuiltinFloat::F64)),
(Name::new_symbol_root(sym::f128.clone()), BuiltinType::Float(BuiltinFloat::F128)),
]
}

pub fn by_name(name: &Name) -> Option<Self> {
Self::ALL.iter().find_map(|(n, ty)| if n == name { Some(*ty) } else { None })
Self::all_builtin_types()
.iter()
.find_map(|(n, ty)| if n == name { Some(*ty) } else { None })
}
}

impl AsName for BuiltinType {
fn as_name(&self) -> Name {
match self {
BuiltinType::Char => name![char],
BuiltinType::Bool => name![bool],
BuiltinType::Str => name![str],
BuiltinType::Char => Name::new_symbol_root(sym::char.clone()),
BuiltinType::Bool => Name::new_symbol_root(sym::bool.clone()),
BuiltinType::Str => Name::new_symbol_root(sym::str.clone()),
BuiltinType::Int(it) => match it {
BuiltinInt::Isize => name![isize],
BuiltinInt::I8 => name![i8],
BuiltinInt::I16 => name![i16],
BuiltinInt::I32 => name![i32],
BuiltinInt::I64 => name![i64],
BuiltinInt::I128 => name![i128],
BuiltinInt::Isize => Name::new_symbol_root(sym::isize.clone()),
BuiltinInt::I8 => Name::new_symbol_root(sym::i8.clone()),
BuiltinInt::I16 => Name::new_symbol_root(sym::i16.clone()),
BuiltinInt::I32 => Name::new_symbol_root(sym::i32.clone()),
BuiltinInt::I64 => Name::new_symbol_root(sym::i64.clone()),
BuiltinInt::I128 => Name::new_symbol_root(sym::i128.clone()),
},
BuiltinType::Uint(it) => match it {
BuiltinUint::Usize => name![usize],
BuiltinUint::U8 => name![u8],
BuiltinUint::U16 => name![u16],
BuiltinUint::U32 => name![u32],
BuiltinUint::U64 => name![u64],
BuiltinUint::U128 => name![u128],
BuiltinUint::Usize => Name::new_symbol_root(sym::usize.clone()),
BuiltinUint::U8 => Name::new_symbol_root(sym::u8.clone()),
BuiltinUint::U16 => Name::new_symbol_root(sym::u16.clone()),
BuiltinUint::U32 => Name::new_symbol_root(sym::u32.clone()),
BuiltinUint::U64 => Name::new_symbol_root(sym::u64.clone()),
BuiltinUint::U128 => Name::new_symbol_root(sym::u128.clone()),
},
BuiltinType::Float(it) => match it {
BuiltinFloat::F16 => name![f16],
BuiltinFloat::F32 => name![f32],
BuiltinFloat::F64 => name![f64],
BuiltinFloat::F128 => name![f128],
BuiltinFloat::F16 => Name::new_symbol_root(sym::f16.clone()),
BuiltinFloat::F32 => Name::new_symbol_root(sym::f32.clone()),
BuiltinFloat::F64 => Name::new_symbol_root(sym::f64.clone()),
BuiltinFloat::F128 => Name::new_symbol_root(sym::f128.clone()),
},
}
}
Expand Down
Loading

0 comments on commit 305bd5d

Please sign in to comment.