Skip to content

Commit ced592a

Browse files
committed
Auto merge of #114003 - lnicola:sync-from-ra, r=lnicola
⬆️ `rust-analyzer` r? `@ghost`
2 parents 155a5c2 + 6a94418 commit ced592a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+717
-405
lines changed

src/tools/rust-analyzer/.github/workflows/ci.yaml

+11-4
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,21 @@ jobs:
161161
# if: runner.os == 'Linux'
162162
# working-directory: ./editors/code
163163

164+
# If this steps fails, your code's type integrity might be wrong at some places at TypeScript level.
165+
- run: npm run typecheck
166+
working-directory: ./editors/code
167+
if: needs.changes.outputs.typescript == 'true'
168+
169+
# You may fix the code automatically by running `npm run lint:fix` if this steps fails.
164170
- run: npm run lint
165171
working-directory: ./editors/code
166172
if: needs.changes.outputs.typescript == 'true'
167173

174+
# To fix this steps, please run `npm run format`.
175+
- run: npm run format:check
176+
working-directory: ./editors/code
177+
if: needs.changes.outputs.typescript == 'true'
178+
168179
- name: Run VS Code tests (Linux)
169180
if: matrix.os == 'ubuntu-latest' && needs.changes.outputs.typescript == 'true'
170181
env:
@@ -179,10 +190,6 @@ jobs:
179190
run: npm test
180191
working-directory: ./editors/code
181192

182-
- run: npm run pretest
183-
working-directory: ./editors/code
184-
if: needs.changes.outputs.typescript == 'true'
185-
186193
- run: npm run package --scripts-prepend-node-path
187194
working-directory: ./editors/code
188195
if: needs.changes.outputs.typescript == 'true'

