Skip to content

Commit

Permalink
Auto merge of rust-lang#134377 - lnicola:sync-from-ra, r=lnicola
Browse files Browse the repository at this point in the history
Subtree update of `rust-analyzer`

r? `@ghost`
  • Loading branch information
bors committed Dec 16, 2024
2 parents eedc229 + 5c6bae0 commit 83ab648
Show file tree
Hide file tree
Showing 71 changed files with 8,793 additions and 2,177 deletions.
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/.github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ jobs:
timeout-minutes: 10
env:
FORCE_COLOR: 1
TYPOS_VERSION: v1.18.0
TYPOS_VERSION: v1.28.3
steps:
- name: download typos
run: curl -LsSf https://github.com/crate-ci/typos/releases/download/$TYPOS_VERSION/typos-$TYPOS_VERSION-x86_64-unknown-linux-musl.tar.gz | tar zxf - -C ${CARGO_HOME:-~/.cargo}/bin
Expand Down
7 changes: 6 additions & 1 deletion src/tools/rust-analyzer/.typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ extend-ignore-re = [
"raison d'être",
"inout",
"INOUT",
"optin"
"optin",
"=Pn",
]

[default.extend-words]
Expand All @@ -26,8 +27,12 @@ fo = "fo"
ket = "ket"
makro = "makro"
trivias = "trivias"
thir = "thir"
jod = "jod"

[default.extend-identifiers]
anc = "anc"
datas = "datas"
impl_froms = "impl_froms"
selfs = "selfs"
taits = "taits"
6 changes: 6 additions & 0 deletions src/tools/rust-analyzer/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bda8e21c04aca2ae33ffc2fd8c23134f3cac46db123ba97bd9d3f3b8a4a85e1"

[[package]]
name = "edition"
version = "0.0.0"

[[package]]
name = "either"
version = "1.13.0"
Expand Down Expand Up @@ -1266,6 +1270,7 @@ name = "parser"
version = "0.0.0"
dependencies = [
"drop_bomb",
"edition",
"expect-test",
"limit",
"ra-ap-rustc_lexer",
Expand Down Expand Up @@ -2662,6 +2667,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"directories",
"edition",
"either",
"flate2",
"itertools",
Expand Down
1 change: 1 addition & 0 deletions src/tools/rust-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ toolchain = { path = "./crates/toolchain", version = "0.0.0" }
tt = { path = "./crates/tt", version = "0.0.0" }
vfs-notify = { path = "./crates/vfs-notify", version = "0.0.0" }
vfs = { path = "./crates/vfs", version = "0.0.0" }
edition = { path = "./crates/edition", version = "0.0.0" }

ra-ap-rustc_lexer = { version = "0.85", default-features = false }
ra-ap-rustc_parse_format = { version = "0.85", default-features = false }
Expand Down
13 changes: 13 additions & 0 deletions src/tools/rust-analyzer/crates/edition/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "edition"
version = "0.0.0"
rust-version.workspace = true
edition.workspace = true
license.workspace = true
authors.workspace = true
repository.workspace = true

[dependencies]

[lints]
workspace = true
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! The edition of the Rust language used in a crate.
// Ideally this would be defined in the span crate, but the dependency chain is all over the place
// wrt to span, parser and syntax.
// This should live in a separate crate because we use it in both actual code and codegen.
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl BuiltinFnLikeExpander {
}

pub fn is_asm(&self) -> bool {
matches!(self, Self::Asm | Self::GlobalAsm)
matches!(self, Self::Asm | Self::GlobalAsm | Self::NakedAsm)
}
}

