Skip to content

Commit 139b49b

Browse files
committed
Auto merge of rust-lang#114576 - lnicola:sync-from-ra, r=lnicola
⬆️ `rust-analyzer` r? `@ghost`
2 parents adb15a2 + a42f832 commit 139b49b

File tree

139 files changed

+4248
-1042
lines changed

Some content is hidden

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

139 files changed

+4248
-1042
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ impl AttrsWithOwner {
485485
},
486486
AttrDefId::ExternBlockId(it) => attrs_from_item_tree_loc(db, it),
487487
AttrDefId::ExternCrateId(it) => attrs_from_item_tree_loc(db, it),
488+
AttrDefId::UseId(it) => attrs_from_item_tree_loc(db, it),
488489
};
489490

490491
let attrs = raw_attrs.filter(db.upcast(), def.krate(db));
@@ -570,6 +571,7 @@ impl AttrsWithOwner {
570571
},
571572
AttrDefId::ExternBlockId(id) => any_has_attrs(db, id),
572573
AttrDefId::ExternCrateId(id) => any_has_attrs(db, id),
574+
AttrDefId::UseId(id) => any_has_attrs(db, id),
573575
};
574576

575577
AttrSourceMap::new(owner.as_ref().map(|node| node as &dyn HasAttrs))

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

+29-10
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,7 @@ impl ExprCollector<'_> {
313313
let body = self.collect_labelled_block_opt(label, e.loop_body());
314314
self.alloc_expr(Expr::Loop { body, label }, syntax_ptr)
315315
}
316-
ast::Expr::WhileExpr(e) => {
317-
let label = e.label().map(|label| self.collect_label(label));
318-
let body = self.collect_labelled_block_opt(label, e.loop_body());
319-
let condition = self.collect_expr_opt(e.condition());
320-
321-
self.alloc_expr(Expr::While { condition, body, label }, syntax_ptr)
322-
}
316+
ast::Expr::WhileExpr(e) => self.collect_while_loop(syntax_ptr, e),
323317
ast::Expr::ForExpr(e) => self.collect_for_loop(syntax_ptr, e),
324318
ast::Expr::CallExpr(e) => {
325319
let is_rustc_box = {
@@ -731,6 +725,32 @@ impl ExprCollector<'_> {
731725
expr_id
732726
}
733727

728+
/// Desugar `ast::WhileExpr` from: `[opt_ident]: while <cond> <body>` into:
729+
/// ```ignore (pseudo-rust)
730+
/// [opt_ident]: loop {
731+
/// if <cond> {
732+
/// <body>
733+
/// }
734+
/// else {
735+
/// break;
736+
/// }
737+
/// }
738+
/// ```
739+
/// FIXME: Rustc wraps the condition in a construct equivalent to `{ let _t = <cond>; _t }`
740+
/// to preserve drop semantics. We should probably do the same in future.
741+
fn collect_while_loop(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::WhileExpr) -> ExprId {
742+
let label = e.label().map(|label| self.collect_label(label));
743+
let body = self.collect_labelled_block_opt(label, e.loop_body());
744+
let condition = self.collect_expr_opt(e.condition());
745+
let break_expr =
746+
self.alloc_expr(Expr::Break { expr: None, label: None }, syntax_ptr.clone());
747+
let if_expr = self.alloc_expr(
748+
Expr::If { condition, then_branch: body, else_branch: Some(break_expr) },
749+
syntax_ptr.clone(),
750+
);
751+
self.alloc_expr(Expr::Loop { body: if_expr, label }, syntax_ptr)
752+
}
753+
734754
/// Desugar `ast::ForExpr` from: `[opt_ident]: for <pat> in <head> <body>` into:
735755
/// ```ignore (pseudo-rust)
736756
/// match IntoIterator::into_iter(<head>) {
@@ -893,15 +913,14 @@ impl ExprCollector<'_> {
893913
self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
894914
}
895915