src/tools/rust-analyzer/crates/hir-def/src/body/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ impl Printer<'_> {
634634
match literal {
635635
Literal::String(it) => w!(self, "{:?}", it),
636636
Literal::ByteString(it) => w!(self, "\"{}\"", it.escape_ascii()),
637-
Literal::CString(it) => w!(self, "\"{}\\0\"", it),
637+
Literal::CString(it) => w!(self, "\"{}\\0\"", it.escape_ascii()),
638638
Literal::Char(it) => w!(self, "'{}'", it.escape_debug()),
639639
Literal::Bool(it) => w!(self, "{}", it),
640640
Literal::Int(i, suffix) => {

src/tools/rust-analyzer/crates/hir-def/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl fmt::Display for FloatTypeWrapper {
8585
pub enum Literal {
8686
String(Box<str>),
8787
ByteString(Box<[u8]>),
88-
CString(Box<str>),
88+
CString(Box<[u8]>),
8989
Char(char),
9090
Bool(bool),
9191
Int(i128, Option<BuiltinInt>),

src/tools/rust-analyzer/crates/hir-ty/src/consteval.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use triomphe::Arc;
1616
use crate::{
1717
db::HirDatabase, infer::InferenceContext, lower::ParamLoweringMode,
1818
mir::monomorphize_mir_body_bad, to_placeholder_idx, utils::Generics, Const, ConstData,
19-
ConstScalar, ConstValue, GenericArg, Interner, MemoryMap, Substitution, Ty, TyBuilder,
19+
ConstScalar, ConstValue, GenericArg, Interner, MemoryMap, Substitution, TraitEnvironment, Ty,
20+
TyBuilder,
2021
};
2122

2223
use super::mir::{interpret_mir, lower_to_mir, pad16, MirEvalError, MirLowerError};
@@ -135,7 +136,7 @@ pub fn intern_const_ref(
135136
ty: Ty,
136137
krate: CrateId,
137138
) -> Const {
138-
let layout = db.layout_of_ty(ty.clone(), krate);
139+
let layout = db.layout_of_ty(ty.clone(), Arc::new(TraitEnvironment::empty(krate)));
139140
let bytes = match value {
140141
LiteralConstRef::Int(i) => {
141142
// FIXME: We should handle failure of layout better.
@@ -173,7 +174,7 @@ pub fn try_const_usize(db: &dyn HirDatabase, c: &Const) -> Option<u128> {
173174
chalk_ir::ConstValue::Concrete(c) => match &c.interned {
174175
ConstScalar::Bytes(it, _) => Some(u128::from_le_bytes(pad16(&it, false))),
175176
ConstScalar::UnevaluatedConst(c, subst) => {
176-
let ec = db.const_eval(*c, subst.clone()).ok()?;
177+
let ec = db.const_eval(*c, subst.clone(), None).ok()?;
177178
try_const_usize(db, &ec)
178179
}
179180
_ => None,
@@ -186,6 +187,7 @@ pub(crate) fn const_eval_recover(
186187
_: &[String],
187188
_: &GeneralConstId,
188189
_: &Substitution,
190+
_: &Option<Arc<TraitEnvironment>>,
189191
) -> Result<Const, ConstEvalError> {
190192
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
191193
}
@@ -210,6 +212,7 @@ pub(crate) fn const_eval_query(
210212
db: &dyn HirDatabase,
211213
def: GeneralConstId,
212214
subst: Substitution,
215+
trait_env: Option<Arc<TraitEnvironment>>,
213216
) -> Result<Const, ConstEvalError> {
214217
let body = match def {
215218
GeneralConstId::ConstId(c) => {
@@ -228,7 +231,7 @@ pub(crate) fn const_eval_query(
228231
}
229232
GeneralConstId::InTypeConstId(c) => db.mir_body(c.into())?,
230233
};
231-
let c = interpret_mir(db, body, false).0?;
234+
let c = interpret_mir(db, body, false, trait_env).0?;
232235
Ok(c)
233236
}
234237

@@ -241,7 +244,7 @@ pub(crate) fn const_eval_static_query(
241244
Substitution::empty(Interner),
242245
db.trait_environment_for_body(def.into()),
243246
)?;
244-
let c = interpret_mir(db, body, false).0?;
247+
let c = interpret_mir(db, body, false, None).0?;
245248
Ok(c)
246249
}
247250

@@ -268,7 +271,7 @@ pub(crate) fn const_eval_discriminant_variant(
268271
Substitution::empty(Interner),
269272
db.trait_environment_for_body(def),
270273
)?;
271-
let c = interpret_mir(db, mir_body, false).0?;
274+
let c = interpret_mir(db, mir_body, false, None).0?;
272275
let c = try_const_usize(db, &c).unwrap() as i128;
273276
Ok(c)
274277
}
@@ -293,7 +296,7 @@ pub(crate) fn eval_to_const(
293296
}
294297
let infer = ctx.clone().resolve_all();
295298
if let Ok(mir_body) = lower_to_mir(ctx.db, ctx.owner, &ctx.body, &infer, expr) {
296-
if let Ok(result) = interpret_mir(db, Arc::new(mir_body), true).0 {
299+
if let Ok(result) = interpret_mir(db, Arc::new(mir_body), true, None).0 {
297300
return result;
298301
}
299302
}

src/tools/rust-analyzer/crates/hir-ty/src/consteval/tests.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn eval_goal(db: &TestDB, file_id: FileId) -> Result<Const, ConstEvalError> {
114114
_ => None,
115115
})
116116
.expect("No const named GOAL found in the test");
117-
db.const_eval(const_id.into(), Substitution::empty(Interner))
117+
db.const_eval(const_id.into(), Substitution::empty(Interner), None)
118118
}
119119

120120
#[test]
@@ -1941,6 +1941,33 @@ fn dyn_trait() {
19411941
"#,
19421942
900,
19431943
);
1944+
check_number(
1945+
r#"
1946+
//- minicore: coerce_unsized, index, slice
1947+
trait A {
1948+
fn x(&self) -> i32;
1949+
}
1950+
1951+
trait B: A {}
1952+
1953+
impl A for i32 {
1954+
fn x(&self) -> i32 {
1955+
5
1956+
}
1957+
}
1958+
1959+
impl B for i32 {
1960+
1961+
}
1962+
1963+
const fn f(x: &dyn B) -> i32 {
1964+
x.x()
1965+
}
1966+
1967+
const GOAL: i32 = f(&2i32);
1968+
"#,
1969+
5,
1970+
);
19441971
}
19451972

19461973
#[test]
@@ -2492,6 +2519,28 @@ fn const_trait_assoc() {
24922519
"#,
24932520
5,
24942521
);
2522+
check_number(
2523+
r#"
2524+
//- minicore: size_of
2525+
//- /a/lib.rs crate:a
2526+
use core::mem::size_of;
2527+
pub struct S<T>(T);
2528+
impl<T> S<T> {
2529+
pub const X: usize = core::mem::size_of::<T>();
2530+
}
2531+
//- /main.rs crate:main deps:a
2532+
use a::{S};
2533+
trait Tr {
2534+
type Ty;
2535+
}
2536+
impl Tr for i32 {
2537+
type Ty = u64;
2538+
}
2539+
struct K<T: Tr>(<T as Tr>::Ty);
2540+
const GOAL: usize = S::<K<i32>>::X;
2541+
"#,
2542+
8,
2543+
);
24952544
check_number(
24962545
r#"
24972546
struct S<T>(*mut T);

src/tools/rust-analyzer/crates/hir-ty/src/db.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
7777

7878
#[salsa::invoke(crate::consteval::const_eval_query)]
7979
#[salsa::cycle(crate::consteval::const_eval_recover)]
80-
fn const_eval(&self, def: GeneralConstId, subst: Substitution)
81-
-> Result<Const, ConstEvalError>;
80+
fn const_eval(
81+
&self,
82+
def: GeneralConstId,
83+
subst: Substitution,
84+
trait_env: Option<Arc<crate::TraitEnvironment>>,
85+
) -> Result<Const, ConstEvalError>;
8286

8387
#[salsa::invoke(crate::consteval::const_eval_static_query)]
8488
#[salsa::cycle(crate::consteval::const_eval_static_recover)]
@@ -100,12 +104,16 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
100104
&self,
101105
def: AdtId,
102106
subst: Substitution,
103-
krate: CrateId,
107+
env: Arc<crate::TraitEnvironment>,
104108
) -> Result<Arc<Layout>, LayoutError>;
105109

106110
#[salsa::invoke(crate::layout::layout_of_ty_query)]
107111
#[salsa::cycle(crate::layout::layout_of_ty_recover)]
108-
fn layout_of_ty(&self, ty: Ty, krate: CrateId) -> Result<Arc<Layout>, LayoutError>;
112+
fn layout_of_ty(
113+
&self,
114+
ty: Ty,
115+
env: Arc<crate::TraitEnvironment>,
116+
) -> Result<Arc<Layout>, LayoutError>;
109117

110118
#[salsa::invoke(crate::layout::target_data_layout_query)]
111119
fn target_data_layout(&self, krate: CrateId) -> Option<Arc<TargetDataLayout>>;

0 commit comments

Comments
 (0)