Expand Down Expand Up @@ -122,6 +122,7 @@ register_builtin! {
(stringify, Stringify) => stringify_expand,
(asm, Asm) => asm_expand,
(global_asm, GlobalAsm) => asm_expand,
(naked_asm, NakedAsm) => asm_expand,
(cfg, Cfg) => cfg_expand,
(core_panic, CorePanic) => panic_expand,
(std_panic, StdPanic) => panic_expand,
Expand Down
17 changes: 11 additions & 6 deletions src/tools/rust-analyzer/crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use rustc_hash::FxHashMap;
use stdx::TupleExt;
use triomphe::Arc;

use core::fmt;
use std::hash::Hash;

use base_db::{ra_salsa::InternValueTrivial, CrateId};
Expand Down Expand Up @@ -147,6 +148,10 @@ impl ExpandError {
pub fn span(&self) -> Span {
self.inner.1
}

pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> RenderedExpandError {
self.inner.0.render_to_string(db)
}
}

#[derive(Debug, PartialEq, Eq, Clone, Hash)]
Expand All @@ -164,18 +169,18 @@ pub enum ExpandErrorKind {
ProcMacroPanic(Box<str>),
}

impl ExpandError {
pub fn render_to_string(&self, db: &dyn ExpandDatabase) -> RenderedExpandError {
self.inner.0.render_to_string(db)
}
}

pub struct RenderedExpandError {
pub message: String,
pub error: bool,
pub kind: &'static str,
}

impl fmt::Display for RenderedExpandError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}

