Skip to content

Commit 96e988f

Browse files
bors[bot]pksunkaraJonas Schievink
authored
Merge #5951 #5975
5951: Rename record_field_pat to record_pat_field r=jonas-schievink a=pksunkara The token was renamed but not this. 5975: Report better errors in project.json/sysroot r=jonas-schievink a=jonas-schievink This does a bunch of light refactoring so that the `Sysroot` is loaded later, which makes sure that any errors are reported to the user. I then added a check that reports an error if libcore is missing in the loaded sysroot. Since a sysroot without libcore is very useless, this indicates a configuration error. Co-authored-by: Pavan Kumar Sunkara <[email protected]> Co-authored-by: Jonas Schievink <[email protected]>
3 parents 87c8dfc + 4d97f5f + 681ac62 commit 96e988f

File tree

14 files changed

+53
-34
lines changed

14 files changed

+53
-34
lines changed

crates/hir/src/semantics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
207207
self.imp.resolve_record_field(field)
208208
}
209209

210-
pub fn resolve_record_field_pat(&self, field: &ast::RecordPatField) -> Option<Field> {
211-
self.imp.resolve_record_field_pat(field)
210+
pub fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<Field> {
211+
self.imp.resolve_record_pat_field(field)
212212
}
213213

214214
pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroDef> {
@@ -433,8 +433,8 @@ impl<'db> SemanticsImpl<'db> {
433433
self.analyze(field.syntax()).resolve_record_field(self.db, field)
434434
}
435435

436-
fn resolve_record_field_pat(&self, field: &ast::RecordPatField) -> Option<Field> {
437-
self.analyze(field.syntax()).resolve_record_field_pat(self.db, field)
436+
fn resolve_record_pat_field(&self, field: &ast::RecordPatField) -> Option<Field> {
437+
self.analyze(field.syntax()).resolve_record_pat_field(self.db, field)
438438
}
439439

440440
fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroDef> {

crates/hir/src/source_analyzer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ impl SourceAnalyzer {
179179
Some((struct_field.into(), local))
180180
}
181181

182-
pub(crate) fn resolve_record_field_pat(
182+
pub(crate) fn resolve_record_pat_field(
183183
&self,
184184
_db: &dyn HirDatabase,
185185
field: &ast::RecordPatField,
186186
) -> Option<Field> {
187187
let pat_id = self.pat_id(&field.pat()?)?;
188-
let struct_field = self.infer.as_ref()?.record_field_pat_resolution(pat_id)?;
188+
let struct_field = self.infer.as_ref()?.record_pat_field_resolution(pat_id)?;
189189
Some(struct_field.into())
190190
}
191191

crates/hir_ty/src/infer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub struct InferenceResult {
125125
field_resolutions: FxHashMap<ExprId, FieldId>,
126126
/// For each field in record literal, records the field it resolves to.
127127
record_field_resolutions: FxHashMap<ExprId, FieldId>,
128-
record_field_pat_resolutions: FxHashMap<PatId, FieldId>,
128+
record_pat_field_resolutions: FxHashMap<PatId, FieldId>,
129129
/// For each struct literal, records the variant it resolves to.
130130
variant_resolutions: FxHashMap<ExprOrPatId, VariantId>,
131131
/// For each associated item record what it resolves to
@@ -146,8 +146,8 @@ impl InferenceResult {
146146
pub fn record_field_resolution(&self, expr: ExprId) -> Option<FieldId> {
147147
self.record_field_resolutions.get(&expr).copied()
148148
}
149-
pub fn record_field_pat_resolution(&self, pat: PatId) -> Option<FieldId> {
150-
self.record_field_pat_resolutions.get(&pat).copied()
149+
pub fn record_pat_field_resolution(&self, pat: PatId) -> Option<FieldId> {
150+
self.record_pat_field_resolutions.get(&pat).copied()
151151
}
152152
pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantId> {
153153
self.variant_resolutions.get(&id.into()).copied()

crates/hir_ty/src/infer/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<'a> InferenceContext<'a> {
7070
let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name));
7171
if let Some(local_id) = matching_field {
7272
let field_def = FieldId { parent: def.unwrap(), local_id };
73-
self.result.record_field_pat_resolutions.insert(subpat.pat, field_def);
73+
self.result.record_pat_field_resolutions.insert(subpat.pat, field_def);
7474
}
7575

7676
let expected_ty =

crates/ide_db/src/defs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option
157157
ast::IdentPat(it) => {
158158
let local = sema.to_def(&it)?;
159159

160-
if let Some(record_field_pat) = it.syntax().parent().and_then(ast::RecordPatField::cast) {
161-
if record_field_pat.name_ref().is_none() {
162-
if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) {
160+
if let Some(record_pat_field) = it.syntax().parent().and_then(ast::RecordPatField::cast) {
161+
if record_pat_field.name_ref().is_none() {
162+
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
163163
let field = Definition::Field(field);
164164
return Some(NameClass::FieldShorthand { local, field });
165165
}
@@ -275,8 +275,8 @@ pub fn classify_name_ref(
275275
}
276276
}
277277

278-
if let Some(record_field_pat) = ast::RecordPatField::cast(parent.clone()) {
279-
if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) {
278+
if let Some(record_pat_field) = ast::RecordPatField::cast(parent.clone()) {
279+
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
280280
let field = Definition::Field(field);
281281
return Some(NameRefClass::Definition(field));
282282
}

crates/parser/src/grammar/patterns.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ fn tuple_pat_fields(p: &mut Parser) {
188188
p.expect(T![')']);
189189
}
190190

191-
// test record_field_pat_list
191+
// test record_pat_field_list
192192
// fn foo() {
193193
// let S {} = ();
194194
// let S { f, ref mut g } = ();
@@ -208,7 +208,7 @@ fn record_pat_field_list(p: &mut Parser) {
208208
c => {
209209
let m = p.start();
210210
match c {
211-
// test record_field_pat
211+
// test record_pat_field
212212
// fn foo() {
213213
// let S { 0: 1 } = ();
214214
// let S { x: 1 } = ();

crates/project_model/src/lib.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub enum ProjectWorkspace {
3333
/// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`.
3434
Cargo { cargo: CargoWorkspace, sysroot: Sysroot },
3535
/// Project workspace was manually specified using a `rust-project.json` file.
36-
Json { project: ProjectJson },
36+
Json { project: ProjectJson, sysroot: Option<Sysroot> },
3737
}
3838

3939
impl fmt::Debug for ProjectWorkspace {
@@ -44,10 +44,10 @@ impl fmt::Debug for ProjectWorkspace {
4444
.field("n_packages", &cargo.packages().len())
4545
.field("n_sysroot_crates", &sysroot.crates().len())
4646
.finish(),
47-
ProjectWorkspace::Json { project } => {
47+
ProjectWorkspace::Json { project, sysroot } => {
4848
let mut debug_struct = f.debug_struct("Json");
4949
debug_struct.field("n_crates", &project.n_crates());
50-
if let Some(sysroot) = &project.sysroot {
50+
if let Some(sysroot) = sysroot {
5151
debug_struct.field("n_sysroot_crates", &sysroot.crates().len());
5252
}
5353
debug_struct.finish()
@@ -169,7 +169,11 @@ impl ProjectWorkspace {
169169
})?;
170170
let project_location = project_json.parent().unwrap().to_path_buf();
171171
let project = ProjectJson::new(&project_location, data);
172-
ProjectWorkspace::Json { project }
172+
let sysroot = match &project.sysroot_src {
173+
Some(path) => Some(Sysroot::load(path)?),
174+
None => None,
175+
};
176+
ProjectWorkspace::Json { project, sysroot }
173177
}
174178
ProjectManifest::CargoToml(cargo_toml) => {
175179
let cargo_version = utf8_stdout({
@@ -203,12 +207,21 @@ impl ProjectWorkspace {
203207
Ok(res)
204208
}
205209

210+
pub fn load_inline(project_json: ProjectJson) -> Result<ProjectWorkspace> {
211+
let sysroot = match &project_json.sysroot_src {
212+
Some(path) => Some(Sysroot::load(path)?),
213+
None => None,
214+
};
215+
216+
Ok(ProjectWorkspace::Json { project: project_json, sysroot })
217+
}
218+
206219
/// Returns the roots for the current `ProjectWorkspace`
207220
/// The return type contains the path and whether or not
208221
/// the root is a member of the current workspace
209222
pub fn to_roots(&self) -> Vec<PackageRoot> {
210223
match self {
211-
ProjectWorkspace::Json { project } => project
224+
ProjectWorkspace::Json { project, sysroot } => project
212225
.crates()
213226
.map(|(_, krate)| PackageRoot {
214227
is_member: krate.is_workspace_member,
@@ -217,7 +230,7 @@ impl ProjectWorkspace {
217230
})
218231
.collect::<FxHashSet<_>>()
219232
.into_iter()
220-
.chain(project.sysroot.as_ref().into_iter().flat_map(|sysroot| {
233+
.chain(sysroot.as_ref().into_iter().flat_map(|sysroot| {
221234
sysroot.crates().map(move |krate| PackageRoot {
222235
is_member: false,
223236
include: vec![sysroot[krate].root_dir().to_path_buf()],
@@ -255,7 +268,7 @@ impl ProjectWorkspace {
255268

256269
pub fn proc_macro_dylib_paths(&self) -> Vec<AbsPathBuf> {
257270
match self {
258-
ProjectWorkspace::Json { project } => project
271+
ProjectWorkspace::Json { project, sysroot: _ } => project
259272
.crates()
260273
.filter_map(|(_, krate)| krate.proc_macro_dylib_path.as_ref())
261274
.cloned()
@@ -285,9 +298,8 @@ impl ProjectWorkspace {
285298
) -> CrateGraph {
286299
let mut crate_graph = CrateGraph::default();
287300
match self {
288-
ProjectWorkspace::Json { project } => {
289-
let sysroot_dps = project
290-
.sysroot
301+
ProjectWorkspace::Json { project, sysroot } => {
302+
let sysroot_dps = sysroot
291303
.as_ref()
292304
.map(|sysroot| sysroot_to_crate_graph(&mut crate_graph, sysroot, target, load));
293305

crates/project_model/src/project_json.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use paths::{AbsPath, AbsPathBuf};
77
use rustc_hash::FxHashMap;
88
use serde::{de, Deserialize};
99

10-
use crate::{cfg_flag::CfgFlag, Sysroot};
10+
use crate::cfg_flag::CfgFlag;
1111

1212
/// Roots and crates that compose this Rust project.
1313
#[derive(Clone, Debug, Eq, PartialEq)]
1414
pub struct ProjectJson {
15-
pub(crate) sysroot: Option<Sysroot>,
15+
pub(crate) sysroot_src: Option<AbsPathBuf>,
1616
crates: Vec<Crate>,
1717
}
1818

@@ -35,7 +35,7 @@ pub struct Crate {
3535
impl ProjectJson {
3636
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
3737
ProjectJson {
38-
sysroot: data.sysroot_src.map(|it| base.join(it)).map(|it| Sysroot::load(&it)),
38+
sysroot_src: data.sysroot_src.map(|it| base.join(it)),
3939
crates: data
4040
.crates
4141
.into_iter()

crates/project_model/src/sysroot.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ impl Sysroot {
5151
pub fn discover(cargo_toml: &AbsPath) -> Result<Sysroot> {
5252
let current_dir = cargo_toml.parent().unwrap();
5353
let sysroot_src_dir = discover_sysroot_src_dir(current_dir)?;
54-
let res = Sysroot::load(&sysroot_src_dir);
54+
let res = Sysroot::load(&sysroot_src_dir)?;
5555
Ok(res)
5656
}
5757

58-
pub fn load(sysroot_src_dir: &AbsPath) -> Sysroot {
58+
pub fn load(sysroot_src_dir: &AbsPath) -> Result<Sysroot> {
5959
let mut sysroot = Sysroot { crates: Arena::default() };
6060

6161
for name in SYSROOT_CRATES.trim().lines() {
@@ -89,7 +89,14 @@ impl Sysroot {
8989
}
9090
}
9191

92-
sysroot
92+
if sysroot.by_name("core").is_none() {
93+
anyhow::bail!(
94+
"could not find libcore in sysroot path `{}`",
95+
sysroot_src_dir.as_ref().display()
96+
);
97+
}
98+
99+
Ok(sysroot)
93100
}
94101

95102
fn by_name(&self, name: &str) -> Option<SysrootCrate> {

crates/rust-analyzer/src/reload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl GlobalState {
109109
)
110110
}
111111
LinkedProject::InlineJsonProject(it) => {
112-
Ok(project_model::ProjectWorkspace::Json { project: it.clone() })
112+
project_model::ProjectWorkspace::load_inline(it.clone())
113113
}
114114
})
115115
.collect::<Vec<_>>();

0 commit comments

Comments
 (0)