896-
fn collect_macro_call<F, T, U>(
916+
fn collect_macro_call<T, U>(
897917
&mut self,
898918
mcall: ast::MacroCall,
899919
syntax_ptr: AstPtr<ast::MacroCall>,
900920
record_diagnostics: bool,
901-
collector: F,
921+
collector: impl FnOnce(&mut Self, Option<T>) -> U,
902922
) -> U
903923
where
904-
F: FnOnce(&mut Self, Option<T>) -> U,
905924
T: ast::AstNode,
906925
{
907926
// File containing the macro call. Expansion errors will be attached here.

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

-8
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,6 @@ impl Printer<'_> {
178178
w!(self, "loop ");
179179
self.print_expr(*body);
180180
}
181-
Expr::While { condition, body, label } => {
182-
if let Some(lbl) = label {
183-
w!(self, "{}: ", self.body[*lbl].name.display(self.db));
184-
}
185-
w!(self, "while ");
186-
self.print_expr(*condition);
187-
self.print_expr(*body);
188-
}
189181
Expr::Call { callee, args, is_assignee_expr: _ } => {
190182
self.print_expr(*callee);
191183
w!(self, "(");

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

-5
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,6 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
228228
scopes.set_scope(expr, scope);
229229
compute_block_scopes(statements, *tail, body, scopes, &mut scope);
230230
}
231-
Expr::While { condition, body: body_expr, label } => {
232-
let mut scope = scopes.new_labeled_scope(*scope, make_label(label));
233-
compute_expr_scopes(*condition, body, scopes, &mut scope);
234-
compute_expr_scopes(*body_expr, body, scopes, &mut scope);
235-
}
236231
Expr::Loop { body: body_expr, label } => {
237232
let mut scope = scopes.new_labeled_scope(*scope, make_label(label));
238233
compute_expr_scopes(*body_expr, body, scopes, &mut scope);

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

+21-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::{
1414
item_scope::ItemScope,
1515
nameres::DefMap,
1616
src::{HasChildSource, HasSource},
17-
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, FieldId, ImplId, Lookup, MacroId,
18-
ModuleDefId, ModuleId, TraitId, VariantId,
17+
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ExternCrateId, FieldId, ImplId,
18+
Lookup, MacroId, ModuleDefId, ModuleId, TraitId, UseId, VariantId,
1919
};
2020

2121
pub trait ChildBySource {
@@ -91,6 +91,8 @@ impl ChildBySource for ItemScope {
9191
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
9292
self.declarations().for_each(|item| add_module_def(db, res, file_id, item));
9393
self.impls().for_each(|imp| add_impl(db, res, file_id, imp));
94+
self.extern_crate_decls().for_each(|ext| add_extern_crate(db, res, file_id, ext));
95+
self.use_decls().for_each(|ext| add_use(db, res, file_id, ext));
9496
self.unnamed_consts().for_each(|konst| {
9597
let loc = konst.lookup(db);
9698
if loc.id.file_id() == file_id {
@@ -167,6 +169,23 @@ impl ChildBySource for ItemScope {
167169
map[keys::IMPL].insert(loc.source(db).value, imp)
168170
}
169171
}
172+
fn add_extern_crate(
173+
db: &dyn DefDatabase,
174+
map: &mut DynMap,
175+
file_id: HirFileId,
176+
ext: ExternCrateId,
177+
) {
178+
let loc = ext.lookup(db);
179+
if loc.id.file_id() == file_id {
180+
map[keys::EXTERN_CRATE].insert(loc.source(db).value, ext)
181+
}
182+
}
183+
fn add_use(db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId, ext: UseId) {
184+
let loc = ext.lookup(db);
185+
if loc.id.file_id() == file_id {
186+
map[keys::USE].insert(loc.source(db).value, ext)
187+
}
188+
}
170189
}
171190
}
172191

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

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
pub mod adt;
44

5+
use base_db::CrateId;
56
use hir_expand::{
67
name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefKind,
78
};
@@ -467,6 +468,7 @@ pub struct ExternCrateDeclData {
467468
pub name: Name,
468469
pub alias: Option<ImportAlias>,
469470
pub visibility: RawVisibility,
471+
pub crate_id: Option<CrateId>,
470472
}
471473

472474
impl ExternCrateDeclData {
@@ -478,10 +480,21 @@ impl ExternCrateDeclData {
478480
let item_tree = loc.id.item_tree(db);
479481
let extern_crate = &item_tree[loc.id.value];
480482

483+
let name = extern_crate.name.clone();
484+
let crate_id = if name == hir_expand::name![self] {
485+
Some(loc.container.krate())
486+
} else {
487+
db.crate_def_map(loc.container.krate())
488+
.extern_prelude()
489+
.find(|&(prelude_name, ..)| *prelude_name == name)
490+
.map(|(_, root)| root.krate())
491+
};
492+
481493
Arc::new(Self {
482494
name: extern_crate.name.clone(),
483495
visibility: item_tree[extern_crate.visibility].clone(),
484496
alias: extern_crate.alias.clone(),
497+
crate_id,
485498
})
486499
}
487500
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ use crate::{
2323
visibility::{self, Visibility},
2424
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstBlockLoc, ConstId, ConstLoc, DefWithBodyId,
2525
EnumId, EnumLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc, FunctionId,
26-
FunctionLoc, GenericDefId, ImplId, ImplLoc, ImportId, ImportLoc, InTypeConstId, InTypeConstLoc,
27-
LocalEnumVariantId, LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc,
28-
ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId, StructLoc, TraitAliasId,
29-
TraitAliasLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, VariantId,
26+
FunctionLoc, GenericDefId, ImplId, ImplLoc, InTypeConstId, InTypeConstLoc, LocalEnumVariantId,
27+
LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc, ProcMacroId, ProcMacroLoc,
28+
StaticId, StaticLoc, StructId, StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc,
29+
TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, VariantId,
3030
};
3131

3232
#[salsa::query_group(InternDatabaseStorage)]
3333
pub trait InternDatabase: SourceDatabase {
3434
// region: items
3535
#[salsa::interned]
36-
fn intern_import(&self, loc: ImportLoc) -> ImportId;
36+
fn intern_use(&self, loc: UseLoc) -> UseId;
3737
#[salsa::interned]
3838
fn intern_extern_crate(&self, loc: ExternCrateLoc) -> ExternCrateId;
3939
#[salsa::interned]

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
dyn_map::{DynMap, Policy},
1111
ConstId, EnumId, EnumVariantId, ExternCrateId, FieldId, FunctionId, ImplId, LifetimeParamId,
1212
Macro2Id, MacroRulesId, ProcMacroId, StaticId, StructId, TraitAliasId, TraitId, TypeAliasId,
13-
TypeOrConstParamId, UnionId,
13+
TypeOrConstParamId, UnionId, UseId,
1414
};
1515

1616
pub type Key<K, V> = crate::dyn_map::Key<K, V, AstPtrPolicy<K, V>>;
@@ -26,6 +26,7 @@ pub const STRUCT: Key<ast::Struct, StructId> = Key::new();
2626
pub const UNION: Key<ast::Union, UnionId> = Key::new();
2727
pub const ENUM: Key<ast::Enum, EnumId> = Key::new();
2828
pub const EXTERN_CRATE: Key<ast::ExternCrate, ExternCrateId> = Key::new();
29+
pub const USE: Key<ast::Use, UseId> = Key::new();
2930

3031
pub const VARIANT: Key<ast::Variant, EnumVariantId> = Key::new();
3132
pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new();

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

+20-12
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,26 @@ impl Expander {
164164
return ExpandResult { value: None, err };
165165
};
166166

167-
Self::enter_expand_inner(db, call_id, err).map(|value| {
168-
value.and_then(|InFile { file_id, value }| {
169-
let parse = value.cast::<T>()?;
170-
171-
self.recursion_depth += 1;
172-
self.hygiene = Hygiene::new(db.upcast(), file_id);
173-
let old_file_id = std::mem::replace(&mut self.current_file_id, file_id);
174-
let mark =
175-
Mark { file_id: old_file_id, bomb: DropBomb::new("expansion mark dropped") };
176-
Some((mark, parse))
177-
})
178-
})
167+
let res = Self::enter_expand_inner(db, call_id, err);
168+
match res.err {
169+
// If proc-macro is disabled or unresolved, we want to expand to a missing expression
170+
// instead of an empty tree which might end up in an empty block.
171+
Some(ExpandError::UnresolvedProcMacro(_)) => res.map(|_| None),
172+
_ => res.map(|value| {
173+
value.and_then(|InFile { file_id, value }| {
174+
let parse = value.cast::<T>()?;
175+
176+
self.recursion_depth += 1;
177+
self.hygiene = Hygiene::new(db.upcast(), file_id);
178+
let old_file_id = std::mem::replace(&mut self.current_file_id, file_id);
179+
let mark = Mark {
180+
file_id: old_file_id,
181+
bomb: DropBomb::new("expansion mark dropped"),
182+
};
183+
Some((mark, parse))
184+
})
185+
}),
186+
}
179187
}
180188
}
181189

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

-9
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,6 @@ pub enum Expr {
191191
body: ExprId,
192192
label: Option<LabelId>,
193193
},
194-
While {
195-
condition: ExprId,
196-
body: ExprId,
197-
label: Option<LabelId>,
198-
},
199194
Call {
200195
callee: ExprId,
201196
args: Box<[ExprId]>,
@@ -379,10 +374,6 @@ impl Expr {
379374
}
380375
}
381376
Expr::Loop { body, .. } => f(*body),
382-
Expr::While { condition, body, .. } => {
383-
f(*condition);
384-
f(*body);
385-
}
386377
Expr::Call { callee, args, .. } => {
387378
f(*callee);
388379
args.iter().copied().for_each(f);

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

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use syntax::ast;
1616
use crate::{
1717
db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ConstId,
1818
ExternCrateId, HasModule, ImplId, LocalModuleId, MacroId, ModuleDefId, ModuleId, TraitId,
19+
UseId,
1920
};
2021

2122
#[derive(Copy, Clone, Debug)]
@@ -113,6 +114,17 @@ impl ItemScope {
113114
self.declarations.iter().copied()
114115
}
115116

117+
pub fn extern_crate_decls(
118+
&self,
119+
) -> impl Iterator<Item = ExternCrateId> + ExactSizeIterator + '_ {
120+
self.extern_crate_decls.iter().copied()
121+
}
122+
123+
pub fn use_decls(&self) -> impl Iterator<Item = UseId> + ExactSizeIterator + '_ {
124+
// FIXME: to be implemented
125+
std::iter::empty()
126+
}
127+
116128
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
117129
self.impls.iter().copied()
118130
}

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl ItemTree {
188188
fn shrink_to_fit(&mut self) {
189189
if let Some(data) = &mut self.data {
190190
let ItemTreeData {
191-
imports,
191+
uses,
192192
extern_crates,
193193
extern_blocks,
194194
functions,
@@ -211,7 +211,7 @@ impl ItemTree {
211211
vis,
212212
} = &mut **data;
213213

214-
imports.shrink_to_fit();
214+
uses.shrink_to_fit();
215215
extern_crates.shrink_to_fit();
216216
extern_blocks.shrink_to_fit();
217217
functions.shrink_to_fit();
@@ -262,7 +262,7 @@ static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(P
262262

263263
#[derive(Default, Debug, Eq, PartialEq)]
264264
struct ItemTreeData {
265-
imports: Arena<Import>,
265+
uses: Arena<Use>,
266266
extern_crates: Arena<ExternCrate>,
267267
extern_blocks: Arena<ExternBlock>,
268268
functions: Arena<Function>,
@@ -486,7 +486,7 @@ macro_rules! mod_items {
486486
}
487487

488488
mod_items! {
489-
Import in imports -> ast::Use,
489+
Use in uses -> ast::Use,
490490
ExternCrate in extern_crates -> ast::ExternCrate,
491491
ExternBlock in extern_blocks -> ast::ExternBlock,
492492
Function in functions -> ast::Fn,
@@ -541,7 +541,7 @@ impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
541541
}
542542

543543
#[derive(Debug, Clone, Eq, PartialEq)]
544-
pub struct Import {
544+
pub struct Use {
545545
pub visibility: RawVisibilityId,
546546
pub ast_id: FileAstId<ast::Use>,
547547
pub use_tree: UseTree,
@@ -744,7 +744,7 @@ pub struct MacroDef {
744744
pub ast_id: FileAstId<ast::MacroDef>,
745745
}
746746

747-
impl Import {
747+
impl Use {
748748
/// Maps a `UseTree` contained in this import back to its AST node.
749749
pub fn use_tree_to_ast(
750750
&self,
@@ -870,7 +870,7 @@ macro_rules! impl_froms {
870870
impl ModItem {
871871
pub fn as_assoc_item(&self) -> Option<AssocItem> {
872872
match self {
873-
ModItem::Import(_)
873+
ModItem::Use(_)
874874
| ModItem::ExternCrate(_)
875875
| ModItem::ExternBlock(_)
876876
| ModItem::Struct(_)
@@ -892,7 +892,7 @@ impl ModItem {
892892

893893
pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> {
894894
match self {
895-
ModItem::Import(it) => tree[it.index].ast_id().upcast(),
895+
ModItem::Use(it) => tree[it.index].ast_id().upcast(),
896896
ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(),
897897
ModItem::ExternBlock(it) => tree[it.index].ast_id().upcast(),
898898
ModItem::Function(it) => tree[it.index].ast_id().upcast(),

0 commit comments

Comments
 (0)