impl RenderedExpandError {
const GENERAL_KIND: &str = "macro-error";
}
Expand Down
17 changes: 15 additions & 2 deletions src/tools/rust-analyzer/crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,8 +1053,21 @@ impl HirDisplay for Ty {
generic_args_sans_defaults(f, Some(generic_def_id), parameters);
assert!(params_len >= parameters.len());
let defaults = params_len - parameters.len();
let without_impl =
self_param as usize + type_ + const_ + lifetime - defaults;

// Normally, functions cannot have default parameters, but they can,
// for function-like things such as struct names or enum variants.
// The former cannot have defaults but parents, and the later cannot have
// parents but defaults.
// So, if `parent_len` > 0, it have a parent and thus it doesn't have any
// default. Therefore, we shouldn't subtract defaults because those defaults
// are from their parents.
// And if `parent_len` == 0, either parents don't exists or they don't have
// any defaults. Thus, we can - and should - subtract defaults.
let without_impl = if parent_len > 0 {
params_len - parent_len - impl_
} else {
params_len - parent_len - impl_ - defaults
};
// parent's params (those from enclosing impl or trait, if any).
let (fn_params, parent_params) = parameters.split_at(without_impl + impl_);

Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-ty/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl chalk_ir::interner::Interner for Interner {
type InternedConst = Interned<InternedWrapper<ConstData>>;
type InternedConcreteConst = ConstScalar;
type InternedGenericArg = GenericArgData;
// We could do the following, but that saves "only" 20mb on self while increasing inferecene
// We could do the following, but that saves "only" 20mb on self while increasing inference
// time by ~2.5%
// type InternedGoal = Interned<InternedWrapper<GoalData>>;
type InternedGoal = Arc<GoalData>;
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rust-analyzer/crates/hir-ty/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4380,7 +4380,7 @@ fn test() {
fn associated_type_in_struct_expr_path() {
// FIXME: All annotation should be resolvable.
// For lines marked as unstable, see rust-lang/rust#86935.
// FIXME: Remove the comments once stablized.
// FIXME: Remove the comments once stabilized.
check_types(
r#"
trait Trait {
Expand Down Expand Up @@ -4416,7 +4416,7 @@ impl Trait for () {
fn associated_type_in_struct_expr_path_enum() {
// FIXME: All annotation should be resolvable.
// For lines marked as unstable, see rust-lang/rust#86935.
// FIXME: Remove the comments once stablized.
// FIXME: Remove the comments once stabilized.
check_types(
r#"
trait Trait {
Expand Down
100 changes: 28 additions & 72 deletions src/tools/rust-analyzer/crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2649,24 +2649,31 @@ impl Const {
Type::from_value_def(db, self.id)
}

/// Evaluate the constant and return the result as a string.
///
/// This function is intended for IDE assistance, different from [`Const::render_eval`].
pub fn eval(self, db: &dyn HirDatabase) -> Result<String, ConstEvalError> {
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
Ok(format!("{}", c.display(db, self.krate(db).edition(db))))
/// Evaluate the constant.
pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst, ConstEvalError> {
db.const_eval(self.id.into(), Substitution::empty(Interner), None)
.map(|it| EvaluatedConst { const_: it, def: self.id.into() })
}
}

/// Evaluate the constant and return the result as a string, with more detailed information.
///
/// This function is intended for user-facing display.
pub fn render_eval(
self,
db: &dyn HirDatabase,
edition: Edition,
) -> Result<String, ConstEvalError> {
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
let data = &c.data(Interner);
impl HasVisibility for Const {
fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
db.const_visibility(self.id)
}
}

pub struct EvaluatedConst {
def: DefWithBodyId,
const_: hir_ty::Const,
}

impl EvaluatedConst {
pub fn render(&self, db: &dyn HirDatabase, edition: Edition) -> String {
format!("{}", self.const_.display(db, edition))
}

pub fn render_debug(&self, db: &dyn HirDatabase) -> Result<String, MirEvalError> {
let data = self.const_.data(Interner);
if let TyKind::Scalar(s) = data.ty.kind(Interner) {
if matches!(s, Scalar::Int(_) | Scalar::Uint(_)) {
if let hir_ty::ConstValue::Concrete(c) = &data.value {
Expand All @@ -2689,17 +2696,7 @@ impl Const {
}
}
}
if let Ok(s) = mir::render_const_using_debug_impl(db, self.id.into(), &c) {
Ok(s)
} else {
Ok(format!("{}", c.display(db, edition)))
}
}
}

impl HasVisibility for Const {
fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
db.const_visibility(self.id)
mir::render_const_using_debug_impl(db, self.def, &self.const_)
}
}

Expand Down Expand Up @@ -2729,51 +2726,10 @@ impl Static {
Type::from_value_def(db, self.id)
}

/// Evaluate the static and return the result as a string.
///
/// This function is intended for IDE assistance, different from [`Static::render_eval`].
pub fn eval(self, db: &dyn HirDatabase) -> Result<String, ConstEvalError> {
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
Ok(format!("{}", c.display(db, self.krate(db).edition(db))))
}

/// Evaluate the static and return the result as a string, with more detailed information.
///
/// This function is intended for user-facing display.
pub fn render_eval(
self,
db: &dyn HirDatabase,
edition: Edition,
) -> Result<String, ConstEvalError> {
let c = db.const_eval(self.id.into(), Substitution::empty(Interner), None)?;
let data = &c.data(Interner);
if let TyKind::Scalar(s) = data.ty.kind(Interner) {
if matches!(s, Scalar::Int(_) | Scalar::Uint(_)) {
if let hir_ty::ConstValue::Concrete(c) = &data.value {
if let hir_ty::ConstScalar::Bytes(b, _) = &c.interned {
let value = u128::from_le_bytes(mir::pad16(b, false));
let value_signed =
i128::from_le_bytes(mir::pad16(b, matches!(s, Scalar::Int(_))));
let mut result = if let Scalar::Int(_) = s {
value_signed.to_string()
} else {
value.to_string()
};
if value >= 10 {
format_to!(result, " ({value:#X})");
return Ok(result);
} else {
return Ok(result);
}
}
}
}
}
if let Ok(s) = mir::render_const_using_debug_impl(db, self.id.into(), &c) {
Ok(s)
} else {
Ok(format!("{}", c.display(db, edition)))
}
/// Evaluate the static initializer.
pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst, ConstEvalError> {
db.const_eval(self.id.into(), Substitution::empty(Interner), None)
.map(|it| EvaluatedConst { const_: it, def: self.id.into() })
}
}

Expand Down
Loading

0 comments on commit 83ab648

Please sign in to comment.