1mod child_by_source;
4mod source_to_def;
5
6use std::{
7 cell::RefCell,
8 convert::Infallible,
9 fmt, iter, mem,
10 ops::{self, ControlFlow, Not},
11};
12
13use base_db::{FxIndexSet, all_crates, toolchain_channel};
14use either::Either;
15use hir_def::{
16 BuiltinDeriveImplId, DefWithBodyId, ExpressionStoreOwnerId, GenericDefId, HasModule, MacroId,
17 StructId, TraitId, VariantId,
18 attrs::parse_extra_crate_attrs,
19 expr_store::{Body, ExprOrPatSource, ExpressionStore, HygieneId, path::Path},
20 hir::{BindingId, Expr, ExprId, ExprOrPatId},
21 nameres::{ModuleOrigin, crate_def_map},
22 resolver::{self, HasResolver, Resolver, TypeNs, ValueNs},
23 type_ref::Mutability,
24};
25use hir_expand::{
26 EditionedFileId, ExpandResult, FileRange, HirFileId, InMacroFile, MacroCallId,
27 attrs::AstPathExt,
28 builtin::{BuiltinFnLikeExpander, EagerExpander},
29 db::ExpandDatabase,
30 files::{FileRangeWrapper, HirFileRange, InRealFile},
31 mod_path::{ModPath, PathKind},
32 name::AsName,
33};
34use hir_ty::{
35 InferBodyId, InferenceResult,
36 db::AnonConstId,
37 diagnostics::unsafe_operations,
38 infer_query_with_inspect,
39 next_solver::{
40 AnyImplId, DbInterner,
41 format_proof_tree::{ProofTreeData, dump_proof_tree_structured},
42 },
43};
44use intern::{Interned, Symbol, sym};
45use itertools::Itertools;
46use rustc_hash::{FxHashMap, FxHashSet};
47use smallvec::{SmallVec, smallvec};
48use span::{FileId, SyntaxContext};
49use stdx::{TupleExt, always};
50use syntax::{
51 AstNode, AstPtr, AstToken, Direction, SmolStr, SmolStrBuilder, SyntaxElement, SyntaxKind,
52 SyntaxNode, SyntaxNodePtr, SyntaxToken, T, TextRange, TextSize,
53 algo::skip_trivia_token,
54 ast::{self, HasAttrs as _, HasGenericParams},
55};
56
57use crate::{
58 Adjust, Adjustment, Adt, AnyFunctionId, AutoBorrow, BindingMode, BuiltinAttr, Callable, Const,
59 ConstParam, Crate, DeriveHelper, Enum, EnumVariant, ExpressionStoreOwner, Field, Function,
60 GenericSubstitution, HasSource, Impl, InFile, InlineAsmOperand, ItemInNs, Label, LifetimeParam,
61 Local, Macro, Module, ModuleDef, Name, OverloadedDeref, ScopeDef, Static, Struct, ToolModule,
62 Trait, TupleField, Type, TypeAlias, TypeParam, Union, Variant,
63 db::HirDatabase,
64 semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
65 source_analyzer::{SourceAnalyzer, resolve_hir_path},
66};
67
68const CONTINUE_NO_BREAKS: ControlFlow<Infallible, ()> = ControlFlow::Continue(());
69
70#[derive(Debug, Copy, Clone, PartialEq, Eq)]
71pub enum PathResolution {
72 Def(ModuleDef),
74 Local(Local),
76 TypeParam(TypeParam),
78 ConstParam(ConstParam),
80 SelfType(Impl),
81 BuiltinAttr(BuiltinAttr),
82 ToolModule(ToolModule),
83 DeriveHelper(DeriveHelper),
84}
85
86impl PathResolution {
87 pub(crate) fn in_type_ns(&self) -> Option<TypeNs> {
88 match self {
89 PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())),
90 PathResolution::Def(ModuleDef::BuiltinType(builtin)) => {
91 Some(TypeNs::BuiltinType((*builtin).into()))
92 }
93 PathResolution::Def(
94 ModuleDef::Const(_)
95 | ModuleDef::EnumVariant(_)
96 | ModuleDef::Macro(_)
97 | ModuleDef::Function(_)
98 | ModuleDef::Module(_)
99 | ModuleDef::Static(_)
100 | ModuleDef::Trait(_),
101 ) => None,
102 PathResolution::Def(ModuleDef::TypeAlias(alias)) => {
103 Some(TypeNs::TypeAliasId((*alias).into()))
104 }
105 PathResolution::BuiltinAttr(_)
106 | PathResolution::ToolModule(_)
107 | PathResolution::Local(_)
108 | PathResolution::DeriveHelper(_)
109 | PathResolution::ConstParam(_) => None,
110 PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())),
111 PathResolution::SelfType(impl_def) => match impl_def.id {
112 AnyImplId::ImplId(id) => Some(TypeNs::SelfType(id)),
113 AnyImplId::BuiltinDeriveImplId(_) => None,
114 },
115 }
116 }
117}
118
119#[derive(Debug, Copy, Clone, PartialEq, Eq)]
120pub struct PathResolutionPerNs {
121 pub type_ns: Option<PathResolution>,
122 pub value_ns: Option<PathResolution>,
123 pub macro_ns: Option<PathResolution>,
124}
125
126impl PathResolutionPerNs {
127 pub fn new(
128 type_ns: Option<PathResolution>,
129 value_ns: Option<PathResolution>,
130 macro_ns: Option<PathResolution>,
131 ) -> Self {
132 PathResolutionPerNs { type_ns, value_ns, macro_ns }
133 }
134 pub fn any(&self) -> Option<PathResolution> {
135 self.type_ns.or(self.value_ns).or(self.macro_ns)
136 }
137}
138
139#[derive(Debug)]
140pub struct TypeInfo<'db> {
141 pub original: Type<'db>,
143 pub adjusted: Option<Type<'db>>,
145}
146
147impl<'db> TypeInfo<'db> {
148 pub fn original(self) -> Type<'db> {
149 self.original
150 }
151
152 pub fn has_adjustment(&self) -> bool {
153 self.adjusted.is_some()
154 }
155
156 pub fn adjusted(self) -> Type<'db> {
158 self.adjusted.unwrap_or(self.original)
159 }
160}
161
162pub struct Semantics<'db, DB: ?Sized> {
164 pub db: &'db DB,
165 imp: SemanticsImpl<'db>,
166}
167
168type DefWithoutBodyWithAnonConsts = Either<GenericDefId, VariantId>;
169type ExprToAnonConst = FxHashMap<ExprId, AnonConstId>;
170type DefAnonConstsMap = FxHashMap<DefWithoutBodyWithAnonConsts, ExprToAnonConst>;
171
172pub struct SemanticsImpl<'db> {
173 pub db: &'db dyn HirDatabase,
174 s2d_cache: RefCell<SourceToDefCache<'db>>,
175 macro_call_cache: RefCell<FxHashMap<InFile<ast::MacroCall>, MacroCallId>>,
177 signature_anon_consts_cache: RefCell<DefAnonConstsMap>,
179}
180
181impl<DB: ?Sized> fmt::Debug for Semantics<'_, DB> {
182 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183 write!(f, "Semantics {{ ... }}")
184 }
185}
186
187impl<'db, DB: ?Sized> ops::Deref for Semantics<'db, DB> {
188 type Target = SemanticsImpl<'db>;
189
190 fn deref(&self) -> &Self::Target {
191 &self.imp
192 }
193}
194
195#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
196pub enum LintAttr {
197 Allow,
198 Expect,
199 Warn,
200 Deny,
201 Forbid,
202}
203
204impl Semantics<'_, dyn HirDatabase> {
208 pub fn new_dyn(db: &'_ dyn HirDatabase) -> Semantics<'_, dyn HirDatabase> {
210 let impl_ = SemanticsImpl::new(db);
211 Semantics { db, imp: impl_ }
212 }
213}
214
215impl<DB: HirDatabase> Semantics<'_, DB> {
216 pub fn new(db: &DB) -> Semantics<'_, DB> {
218 let impl_ = SemanticsImpl::new(db);
219 Semantics { db, imp: impl_ }
220 }
221}
222
223impl<DB: HirDatabase + ?Sized> Semantics<'_, DB> {
226 pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId {
227 self.imp.find_file(syntax_node).file_id
228 }
229
230 pub fn token_ancestors_with_macros(
231 &self,
232 token: SyntaxToken,
233 ) -> impl Iterator<Item = SyntaxNode> + '_ {
234 token.parent().into_iter().flat_map(move |it| self.ancestors_with_macros(it))
235 }
236
237 pub fn find_node_at_offset_with_macros<N: AstNode>(
240 &self,
241 node: &SyntaxNode,
242 offset: TextSize,
243 ) -> Option<N> {
244 self.imp.ancestors_at_offset_with_macros(node, offset).find_map(N::cast)
245 }
246
247 pub fn find_node_at_offset_with_descend<N: AstNode>(
251 &self,
252 node: &SyntaxNode,
253 offset: TextSize,
254 ) -> Option<N> {
255 self.imp.descend_node_at_offset(node, offset).flatten().find_map(N::cast)
256 }
257
258 pub fn find_nodes_at_offset_with_descend<'slf, N: AstNode + 'slf>(
262 &'slf self,
263 node: &SyntaxNode,
264 offset: TextSize,
265 ) -> impl Iterator<Item = N> + 'slf {
266 self.imp.descend_node_at_offset(node, offset).filter_map(|mut it| it.find_map(N::cast))
267 }
268
269 pub fn find_namelike_at_offset_with_descend<'slf>(
271 &'slf self,
272 node: &SyntaxNode,
273 offset: TextSize,
274 ) -> impl Iterator<Item = ast::NameLike> + 'slf {
275 node.token_at_offset(offset)
276 .map(move |token| self.descend_into_macros_no_opaque(token, true))
277 .map(|descendants| descendants.into_iter().filter_map(move |it| it.value.parent()))
278 .kmerge_by(|left, right| left.text_range().len().lt(&right.text_range().len()))
281 .filter_map(ast::NameLike::cast)
282 }
283
284 pub fn lint_attrs(
285 &self,
286 file_id: FileId,
287 krate: Crate,
288 item: ast::AnyHasAttrs,
289 ) -> impl DoubleEndedIterator<Item = (LintAttr, SmolStr)> {
290 let mut cfg_options = None;
291 let cfg_options = || *cfg_options.get_or_insert_with(|| krate.id.cfg_options(self.db));
292
293 let is_crate_root = file_id == krate.root_file(self.imp.db);
294 let is_source_file = ast::SourceFile::can_cast(item.syntax().kind());
295 let extra_crate_attrs = (is_crate_root && is_source_file)
296 .then(|| {
297 parse_extra_crate_attrs(self.imp.db, krate.id)
298 .into_iter()
299 .flat_map(|src| src.attrs())
300 })
301 .into_iter()
302 .flatten();
303
304 let mut result = Vec::new();
305 hir_expand::attrs::expand_cfg_attr::<Infallible>(
306 extra_crate_attrs.chain(ast::attrs_including_inner(&item)),
307 cfg_options,
308 |attr, _| {
309 let ast::Meta::TokenTreeMeta(attr) = attr else {
310 return ControlFlow::Continue(());
311 };
312 let (Some(segment), Some(tt)) = (attr.path().as_one_segment(), attr.token_tree())
313 else {
314 return ControlFlow::Continue(());
315 };
316 let lint_attr = match &*segment {
317 "allow" => LintAttr::Allow,
318 "expect" => LintAttr::Expect,
319 "warn" => LintAttr::Warn,
320 "deny" => LintAttr::Deny,
321 "forbid" => LintAttr::Forbid,
322 _ => return ControlFlow::Continue(()),
323 };
324 let mut lint = SmolStrBuilder::new();
325 for token in
326 tt.syntax().children_with_tokens().filter_map(SyntaxElement::into_token)
327 {
328 match token.kind() {
329 T![:] | T![::] => lint.push_str(token.text()),
330 kind if kind.is_any_identifier() => lint.push_str(token.text()),
331 T![,] => {
332 let lint = mem::replace(&mut lint, SmolStrBuilder::new()).finish();
333 if !lint.is_empty() {
334 result.push((lint_attr, lint));
335 }
336 }
337 _ => {}
338 }
339 }
340 let lint = lint.finish();
341 if !lint.is_empty() {
342 result.push((lint_attr, lint));
343 }
344
345 ControlFlow::Continue(())
346 },
347 );
348 result.into_iter()
349 }
350
351 pub fn resolve_range_pat(&self, range_pat: &ast::RangePat) -> Option<Struct> {
352 self.imp.resolve_range_pat(range_pat).map(Struct::from)
353 }
354
355 pub fn resolve_range_expr(&self, range_expr: &ast::RangeExpr) -> Option<Struct> {
356 self.imp.resolve_range_expr(range_expr).map(Struct::from)
357 }
358
359 pub fn resolve_await_to_poll(&self, await_expr: &ast::AwaitExpr) -> Option<Function> {
360 self.imp.resolve_await_to_poll(await_expr)
361 }
362
363 pub fn resolve_prefix_expr(&self, prefix_expr: &ast::PrefixExpr) -> Option<Function> {
364 self.imp.resolve_prefix_expr(prefix_expr)
365 }
366
367 pub fn resolve_index_expr(&self, index_expr: &ast::IndexExpr) -> Option<Function> {
368 self.imp.resolve_index_expr(index_expr)
369 }
370
371 pub fn resolve_bin_expr(&self, bin_expr: &ast::BinExpr) -> Option<Function> {
372 self.imp.resolve_bin_expr(bin_expr)
373 }
374
375 pub fn resolve_try_expr(&self, try_expr: &ast::TryExpr) -> Option<Function> {
376 self.imp.resolve_try_expr(try_expr)
377 }
378
379 pub fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<Variant> {
380 self.imp.resolve_variant(record_lit).map(Variant::from)
381 }
382
383 pub fn file_to_module_def(&self, file: impl Into<FileId>) -> Option<Module> {
384 self.imp.file_to_module_defs(file.into()).next()
385 }
386
387 pub fn file_to_module_defs(&self, file: impl Into<FileId>) -> impl Iterator<Item = Module> {
388 self.imp.file_to_module_defs(file.into())
389 }
390
391 pub fn hir_file_to_module_def(&self, file: impl Into<HirFileId>) -> Option<Module> {
392 self.imp.hir_file_to_module_defs(file.into()).next()
393 }
394
395 pub fn hir_file_to_module_defs(
396 &self,
397 file: impl Into<HirFileId>,
398 ) -> impl Iterator<Item = Module> {
399 self.imp.hir_file_to_module_defs(file.into())
400 }
401
402 pub fn is_nightly(&self, krate: Crate) -> bool {
403 let toolchain = toolchain_channel(self.db.as_dyn_database(), krate.into());
404 matches!(toolchain, Some(base_db::ReleaseChannel::Nightly) | None)
407 }
408
409 pub fn to_adt_def(&self, a: &ast::Adt) -> Option<Adt> {
410 self.imp.to_def(a)
411 }
412
413 pub fn to_const_def(&self, c: &ast::Const) -> Option<Const> {
414 self.imp.to_def(c)
415 }
416
417 pub fn to_enum_def(&self, e: &ast::Enum) -> Option<Enum> {
418 self.imp.to_def(e)
419 }
420
421 pub fn to_enum_variant_def(&self, v: &ast::Variant) -> Option<EnumVariant> {
422 self.imp.to_def(v)
423 }
424
425 pub fn to_fn_def(&self, f: &ast::Fn) -> Option<Function> {
426 self.imp.to_def(f)
427 }
428
429 pub fn to_impl_def(&self, i: &ast::Impl) -> Option<Impl> {
430 self.imp.to_def(i)
431 }
432
433 pub fn to_macro_def(&self, m: &ast::Macro) -> Option<Macro> {
434 self.imp.to_def(m)
435 }
436
437 pub fn to_module_def(&self, m: &ast::Module) -> Option<Module> {
438 self.imp.to_def(m)
439 }
440
441 pub fn to_static_def(&self, s: &ast::Static) -> Option<Static> {
442 self.imp.to_def(s)
443 }
444
445 pub fn to_struct_def(&self, s: &ast::Struct) -> Option<Struct> {
446 self.imp.to_def(s)
447 }
448
449 pub fn to_trait_def(&self, t: &ast::Trait) -> Option<Trait> {
450 self.imp.to_def(t)
451 }
452
453 pub fn to_type_alias_def(&self, t: &ast::TypeAlias) -> Option<TypeAlias> {
454 self.imp.to_def(t)
455 }
456
457 pub fn to_union_def(&self, u: &ast::Union) -> Option<Union> {
458 self.imp.to_def(u)
459 }
460}
461
462impl<'db> SemanticsImpl<'db> {
463 fn new(db: &'db dyn HirDatabase) -> Self {
464 SemanticsImpl {
465 db,
466 s2d_cache: Default::default(),
467 macro_call_cache: Default::default(),
468 signature_anon_consts_cache: Default::default(),
469 }
470 }
471
472 pub fn parse(&self, file_id: EditionedFileId) -> ast::SourceFile {
473 let hir_file_id = file_id.into();
474 let tree = file_id.parse(self.db).tree();
475 self.cache(tree.syntax().clone(), hir_file_id);
476 tree
477 }
478
479 pub fn first_crate(&self, file: FileId) -> Option<Crate> {
481 match self.file_to_module_defs(file).next() {
482 Some(module) => Some(module.krate(self.db)),
483 None => all_crates(self.db).last().copied().map(Into::into),
484 }
485 }
486
487 pub fn attach_first_edition_opt(&self, file: FileId) -> Option<EditionedFileId> {
488 let krate = self.file_to_module_defs(file).next()?.krate(self.db);
489 Some(EditionedFileId::new(self.db, file, krate.edition(self.db)))
490 }
491
492 pub fn attach_first_edition(&self, file: FileId) -> EditionedFileId {
493 self.attach_first_edition_opt(file)
494 .unwrap_or_else(|| EditionedFileId::current_edition(self.db, file))
495 }
496
497 pub fn parse_guess_edition(&self, file_id: FileId) -> ast::SourceFile {
498 let file_id = self.attach_first_edition(file_id);
499
500 let tree = file_id.parse(self.db).tree();
501 self.cache(tree.syntax().clone(), file_id.into());
502 tree
503 }
504
505 pub fn adjust_edition(&self, file_id: HirFileId) -> HirFileId {
506 if let Some(editioned_file_id) = file_id.file_id() {
507 self.attach_first_edition_opt(editioned_file_id.file_id(self.db))
508 .map_or(file_id, Into::into)
509 } else {
510 file_id
511 }
512 }
513
514 pub fn find_parent_file(&self, file_id: HirFileId) -> Option<InFile<SyntaxNode>> {
515 match file_id {
516 HirFileId::FileId(file_id) => {
517 let module = self.file_to_module_defs(file_id.file_id(self.db)).next()?;
518 let def_map = crate_def_map(self.db, module.krate(self.db).id);
519 match def_map[module.id].origin {
520 ModuleOrigin::CrateRoot { .. } => None,
521 ModuleOrigin::File { declaration, declaration_tree_id, .. } => {
522 let file_id = declaration_tree_id.file_id();
523 let in_file = InFile::new(file_id, declaration);
524 let node = in_file.to_node(self.db);
525 let root = find_root(node.syntax());
526 self.cache(root, file_id);
527 Some(in_file.with_value(node.syntax().clone()))
528 }
529 _ => unreachable!("FileId can only belong to a file module"),
530 }
531 }
532 HirFileId::MacroFile(macro_file) => {
533 let node = macro_file.loc(self.db).to_node(self.db);
534 let root = find_root(&node.value);
535 self.cache(root, node.file_id);
536 Some(node)
537 }
538 }
539 }
540
541 pub fn module_definition_node(&self, module: Module) -> InFile<SyntaxNode> {
544 let def_map = module.id.def_map(self.db);
545 let definition = def_map[module.id].origin.definition_source(self.db);
546 let definition = definition.map(|it| it.node());
547 let root_node = find_root(&definition.value);
548 self.cache(root_node, definition.file_id);
549 definition
550 }
551
552 pub fn parse_or_expand(&self, file_id: HirFileId) -> SyntaxNode {
553 let node = self.db.parse_or_expand(file_id);
554 self.cache(node.clone(), file_id);
555 node
556 }
557
558 pub fn to_node_syntax(&self, ptr: InFile<SyntaxNodePtr>) -> SyntaxNode {
559 ptr.value.to_node(&self.parse_or_expand(ptr.file_id))
560 }
561
562 pub fn to_node<N: AstNode>(&self, ptr: InFile<AstPtr<N>>) -> N {
563 ptr.value.to_node(&self.parse_or_expand(ptr.file_id))
564 }
565
566 pub fn expand(&self, file_id: MacroCallId) -> ExpandResult<SyntaxNode> {
567 let res = self.db.parse_macro_expansion(file_id).as_ref().map(|it| it.0.syntax_node());
568 self.cache(res.value.clone(), file_id.into());
569 res
570 }
571
572 pub fn expand_macro_call(&self, macro_call: &ast::MacroCall) -> Option<InFile<SyntaxNode>> {
573 let file_id = self.to_def(macro_call)?;
574 let node = self.parse_or_expand(file_id.into());
575 Some(InFile::new(file_id.into(), node))
576 }
577
578 pub fn expand_allowed_builtins(
581 &self,
582 macro_call: &ast::MacroCall,
583 ) -> Option<ExpandResult<SyntaxNode>> {
584 let file_id = self.to_def(macro_call)?;
585 let macro_call = file_id.loc(self.db);
586
587 let skip = matches!(
588 macro_call.def.kind,
589 hir_expand::MacroDefKind::BuiltIn(
590 _,
591 BuiltinFnLikeExpander::Column
592 | BuiltinFnLikeExpander::File
593 | BuiltinFnLikeExpander::ModulePath
594 | BuiltinFnLikeExpander::Asm
595 | BuiltinFnLikeExpander::GlobalAsm
596 | BuiltinFnLikeExpander::NakedAsm
597 | BuiltinFnLikeExpander::LogSyntax
598 | BuiltinFnLikeExpander::TraceMacros
599 | BuiltinFnLikeExpander::FormatArgs
600 | BuiltinFnLikeExpander::FormatArgsNl
601 | BuiltinFnLikeExpander::ConstFormatArgs,
602 ) | hir_expand::MacroDefKind::BuiltInEager(_, EagerExpander::CompileError)
603 );
604 if skip {
605 return None;
608 }
609
610 let node = self.expand(file_id);
611 Some(node)
612 }
613
614 pub fn expand_attr_macro(&self, item: &ast::Item) -> Option<ExpandResult<InFile<SyntaxNode>>> {
616 let src = self.wrap_node_infile(item.clone());
617 let macro_call_id = self.with_ctx(|ctx| ctx.item_to_macro_call(src.as_ref()))?;
618 Some(self.expand(macro_call_id).map(|it| InFile::new(macro_call_id.into(), it)))
619 }
620
621 pub fn expand_derive_as_pseudo_attr_macro(&self, attr: &ast::Meta) -> Option<SyntaxNode> {
622 let adt = attr.parent_attr()?.syntax().parent().and_then(ast::Adt::cast)?;
623 let src = self.wrap_node_infile(attr.clone());
624 let call_id = self.with_ctx(|ctx| {
625 ctx.attr_to_derive_macro_call(src.with_value(&adt), src).map(|(_, it, _)| it)
626 })?;
627 Some(self.parse_or_expand(call_id.into()))
628 }
629
630 pub fn resolve_derive_macro(&self, attr: &ast::Meta) -> Option<Vec<Option<Macro>>> {
631 let calls = self.derive_macro_calls(attr)?;
632 self.with_ctx(|ctx| {
633 Some(
634 calls
635 .into_iter()
636 .map(|call| {
637 let call = call?;
638 match call {
639 Either::Left(call) => {
640 macro_call_to_macro_id(ctx, call).map(|id| Macro { id })
641 }
642 Either::Right(call) => {
643 let call = call.loc(self.db);
644 let krate = call.krate(self.db);
645 let lang_items = hir_def::lang_item::lang_items(self.db, krate);
646 call.trait_.derive_macro(lang_items).map(|id| Macro { id })
647 }
648 }
649 })
650 .collect(),
651 )
652 })
653 }
654
655 pub fn expand_derive_macro(
656 &self,
657 attr: &ast::Meta,
658 ) -> Option<Vec<Option<ExpandResult<SyntaxNode>>>> {
659 let res: Vec<_> = self
660 .derive_macro_calls(attr)?
661 .into_iter()
662 .map(|call| {
663 let file_id = call?.left()?;
664 let ExpandResult { value, err } = self.db.parse_macro_expansion(file_id);
665 let root_node = value.0.syntax_node();
666 self.cache(root_node.clone(), file_id.into());
667 Some(ExpandResult { value: root_node, err: err.clone() })
668 })
669 .collect();
670 Some(res)
671 }
672
673 fn derive_macro_calls(
674 &self,
675 attr: &ast::Meta,
676 ) -> Option<Vec<Option<Either<MacroCallId, BuiltinDeriveImplId>>>> {
677 let adt = attr.parent_attr()?.syntax().parent().and_then(ast::Adt::cast)?;
678 let file_id = self.find_file(adt.syntax()).file_id;
679 let adt = InFile::new(file_id, &adt);
680 let src = InFile::new(file_id, attr.clone());
681 self.with_ctx(|ctx| {
682 let (.., res) = ctx.attr_to_derive_macro_call(adt, src)?;
683 Some(res.to_vec())
684 })
685 }
686
687 pub fn is_derive_annotated(&self, adt: InFile<&ast::Adt>) -> bool {
688 self.with_ctx(|ctx| ctx.file_of_adt_has_derives(adt))
689 }
690
691 pub fn derive_helpers_in_scope(&self, adt: &ast::Adt) -> Option<Vec<(Symbol, Symbol)>> {
692 let sa = self.analyze_no_infer(adt.syntax())?;
693 let id = self.db.ast_id_map(sa.file_id).ast_id(adt);
694 let result = sa
695 .resolver
696 .def_map()
697 .derive_helpers_in_scope(InFile::new(sa.file_id, id))?
698 .iter()
699 .map(|(name, macro_, _)| {
700 let macro_name = Macro::from(*macro_).name(self.db).symbol().clone();
701 (name.symbol().clone(), macro_name)
702 })
703 .collect();
704 Some(result)
705 }
706
707 pub fn derive_helper(&self, attr: &ast::Attr) -> Option<Vec<(Macro, MacroCallId)>> {
708 let adt = attr.syntax().ancestors().find_map(ast::Item::cast).and_then(|it| match it {
709 ast::Item::Struct(it) => Some(ast::Adt::Struct(it)),
710 ast::Item::Enum(it) => Some(ast::Adt::Enum(it)),
711 ast::Item::Union(it) => Some(ast::Adt::Union(it)),
712 _ => None,
713 })?;
714 let attr_name = attr.path().and_then(|it| it.as_single_name_ref())?.as_name();
715 let sa = self.analyze_no_infer(adt.syntax())?;
716 let id = self.db.ast_id_map(sa.file_id).ast_id(&adt);
717 let res: Vec<_> = sa
718 .resolver
719 .def_map()
720 .derive_helpers_in_scope(InFile::new(sa.file_id, id))?
721 .iter()
722 .filter(|&(name, _, _)| *name == attr_name)
723 .filter_map(|&(_, macro_, call)| Some((macro_.into(), call.left()?)))
724 .collect();
725 res.is_empty().not().then_some(res)
727 }
728
729 pub fn is_attr_macro_call(&self, item: InFile<&ast::Item>) -> bool {
730 self.with_ctx(|ctx| ctx.item_to_macro_call(item).is_some())
731 }
732
733 pub fn speculative_expand_macro_call(
736 &self,
737 actual_macro_call: &ast::MacroCall,
738 speculative_args: &ast::TokenTree,
739 token_to_map: SyntaxToken,
740 ) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
741 let macro_file = self.to_def(actual_macro_call)?;
742 hir_expand::db::expand_speculative(
743 self.db,
744 macro_file,
745 speculative_args.syntax(),
746 token_to_map,
747 )
748 }
749
750 pub fn speculative_expand_raw(
751 &self,
752 macro_file: MacroCallId,
753 speculative_args: &SyntaxNode,
754 token_to_map: SyntaxToken,
755 ) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
756 hir_expand::db::expand_speculative(self.db, macro_file, speculative_args, token_to_map)
757 }
758
759 pub fn speculative_expand_attr_macro(
762 &self,
763 actual_macro_call: &ast::Item,
764 speculative_args: &ast::Item,
765 token_to_map: SyntaxToken,
766 ) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
767 let macro_call = self.wrap_node_infile(actual_macro_call.clone());
768 let macro_call_id = self.with_ctx(|ctx| ctx.item_to_macro_call(macro_call.as_ref()))?;
769 hir_expand::db::expand_speculative(
770 self.db,
771 macro_call_id,
772 speculative_args.syntax(),
773 token_to_map,
774 )
775 }
776
777 pub fn speculative_expand_derive_as_pseudo_attr_macro(
778 &self,
779 actual_macro_call: &ast::Attr,
780 speculative_args: &ast::Attr,
781 token_to_map: SyntaxToken,
782 ) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
783 let attr = self.wrap_node_infile(actual_macro_call.clone());
784 let adt = actual_macro_call.syntax().parent().and_then(ast::Adt::cast)?;
785 let macro_call_id = self.with_ctx(|ctx| {
786 ctx.attr_to_derive_macro_call(
787 attr.with_value(&adt),
788 attr.with_value(attr.value.meta()?),
789 )
790 .map(|(_, it, _)| it)
791 })?;
792 hir_expand::db::expand_speculative(
793 self.db,
794 macro_call_id,
795 speculative_args.syntax(),
796 token_to_map,
797 )
798 }
799
800 pub fn rename_conflicts(&self, to_be_renamed: &Local, new_name: &Name) -> Vec<Local> {
803 let (store, root_expr) = to_be_renamed.parent_infer.store_and_root_expr(self.db);
804 let resolver = to_be_renamed.parent.resolver(self.db);
805 let starting_expr = store.binding_owner(to_be_renamed.binding_id).unwrap_or(root_expr);
806 let mut visitor = RenameConflictsVisitor {
807 body: store,
808 conflicts: FxHashSet::default(),
809 db: self.db,
810 new_name: new_name.symbol().clone(),
811 old_name: to_be_renamed.name(self.db).symbol().clone(),
812 owner: to_be_renamed.parent,
813 to_be_renamed: to_be_renamed.binding_id,
814 resolver,
815 };
816 visitor.rename_conflicts(starting_expr);
817 visitor
818 .conflicts
819 .into_iter()
820 .map(|binding_id| Local {
821 parent: to_be_renamed.parent,
822 parent_infer: to_be_renamed.parent_infer,
823 binding_id,
824 })
825 .collect()
826 }
827
828 pub fn as_format_args_parts(
830 &self,
831 string: &ast::String,
832 ) -> Option<Vec<(TextRange, Option<Either<PathResolution, InlineAsmOperand>>)>> {
833 let string_start = string.syntax().text_range().start();
834 let token = self.wrap_token_infile(string.syntax().clone());
835 self.descend_into_macros_breakable(token, |token, _| {
836 (|| {
837 let token = token.value;
838 let string = ast::String::cast(token)?;
839 let literal =
840 string.syntax().parent().filter(|it| it.kind() == SyntaxKind::LITERAL)?;
841 let parent = literal.parent()?;
842 if let Some(format_args) = ast::FormatArgsExpr::cast(parent.clone()) {
843 let source_analyzer = self.analyze_no_infer(format_args.syntax())?;
844 let format_args = self.wrap_node_infile(format_args);
845 let res = source_analyzer
846 .as_format_args_parts(self.db, format_args.as_ref())?
847 .map(|(range, res)| (range + string_start, res.map(Either::Left)))
848 .collect();
849 Some(res)
850 } else {
851 let asm = ast::AsmExpr::cast(parent)?;
852 let source_analyzer = self.analyze_no_infer(asm.syntax())?;
853 let line = asm.template().position(|it| *it.syntax() == literal)?;
854 let asm = self.wrap_node_infile(asm);
855 let (owner, (expr, asm_parts)) = source_analyzer.as_asm_parts(asm.as_ref())?;
856 let res = asm_parts
857 .get(line)?
858 .iter()
859 .map(|&(range, index)| {
860 (
861 range + string_start,
862 Some(Either::Right(InlineAsmOperand { owner, expr, index })),
863 )
864 })
865 .collect();
866 Some(res)
867 }
868 })()
869 .map_or(ControlFlow::Continue(()), ControlFlow::Break)
870 })
871 }
872
873 pub fn check_for_format_args_template(
882 &self,
883 original_token: SyntaxToken,
884 offset: TextSize,
885 ) -> Option<(
886 TextRange,
887 HirFileRange,
888 ast::String,
889 Option<Either<PathResolution, InlineAsmOperand>>,
890 )> {
891 let original_token =
892 self.wrap_token_infile(original_token).map(ast::String::cast).transpose()?;
893 self.check_for_format_args_template_with_file(original_token, offset)
894 }
895
896 pub fn check_for_format_args_template_with_file(
904 &self,
905 original_token: InFile<ast::String>,
906 offset: TextSize,
907 ) -> Option<(
908 TextRange,
909 HirFileRange,
910 ast::String,
911 Option<Either<PathResolution, InlineAsmOperand>>,
912 )> {
913 let relative_offset =
914 offset.checked_sub(original_token.value.syntax().text_range().start())?;
915 self.descend_into_macros_breakable(
916 original_token.as_ref().map(|it| it.syntax().clone()),
917 |token, _| {
918 (|| {
919 let token = token.map(ast::String::cast).transpose()?;
920 self.resolve_offset_in_format_args(token.as_ref(), relative_offset).map(
921 |(range, res)| {
922 (
923 range + original_token.value.syntax().text_range().start(),
924 HirFileRange {
925 file_id: token.file_id,
926 range: range + token.value.syntax().text_range().start(),
927 },
928 token.value,
929 res,
930 )
931 },
932 )
933 })()
934 .map_or(ControlFlow::Continue(()), ControlFlow::Break)
935 },
936 )
937 }
938
939 fn resolve_offset_in_format_args(
940 &self,
941 InFile { value: string, file_id }: InFile<&ast::String>,
942 offset: TextSize,
943 ) -> Option<(TextRange, Option<Either<PathResolution, InlineAsmOperand>>)> {
944 debug_assert!(offset <= string.syntax().text_range().len());
945 let literal = string.syntax().parent().filter(|it| it.kind() == SyntaxKind::LITERAL)?;
946 let parent = literal.parent()?;
947 if let Some(format_args) = ast::FormatArgsExpr::cast(parent.clone()) {
948 let source_analyzer =
949 &self.analyze_impl(InFile::new(file_id, format_args.syntax()), None, false)?;
950 source_analyzer
951 .resolve_offset_in_format_args(self.db, InFile::new(file_id, &format_args), offset)
952 .map(|(range, res)| (range, res.map(Either::Left)))
953 } else {
954 let asm = ast::AsmExpr::cast(parent)?;
955 let source_analyzer =
956 self.analyze_impl(InFile::new(file_id, asm.syntax()), None, false)?;
957 let line = asm.template().position(|it| *it.syntax() == literal)?;
958 source_analyzer
959 .resolve_offset_in_asm_template(InFile::new(file_id, &asm), line, offset)
960 .map(|(owner, (expr, range, index))| {
961 (range, Some(Either::Right(InlineAsmOperand { owner, expr, index })))
962 })
963 }
964 }
965
966 pub fn debug_hir_at(&self, token: SyntaxToken) -> Option<String> {
967 self.analyze_no_infer(&token.parent()?).and_then(|it| {
968 Some(match it.body_or_sig.as_ref()? {
969 crate::source_analyzer::BodyOrSig::Body { def, body, .. } => {
970 hir_def::expr_store::pretty::print_body_hir(
971 self.db,
972 body,
973 *def,
974 it.file_id.edition(self.db),
975 )
976 }
977 &crate::source_analyzer::BodyOrSig::VariantFields { def, .. } => {
978 hir_def::expr_store::pretty::print_variant_body_hir(
979 self.db,
980 def,
981 it.file_id.edition(self.db),
982 )
983 }
984 &crate::source_analyzer::BodyOrSig::Sig { def, .. } => {
985 hir_def::expr_store::pretty::print_signature(
986 self.db,
987 def,
988 it.file_id.edition(self.db),
989 )
990 }
991 })
992 })
993 }
994
995 pub fn descend_token_into_include_expansion(
997 &self,
998 tok: InRealFile<SyntaxToken>,
999 ) -> InFile<SyntaxToken> {
1000 let Some(include) =
1001 self.s2d_cache.borrow_mut().get_or_insert_include_for(self.db, tok.file_id)
1002 else {
1003 return tok.into();
1004 };
1005 let span = self.db.real_span_map(tok.file_id).span_for_range(tok.value.text_range());
1006 let Some(InMacroFile { file_id, value: mut mapped_tokens }) = self.with_ctx(|ctx| {
1007 Some(
1008 ctx.cache
1009 .get_or_insert_expansion(ctx.db, include)
1010 .map_range_down(span)?
1011 .map(SmallVec::<[_; 2]>::from_iter),
1012 )
1013 }) else {
1014 return tok.into();
1015 };
1016 mapped_tokens.pop().map_or_else(|| tok.into(), |(tok, _)| InFile::new(file_id.into(), tok))
1018 }
1019
1020 pub fn descend_node_into_attributes<N: AstNode>(&self, node: N) -> SmallVec<[N; 1]> {
1022 let mut res = smallvec![];
1024 let tokens = (|| {
1025 let first = skip_trivia_token(node.syntax().first_token()?, Direction::Next)?;
1027 let last = skip_trivia_token(node.syntax().last_token()?, Direction::Prev)?;
1028 Some((first, last))
1029 })();
1030 let (first, last) = match tokens {
1031 Some(it) => it,
1032 None => return res,
1033 };
1034 let file = self.find_file(node.syntax());
1035
1036 if first == last {
1037 self.descend_into_macros_all(
1039 InFile::new(file.file_id, first),
1040 false,
1041 &mut |InFile { value, .. }, _ctx| {
1042 if let Some(node) = value
1043 .parent_ancestors()
1044 .take_while(|it| it.text_range() == value.text_range())
1045 .find_map(N::cast)
1046 {
1047 res.push(node)
1048 }
1049 },
1050 );
1051 } else {
1052 let mut scratch: SmallVec<[_; 1]> = smallvec![];
1054 self.descend_into_macros_all(
1055 InFile::new(file.file_id, first),
1056 false,
1057 &mut |token, _ctx| scratch.push(token),
1058 );
1059
1060 let mut scratch = scratch.into_iter();
1061 self.descend_into_macros_all(
1062 InFile::new(file.file_id, last),
1063 false,
1064 &mut |InFile { value: last, file_id: last_fid }, _ctx| {
1065 if let Some(InFile { value: first, file_id: first_fid }) = scratch.next()
1066 && first_fid == last_fid
1067 && let Some(p) = first.parent()
1068 {
1069 let range = first.text_range().cover(last.text_range());
1070 let node = find_root(&p)
1071 .covering_element(range)
1072 .ancestors()
1073 .take_while(|it| it.text_range() == range)
1074 .find_map(N::cast);
1075 if let Some(node) = node {
1076 res.push(node);
1077 }
1078 }
1079 },
1080 );
1081 }
1082 res
1083 }
1084
1085 pub fn is_inside_macro_call(&self, token @ InFile { value, .. }: InFile<&SyntaxToken>) -> bool {
1090 value.parent_ancestors().any(|ancestor| {
1091 if let Some(macro_call) = ast::MacroCall::cast(ancestor.clone())
1092 && macro_call.path().is_none_or(|path| {
1094 !path.syntax().text_range().contains_range(value.text_range())
1095 })
1096 {
1097 return true;
1098 }
1099
1100 let Some(item) = ast::Item::cast(ancestor) else {
1101 return false;
1102 };
1103 self.with_ctx(|ctx| {
1104 if ctx.item_to_macro_call(token.with_value(&item)).is_some() {
1105 return true;
1106 }
1107 let adt = match item {
1108 ast::Item::Struct(it) => it.into(),
1109 ast::Item::Enum(it) => it.into(),
1110 ast::Item::Union(it) => it.into(),
1111 _ => return false,
1112 };
1113 ctx.file_of_adt_has_derives(token.with_value(&adt))
1114 })
1115 })
1116 }
1117
1118 pub fn descend_into_macros_cb(
1119 &self,
1120 token: SyntaxToken,
1121 mut cb: impl FnMut(InFile<SyntaxToken>, SyntaxContext),
1122 ) {
1123 self.descend_into_macros_all(self.wrap_token_infile(token), false, &mut |t, ctx| {
1124 cb(t, ctx)
1125 });
1126 }
1127
1128 pub fn descend_into_macros(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> {
1129 let mut res = smallvec![];
1130 self.descend_into_macros_all(
1131 self.wrap_token_infile(token.clone()),
1132 false,
1133 &mut |t, _ctx| res.push(t.value),
1134 );
1135 if res.is_empty() {
1136 res.push(token);
1137 }
1138 res
1139 }
1140
1141 pub fn descend_into_macros_no_opaque(
1142 &self,
1143 token: SyntaxToken,
1144 always_descend_into_derives: bool,
1145 ) -> SmallVec<[InFile<SyntaxToken>; 1]> {
1146 let mut res = smallvec![];
1147 let token = self.wrap_token_infile(token);
1148 self.descend_into_macros_all(token.clone(), always_descend_into_derives, &mut |t, ctx| {
1149 if !ctx.is_opaque(self.db) {
1150 res.push(t);
1152 }
1153 });
1154 if res.is_empty() {
1155 res.push(token);
1156 }
1157 res
1158 }
1159
1160 pub fn descend_into_macros_breakable<T>(
1161 &self,
1162 token: InFile<SyntaxToken>,
1163 mut cb: impl FnMut(InFile<SyntaxToken>, SyntaxContext) -> ControlFlow<T>,
1164 ) -> Option<T> {
1165 self.descend_into_macros_impl(token, false, &mut cb)
1166 }
1167
1168 pub fn descend_into_macros_exact(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> {
1171 let mut r = smallvec![];
1172 let text = token.text();
1173 let kind = token.kind();
1174
1175 self.descend_into_macros_cb(token.clone(), |InFile { value, file_id: _ }, ctx| {
1176 let mapped_kind = value.kind();
1177 let any_ident_match = || kind.is_any_identifier() && value.kind().is_any_identifier();
1178 let matches = (kind == mapped_kind || any_ident_match())
1179 && text == value.text()
1180 && !ctx.is_opaque(self.db);
1181 if matches {
1182 r.push(value);
1183 }
1184 });
1185 if r.is_empty() {
1186 r.push(token);
1187 }
1188 r
1189 }
1190
1191 pub fn descend_into_macros_exact_with_file(
1194 &self,
1195 token: SyntaxToken,
1196 ) -> SmallVec<[InFile<SyntaxToken>; 1]> {
1197 let mut r = smallvec![];
1198 let text = token.text();
1199 let kind = token.kind();
1200
1201 self.descend_into_macros_cb(token.clone(), |InFile { value, file_id }, ctx| {
1202 let mapped_kind = value.kind();
1203 let any_ident_match = || kind.is_any_identifier() && value.kind().is_any_identifier();
1204 let matches = (kind == mapped_kind || any_ident_match())
1205 && text == value.text()
1206 && !ctx.is_opaque(self.db);
1207 if matches {
1208 r.push(InFile { value, file_id });
1209 }
1210 });
1211 if r.is_empty() {
1212 r.push(self.wrap_token_infile(token));
1213 }
1214 r
1215 }
1216
1217 pub fn descend_into_macros_single_exact(&self, token: SyntaxToken) -> SyntaxToken {
1220 let text = token.text();
1221 let kind = token.kind();
1222 self.descend_into_macros_breakable(
1223 self.wrap_token_infile(token.clone()),
1224 |InFile { value, file_id: _ }, _ctx| {
1225 let mapped_kind = value.kind();
1226 let any_ident_match =
1227 || kind.is_any_identifier() && value.kind().is_any_identifier();
1228 let matches = (kind == mapped_kind || any_ident_match()) && text == value.text();
1229 if matches { ControlFlow::Break(value) } else { ControlFlow::Continue(()) }
1230 },
1231 )
1232 .unwrap_or(token)
1233 }
1234
1235 fn descend_into_macros_all(
1236 &self,
1237 token: InFile<SyntaxToken>,
1238 always_descend_into_derives: bool,
1239 f: &mut dyn FnMut(InFile<SyntaxToken>, SyntaxContext),
1240 ) {
1241 self.descend_into_macros_impl(token, always_descend_into_derives, &mut |tok, ctx| {
1242 f(tok, ctx);
1243 CONTINUE_NO_BREAKS
1244 });
1245 }
1246
1247 fn descend_into_macros_impl<T>(
1248 &self,
1249 InFile { value: token, file_id }: InFile<SyntaxToken>,
1250 always_descend_into_derives: bool,
1251 f: &mut dyn FnMut(InFile<SyntaxToken>, SyntaxContext) -> ControlFlow<T>,
1252 ) -> Option<T> {
1253 let _p = tracing::info_span!("descend_into_macros_impl").entered();
1254
1255 let db = self.db;
1256 let span = db.span_map(file_id).span_for_range(token.text_range());
1257
1258 let process_expansion_for_token =
1260 |ctx: &mut SourceToDefCtx<'_, '_>, stack: &mut Vec<_>, macro_file| {
1261 let InMacroFile { file_id, value: mapped_tokens } = ctx
1262 .cache
1263 .get_or_insert_expansion(ctx.db, macro_file)
1264 .map_range_down(span)?
1265 .map(SmallVec::<[_; 2]>::from_iter);
1266 let res = mapped_tokens.is_empty().not().then_some(());
1268 stack.push((HirFileId::from(file_id), mapped_tokens));
1270 res
1271 };
1272
1273 let mut stack: Vec<(_, SmallVec<[_; 2]>)> = vec![];
1278 let include = file_id
1279 .file_id()
1280 .and_then(|file_id| self.s2d_cache.borrow_mut().get_or_insert_include_for(db, file_id));
1281 match include {
1282 Some(include) => {
1283 self.with_ctx(|ctx| process_expansion_for_token(ctx, &mut stack, include))?;
1285 }
1286 None => {
1287 stack.push((file_id, smallvec![(token, span.ctx)]));
1288 }
1289 }
1290
1291 let mut m_cache = self.macro_call_cache.borrow_mut();
1292
1293 let filter_duplicates = |tokens: &mut SmallVec<_>, range: TextRange| {
1296 tokens.retain(|(t, _): &mut (SyntaxToken, _)| !range.contains_range(t.text_range()))
1297 };
1298
1299 while let Some((expansion, ref mut tokens)) = stack.pop() {
1300 tokens.reverse();
1304 while let Some((token, ctx)) = tokens.pop() {
1305 let was_not_remapped = (|| {
1306 let res = self.with_ctx(|ctx| {
1310 token
1311 .parent_ancestors()
1312 .filter_map(ast::Item::cast)
1313 .find_map(|item| {
1323 item.attrs().next()?;
1325 ctx.item_to_macro_call(InFile::new(expansion, &item))
1326 .zip(Some(item))
1327 })
1328 .map(|(call_id, item)| {
1329 let item_range = item.syntax().text_range();
1330 let loc = call_id.loc(db);
1331 let text_range = match &loc.kind {
1332 hir_expand::MacroCallKind::Attr {
1333 censored_attr_ids: attr_ids,
1334 ..
1335 } => {
1336 let (attr, _) = attr_ids
1350 .invoc_attr()
1351 .find_attr_range_with_source(db, loc.krate, &item);
1352 let start = attr.syntax().text_range().start();
1353 TextRange::new(start, item_range.end())
1354 }
1355 _ => item_range,
1356 };
1357 filter_duplicates(tokens, text_range);
1358 process_expansion_for_token(ctx, &mut stack, call_id)
1359 })
1360 });
1361
1362 if let Some(res) = res {
1363 return res;
1364 }
1365
1366 if always_descend_into_derives {
1367 let res = self.with_ctx(|ctx| {
1368 let (derives, adt) = token
1369 .parent_ancestors()
1370 .filter_map(ast::Adt::cast)
1371 .find_map(|adt| {
1372 Some((
1373 ctx.derive_macro_calls(InFile::new(expansion, &adt))?
1374 .map(|(a, b, c)| (a, b, c.to_owned()))
1375 .collect::<SmallVec<[_; 2]>>(),
1376 adt,
1377 ))
1378 })?;
1379 for (_, derive_attr, derives) in derives {
1380 process_expansion_for_token(ctx, &mut stack, derive_attr);
1384 for derive in derives.into_iter().flatten() {
1385 let Either::Left(derive) = derive else { continue };
1386 process_expansion_for_token(ctx, &mut stack, derive);
1387 }
1388 }
1389 filter_duplicates(tokens, adt.syntax().text_range());
1391 Some(())
1392 });
1393 if let Some(()) = res {
1396 return None;
1401 }
1402 }
1403 let tt = token
1406 .parent_ancestors()
1407 .map_while(Either::<ast::TokenTree, ast::Meta>::cast)
1408 .last()?;
1409
1410 match tt {
1411 Either::Left(tt) => {
1413 let macro_call = tt.syntax().parent().and_then(ast::MacroCall::cast)?;
1414 if tt.left_delimiter_token().map_or(false, |it| it == token) {
1415 return None;
1416 }
1417 if tt.right_delimiter_token().map_or(false, |it| it == token) {
1418 return None;
1419 }
1420 let mcall = InFile::new(expansion, macro_call);
1421 let file_id = match m_cache.get(&mcall) {
1422 Some(&it) => it,
1423 None => {
1424 let it = ast::MacroCall::to_def(self, mcall.as_ref())?;
1425 m_cache.insert(mcall, it);
1426 it
1427 }
1428 };
1429 let text_range = tt.syntax().text_range();
1430 filter_duplicates(tokens, text_range);
1431
1432 self.with_ctx(|ctx| {
1433 process_expansion_for_token(ctx, &mut stack, file_id).or(file_id
1434 .eager_arg(db)
1435 .and_then(|arg| {
1436 process_expansion_for_token(ctx, &mut stack, arg)
1438 }))
1439 })
1440 }
1441 Either::Right(_) if always_descend_into_derives => None,
1442 Either::Right(meta) => {
1444 let attr = meta.parent_attr()?;
1447 let adt = match attr.syntax().parent().and_then(ast::Adt::cast) {
1448 Some(adt) => {
1449 let res = self.with_ctx(|ctx| {
1451 let derive_call = ctx
1454 .attr_to_derive_macro_call(
1455 InFile::new(expansion, &adt),
1456 InFile::new(expansion, meta.clone()),
1457 )?
1458 .1;
1459
1460 let text_range = attr.syntax().text_range();
1462 tokens.retain(|(t, _)| {
1465 !text_range.contains_range(t.text_range())
1466 });
1467 Some(process_expansion_for_token(
1468 ctx,
1469 &mut stack,
1470 derive_call,
1471 ))
1472 });
1473 if let Some(res) = res {
1474 return res;
1475 }
1476 Some(adt)
1477 }
1478 None => {
1479 attr.syntax().ancestors().find_map(ast::Item::cast).and_then(
1481 |it| match it {
1482 ast::Item::Struct(it) => Some(ast::Adt::Struct(it)),
1483 ast::Item::Enum(it) => Some(ast::Adt::Enum(it)),
1484 ast::Item::Union(it) => Some(ast::Adt::Union(it)),
1485 _ => None,
1486 },
1487 )
1488 }
1489 }?;
1490 let attr_name =
1491 attr.path().and_then(|it| it.as_single_name_ref())?.as_name();
1492 let resolver = &token
1495 .parent()
1496 .and_then(|parent| {
1497 self.analyze_impl(InFile::new(expansion, &parent), None, false)
1498 })?
1499 .resolver;
1500 let id = db.ast_id_map(expansion).ast_id(&adt);
1501 let helpers = resolver
1502 .def_map()
1503 .derive_helpers_in_scope(InFile::new(expansion, id))?;
1504
1505 if !helpers.is_empty() {
1506 let text_range = attr.syntax().text_range();
1507 filter_duplicates(tokens, text_range);
1508 }
1509
1510 let mut res = None;
1511 self.with_ctx(|ctx| {
1512 for (.., derive) in
1513 helpers.iter().filter(|(helper, ..)| *helper == attr_name)
1514 {
1515 let Either::Left(derive) = *derive else { continue };
1516 res = res
1520 .or(process_expansion_for_token(ctx, &mut stack, derive));
1521 }
1522 res
1523 })
1524 }
1525 }
1526 })()
1527 .is_none();
1528 if was_not_remapped
1529 && let ControlFlow::Break(b) = f(InFile::new(expansion, token), ctx)
1530 {
1531 return Some(b);
1532 }
1533 }
1534 }
1535 None
1536 }
1537
1538 fn descend_node_at_offset(
1543 &self,
1544 node: &SyntaxNode,
1545 offset: TextSize,
1546 ) -> impl Iterator<Item = impl Iterator<Item = SyntaxNode> + '_> + '_ {
1547 node.token_at_offset(offset)
1548 .map(move |token| self.descend_into_macros_exact(token))
1549 .map(|descendants| {
1550 descendants.into_iter().map(move |it| self.token_ancestors_with_macros(it))
1551 })
1552 .kmerge_by(|left, right| {
1555 left.clone()
1556 .map(|node| node.text_range().len())
1557 .lt(right.clone().map(|node| node.text_range().len()))
1558 })
1559 }
1560
1561 pub fn original_range(&self, node: &SyntaxNode) -> FileRange {
1565 let node = self.find_file(node);
1566 node.original_file_range_rooted(self.db)
1567 }
1568
1569 pub fn original_range_opt(&self, node: &SyntaxNode) -> Option<FileRange> {
1571 let node = self.find_file(node);
1572 node.original_file_range_opt(self.db).filter(|(_, ctx)| ctx.is_root()).map(TupleExt::head)
1573 }
1574
1575 pub fn original_ast_node<N: AstNode>(&self, node: N) -> Option<N> {
1578 self.wrap_node_infile(node).original_ast_node_rooted(self.db).map(
1579 |InRealFile { file_id, value }| {
1580 self.cache(find_root(value.syntax()), file_id.into());
1581 value
1582 },
1583 )
1584 }
1585
1586 pub fn original_syntax_node_rooted(&self, node: &SyntaxNode) -> Option<SyntaxNode> {
1589 let InFile { file_id, .. } = self.find_file(node);
1590 InFile::new(file_id, node).original_syntax_node_rooted(self.db).map(
1591 |InRealFile { file_id, value }| {
1592 self.cache(find_root(&value), file_id.into());
1593 value
1594 },
1595 )
1596 }
1597
1598 pub fn diagnostics_display_range(
1599 &self,
1600 src: InFile<SyntaxNodePtr>,
1601 ) -> FileRangeWrapper<FileId> {
1602 let root = self.parse_or_expand(src.file_id);
1603 let node = src.map(|it| it.to_node(&root));
1604 let FileRange { file_id, range } = node.as_ref().original_file_range_rooted(self.db);
1605 FileRangeWrapper { file_id: file_id.file_id(self.db), range }
1606 }
1607
1608 pub fn diagnostics_display_range_for_range(
1609 &self,
1610 src: InFile<TextRange>,
1611 ) -> FileRangeWrapper<FileId> {
1612 let FileRange { file_id, range } = src.original_node_file_range_rooted(self.db);
1613 FileRangeWrapper { file_id: file_id.file_id(self.db), range }
1614 }
1615
1616 fn token_ancestors_with_macros(
1617 &self,
1618 token: SyntaxToken,
1619 ) -> impl Iterator<Item = SyntaxNode> + Clone + '_ {
1620 token.parent().into_iter().flat_map(move |parent| self.ancestors_with_macros(parent))
1621 }
1622
1623 pub fn ancestors_with_macros(
1626 &self,
1627 node: SyntaxNode,
1628 ) -> impl Iterator<Item = SyntaxNode> + Clone + '_ {
1629 let node = self.find_file(&node);
1630 self.ancestors_with_macros_file(node.cloned()).map(|it| it.value)
1631 }
1632
1633 pub fn ancestors_with_macros_file(
1635 &self,
1636 node: InFile<SyntaxNode>,
1637 ) -> impl Iterator<Item = InFile<SyntaxNode>> + Clone + '_ {
1638 iter::successors(Some(node), move |&InFile { file_id, ref value }| match value.parent() {
1639 Some(parent) => Some(InFile::new(file_id, parent)),
1640 None => {
1641 let macro_file = file_id.macro_file()?;
1642
1643 self.with_ctx(|ctx| {
1644 let expansion_info = ctx.cache.get_or_insert_expansion(ctx.db, macro_file);
1645 expansion_info.arg().map(|node| node?.parent()).transpose()
1646 })
1647 }
1648 })
1649 }
1650
1651 pub fn ancestors_at_offset_with_macros(
1652 &self,
1653 node: &SyntaxNode,
1654 offset: TextSize,
1655 ) -> impl Iterator<Item = SyntaxNode> + '_ {
1656 node.token_at_offset(offset)
1657 .map(|token| self.token_ancestors_with_macros(token))
1658 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len())
1659 }
1660
1661 pub fn fn_return_points(&self, func: Function) -> Vec<InFile<ast::ReturnExpr>> {
1664 let func_id = match func.id {
1665 AnyFunctionId::FunctionId(id) => id,
1666 _ => return vec![],
1667 };
1668 let (body, source_map) = Body::with_source_map(self.db, func_id.into());
1669
1670 fn collect_returns(
1671 sema: &SemanticsImpl<'_>,
1672 body: &Body,
1673 source_map: &hir_def::expr_store::ExpressionStoreSourceMap,
1674 expr_id: ExprId,
1675 acc: &mut Vec<InFile<ast::ReturnExpr>>,
1676 ) {
1677 match &body[expr_id] {
1678 Expr::Closure { .. } | Expr::Const(_) => return,
1679 Expr::Return { .. } => {
1680 if let Ok(source) = source_map.expr_syntax(expr_id)
1681 && let Some(ret_expr) = source.value.cast::<ast::ReturnExpr>()
1682 {
1683 let root = sema.parse_or_expand(source.file_id);
1684 acc.push(InFile::new(source.file_id, ret_expr.to_node(&root)));
1685 }
1686 }
1687 _ => {}
1688 }
1689 body.walk_child_exprs(expr_id, |child| {
1690 collect_returns(sema, body, source_map, child, acc);
1691 });
1692 }
1693
1694 let mut returns = vec![];
1695 collect_returns(self, body, source_map, body.root_expr(), &mut returns);
1696 returns
1697 }
1698
1699 pub fn resolve_lifetime_param(&self, lifetime: &ast::Lifetime) -> Option<LifetimeParam> {
1700 let text = lifetime.text();
1701 let lifetime_param = lifetime.syntax().ancestors().find_map(|syn| {
1702 let gpl = ast::AnyHasGenericParams::cast(syn)?.generic_param_list()?;
1703 gpl.lifetime_params()
1704 .find(|tp| tp.lifetime().as_ref().map(|lt| lt.text()).as_ref() == Some(&text))
1705 })?;
1706 let src = self.wrap_node_infile(lifetime_param);
1707 ToDef::to_def(self, src.as_ref())
1708 }
1709
1710 pub fn resolve_label(&self, label: &ast::Lifetime) -> Option<Label> {
1711 let src = self.wrap_node_infile(label.clone());
1712 let (parent, label_id) = self.with_ctx(|ctx| ctx.label_ref_to_def(src.as_ref()))?;
1713 Some(Label { parent, label_id })
1714 }
1715
1716 pub fn resolve_type(&self, ty: &ast::Type) -> Option<Type<'db>> {
1717 let analyze = self.analyze(ty.syntax())?;
1718 analyze.type_of_type(self.db, ty)
1719 }
1720
1721 pub fn resolve_trait(&self, path: &ast::Path) -> Option<Trait> {
1722 let parent_ty = path.syntax().parent().and_then(ast::Type::cast)?;
1723 let analyze = self.analyze(path.syntax())?;
1724 let ty = analyze.store_sm()?.node_type(InFile::new(analyze.file_id, &parent_ty))?;
1725 let path = match &analyze.store()?.types[ty] {
1726 hir_def::type_ref::TypeRef::Path(path) => path,
1727 _ => return None,
1728 };
1729 match analyze.resolver.resolve_path_in_type_ns_fully(self.db, path)? {
1730 TypeNs::TraitId(trait_id) => Some(trait_id.into()),
1731 _ => None,
1732 }
1733 }
1734
1735 pub fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjustment<'db>>> {
1736 let mutability = |m| match m {
1737 hir_ty::next_solver::Mutability::Not => Mutability::Shared,
1738 hir_ty::next_solver::Mutability::Mut => Mutability::Mut,
1739 };
1740
1741 let analyzer = self.analyze(expr.syntax())?;
1742
1743 let (mut source_ty, _) = analyzer.type_of_expr(self.db, expr)?;
1744
1745 analyzer.expr_adjustments(expr).map(|it| {
1746 it.iter()
1747 .map(|adjust| {
1748 let target = analyzer.ty(adjust.target.as_ref());
1749 let kind = match adjust.kind {
1750 hir_ty::Adjust::NeverToAny => Adjust::NeverToAny,
1751 hir_ty::Adjust::Deref(Some(hir_ty::OverloadedDeref(m))) => {
1752 Adjust::Deref(Some(OverloadedDeref(mutability(m))))
1754 }
1755 hir_ty::Adjust::Deref(None) => Adjust::Deref(None),
1756 hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::RawPtr(m)) => {
1757 Adjust::Borrow(AutoBorrow::RawPtr(mutability(m)))
1758 }
1759 hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::Ref(m)) => {
1760 Adjust::Borrow(AutoBorrow::Ref(mutability(m.into())))
1762 }
1763 hir_ty::Adjust::Pointer(pc) => Adjust::Pointer(pc),
1764 };
1765
1766 let source = mem::replace(&mut source_ty, target.clone());
1768
1769 Adjustment { source, target, kind }
1770 })
1771 .collect()
1772 })
1773 }
1774
1775 pub fn expr_is_diverging(&self, expr: &ast::Expr) -> bool {
1776 (|| self.analyze(expr.syntax())?.expr_is_diverging(self.db, expr))().unwrap_or(false)
1777 }
1778
1779 pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<TypeInfo<'db>> {
1780 self.analyze(expr.syntax())?
1781 .type_of_expr(self.db, expr)
1782 .map(|(ty, coerced)| TypeInfo { original: ty, adjusted: coerced })
1783 }
1784
1785 pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<TypeInfo<'db>> {
1786 self.analyze(pat.syntax())?
1787 .type_of_pat(self.db, pat)
1788 .map(|(ty, coerced)| TypeInfo { original: ty, adjusted: coerced })
1789 }
1790
1791 pub fn type_of_binding_in_pat(&self, pat: &ast::IdentPat) -> Option<Type<'db>> {
1795 self.analyze(pat.syntax())?.type_of_binding_in_pat(self.db, pat)
1796 }
1797
1798 pub fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type<'db>> {
1799 self.analyze(param.syntax())?.type_of_self(self.db, param)
1800 }
1801
1802 pub fn pattern_adjustments(&self, pat: &ast::Pat) -> SmallVec<[Type<'db>; 1]> {
1803 self.analyze(pat.syntax())
1804 .and_then(|it| it.pattern_adjustments(self.db, pat))
1805 .unwrap_or_default()
1806 }
1807
1808 pub fn binding_mode_of_pat(&self, pat: &ast::IdentPat) -> Option<BindingMode> {
1809 self.analyze(pat.syntax())?.binding_mode_of_pat(self.db, pat)
1810 }
1811
1812 pub fn resolve_expr_as_callable(&self, call: &ast::Expr) -> Option<Callable<'db>> {
1813 self.analyze(call.syntax())?.resolve_expr_as_callable(self.db, call)
1814 }
1815
1816 pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
1817 self.analyze(call.syntax())?.resolve_method_call(self.db, call)
1818 }
1819
1820 pub fn resolve_method_call_fallback(
1822 &self,
1823 call: &ast::MethodCallExpr,
1824 ) -> Option<(Either<Function, Field>, Option<GenericSubstitution<'db>>)> {
1825 self.analyze(call.syntax())?.resolve_method_call_fallback(self.db, call)
1826 }
1827
1828 pub fn resolve_trait_impl_method(
1831 &self,
1832 env: Type<'db>,
1833 trait_: Trait,
1834 func: Function,
1835 subst: impl IntoIterator<Item = Type<'db>>,
1836 ) -> Option<Function> {
1837 let AnyFunctionId::FunctionId(func) = func.id else { return Some(func) };
1838 let interner = DbInterner::new_no_crate(self.db);
1839 let mut subst = subst.into_iter();
1840 let substs =
1841 hir_ty::next_solver::GenericArgs::for_item(interner, trait_.id.into(), |_, id, _| {
1842 assert!(matches!(id, hir_def::GenericParamId::TypeParamId(_)), "expected a type");
1843 subst.next().expect("too few subst").ty.skip_binder().into()
1844 });
1845 assert!(subst.next().is_none(), "too many subst");
1846 Some(match self.db.lookup_impl_method(env.param_env(self.db), func, substs).0 {
1847 Either::Left(it) => it.into(),
1848 Either::Right((impl_, method)) => {
1849 Function { id: AnyFunctionId::BuiltinDeriveImplMethod { method, impl_ } }
1850 }
1851 })
1852 }
1853
1854 fn resolve_range_pat(&self, range_pat: &ast::RangePat) -> Option<StructId> {
1855 self.analyze(range_pat.syntax())?.resolve_range_pat(self.db, range_pat)
1856 }
1857
1858 fn resolve_range_expr(&self, range_expr: &ast::RangeExpr) -> Option<StructId> {
1859 self.analyze(range_expr.syntax())?.resolve_range_expr(self.db, range_expr)
1860 }
1861
1862 fn resolve_await_to_poll(&self, await_expr: &ast::AwaitExpr) -> Option<Function> {
1863 self.analyze(await_expr.syntax())?.resolve_await_to_poll(self.db, await_expr)
1864 }
1865
1866 fn resolve_prefix_expr(&self, prefix_expr: &ast::PrefixExpr) -> Option<Function> {
1867 self.analyze(prefix_expr.syntax())?.resolve_prefix_expr(self.db, prefix_expr)
1868 }
1869
1870 fn resolve_index_expr(&self, index_expr: &ast::IndexExpr) -> Option<Function> {
1871 self.analyze(index_expr.syntax())?.resolve_index_expr(self.db, index_expr)
1872 }
1873
1874 fn resolve_bin_expr(&self, bin_expr: &ast::BinExpr) -> Option<Function> {
1875 self.analyze(bin_expr.syntax())?.resolve_bin_expr(self.db, bin_expr)
1876 }
1877
1878 fn resolve_try_expr(&self, try_expr: &ast::TryExpr) -> Option<Function> {
1879 self.analyze(try_expr.syntax())?.resolve_try_expr(self.db, try_expr)
1880 }
1881
1882 pub fn try_expr_returned_type(&self, try_expr: &ast::TryExpr) -> Option<Type<'db>> {
1884 self.ancestors_with_macros(try_expr.syntax().clone()).find_map(|parent| {
1885 if let Some(try_block) = ast::BlockExpr::cast(parent.clone())
1886 && try_block.try_block_modifier().is_some()
1887 {
1888 Some(self.type_of_expr(&try_block.into())?.original)
1889 } else if let Some(closure) = ast::ClosureExpr::cast(parent.clone()) {
1890 Some(
1891 self.type_of_expr(&closure.into())?
1892 .original
1893 .as_callable(self.db)?
1894 .return_type(),
1895 )
1896 } else if let Some(function) = ast::Fn::cast(parent) {
1897 Some(self.to_def(&function)?.ret_type(self.db))
1898 } else {
1899 None
1900 }
1901 })
1902 }
1903
1904 pub fn resolve_method_call_as_callable(
1907 &self,
1908 call: &ast::MethodCallExpr,
1909 ) -> Option<Callable<'db>> {
1910 self.analyze(call.syntax())?.resolve_method_call_as_callable(self.db, call)
1911 }
1912
1913 pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Either<Field, TupleField>> {
1914 self.analyze(field.syntax())?.resolve_field(field)
1915 }
1916
1917 pub fn resolve_field_fallback(
1918 &self,
1919 field: &ast::FieldExpr,
1920 ) -> Option<(Either<Either<Field, TupleField>, Function>, Option<GenericSubstitution<'db>>)>
1921 {
1922 self.analyze(field.syntax())?.resolve_field_fallback(self.db, field)
1923 }
1924
1925 pub fn resolve_record_field(
1926 &self,
1927 field: &ast::RecordExprField,
1928 ) -> Option<(Field, Option<Local>, Type<'db>)> {
1929 self.resolve_record_field_with_substitution(field)
1930 .map(|(field, local, ty, _)| (field, local, ty))
1931 }
1932
1933 pub fn resolve_record_field_with_substitution(
1934 &self,
1935 field: &ast::RecordExprField,
1936 ) -> Option<(Field, Option<Local>, Type<'db>, GenericSubstitution<'db>)> {
1937 self.analyze(field.syntax())?.resolve_record_field(self.db, field)
1938 }
1939
1940 pub fn resolve_record_pat_field(
1941 &self,
1942 field: &ast::RecordPatField,
1943 ) -> Option<(Field, Type<'db>)> {
1944 self.resolve_record_pat_field_with_subst(field).map(|(field, ty, _)| (field, ty))
1945 }
1946
1947 pub fn resolve_record_pat_field_with_subst(
1948 &self,
1949 field: &ast::RecordPatField,
1950 ) -> Option<(Field, Type<'db>, GenericSubstitution<'db>)> {
1951 self.analyze(field.syntax())?.resolve_record_pat_field(self.db, field)
1952 }
1953
1954 pub fn resolve_tuple_struct_pat_fields(
1956 &self,
1957 tuple_struct_pat: &ast::TupleStructPat,
1958 ) -> Option<Vec<(Field, Type<'db>)>> {
1959 self.analyze(tuple_struct_pat.syntax())?
1960 .resolve_tuple_struct_pat_fields(self.db, tuple_struct_pat)
1961 }
1962
1963 pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<Macro> {
1965 let macro_call = self.find_file(macro_call.syntax()).with_value(macro_call);
1966 self.resolve_macro_call2(macro_call)
1967 }
1968
1969 pub fn resolve_macro_call2(&self, macro_call: InFile<&ast::MacroCall>) -> Option<Macro> {
1970 self.to_def2(macro_call)
1971 .and_then(|call| self.with_ctx(|ctx| macro_call_to_macro_id(ctx, call)))
1972 .map(Into::into)
1973 }
1974
1975 pub fn is_proc_macro_call(&self, macro_call: InFile<&ast::MacroCall>) -> bool {
1976 self.resolve_macro_call2(macro_call)
1977 .is_some_and(|m| matches!(m.id, MacroId::ProcMacroId(..)))
1978 }
1979
1980 pub fn resolve_macro_call_arm(&self, macro_call: &ast::MacroCall) -> Option<u32> {
1981 let file_id = self.to_def(macro_call)?;
1982 self.db.parse_macro_expansion(file_id).value.1.matched_arm
1983 }
1984
1985 pub fn get_unsafe_ops(&self, def: ExpressionStoreOwner) -> FxHashSet<ExprOrPatSource> {
1986 let Ok(def) = ExpressionStoreOwnerId::try_from(def) else { return Default::default() };
1987 let (body, source_map) = ExpressionStore::with_source_map(self.db, def);
1988 let mut res = FxHashSet::default();
1989 self.with_all_infers_for_store(def, &mut |infer| {
1990 for root in body.expr_roots() {
1991 unsafe_operations(self.db, infer, def, body, root, &mut |node, _| {
1992 if let Ok(node) = source_map.expr_or_pat_syntax(node) {
1993 res.insert(node);
1994 }
1995 });
1996 }
1997 });
1998 res
1999 }
2000
2001 pub fn get_unsafe_ops_for_unsafe_block(&self, block: ast::BlockExpr) -> Vec<ExprOrPatSource> {
2002 always!(block.unsafe_token().is_some());
2003 let Some(sa) = self.analyze(block.syntax()) else { return vec![] };
2004 let Some((def, store, sm, Some(infer))) = sa.def() else { return vec![] };
2005 let block = self.wrap_node_infile(ast::Expr::from(block));
2006 let Some(ExprOrPatId::ExprId(block)) = sm.node_expr(block.as_ref()) else {
2007 return Vec::new();
2008 };
2009 let mut res = Vec::default();
2010 unsafe_operations(self.db, infer, def, store, block, &mut |node, _| {
2011 if let Ok(node) = sm.expr_or_pat_syntax(node) {
2012 res.push(node);
2013 }
2014 });
2015 res
2016 }
2017
2018 pub fn is_unsafe_macro_call(&self, macro_call: &ast::MacroCall) -> bool {
2019 let Some(mac) = self.resolve_macro_call(macro_call) else { return false };
2020 if mac.is_asm_like(self.db) {
2021 return true;
2022 }
2023
2024 let Some(sa) = self.analyze(macro_call.syntax()) else { return false };
2025 let macro_call = self.find_file(macro_call.syntax()).with_value(macro_call);
2026 match macro_call.map(|it| it.syntax().parent().and_then(ast::MacroExpr::cast)).transpose() {
2027 Some(it) => sa.is_unsafe_macro_call_expr(self.db, it.as_ref()),
2028 None => false,
2029 }
2030 }
2031
2032 pub fn resolve_attr_macro_call(&self, item: &ast::Item) -> Option<Macro> {
2033 let item_in_file = self.wrap_node_infile(item.clone());
2034 let id = self.with_ctx(|ctx| {
2035 let macro_call_id = ctx.item_to_macro_call(item_in_file.as_ref())?;
2036 macro_call_to_macro_id(ctx, macro_call_id)
2037 })?;
2038 Some(Macro { id })
2039 }
2040
2041 pub fn resolve_path(&self, path: &ast::Path) -> Option<PathResolution> {
2042 self.resolve_path_with_subst(path).map(|(it, _)| it)
2043 }
2044
2045 pub fn resolve_path_per_ns(&self, path: &ast::Path) -> Option<PathResolutionPerNs> {
2046 self.analyze(path.syntax())?.resolve_hir_path_per_ns(self.db, path)
2047 }
2048
2049 pub fn resolve_path_with_subst(
2050 &self,
2051 path: &ast::Path,
2052 ) -> Option<(PathResolution, Option<GenericSubstitution<'db>>)> {
2053 self.analyze(path.syntax())?.resolve_path(self.db, path)
2054 }
2055
2056 pub fn resolve_use_type_arg(&self, name: &ast::NameRef) -> Option<TypeParam> {
2057 self.analyze(name.syntax())?.resolve_use_type_arg(name)
2058 }
2059
2060 pub fn resolve_offset_of_field(
2061 &self,
2062 name_ref: &ast::NameRef,
2063 ) -> Option<(Either<EnumVariant, Field>, GenericSubstitution<'db>)> {
2064 self.analyze_no_infer(name_ref.syntax())?.resolve_offset_of_field(self.db, name_ref)
2065 }
2066
2067 pub fn resolve_mod_path(
2068 &self,
2069 scope: &SyntaxNode,
2070 path: &ModPath,
2071 ) -> Option<impl Iterator<Item = ItemInNs>> {
2072 let analyze = self.analyze(scope)?;
2073 let items = analyze.resolver.resolve_module_path_in_items(self.db, path);
2074 Some(items.iter_items().map(|(item, _)| item.into()))
2075 }
2076
2077 fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantId> {
2078 self.analyze(record_lit.syntax())?.resolve_variant(record_lit)
2079 }
2080
2081 pub fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> {
2082 self.analyze(pat.syntax())?.resolve_bind_pat_to_const(self.db, pat)
2083 }
2084
2085 pub fn record_literal_missing_fields(
2086 &self,
2087 literal: &ast::RecordExpr,
2088 ) -> Vec<(Field, Type<'db>)> {
2089 self.analyze(literal.syntax())
2090 .and_then(|it| it.record_literal_missing_fields(self.db, literal))
2091 .unwrap_or_default()
2092 }
2093
2094 pub fn record_literal_matched_fields(
2095 &self,
2096 literal: &ast::RecordExpr,
2097 ) -> Vec<(Field, Type<'db>)> {
2098 self.analyze(literal.syntax())
2099 .and_then(|it| it.record_literal_matched_fields(self.db, literal))
2100 .unwrap_or_default()
2101 }
2102
2103 pub fn record_pattern_missing_fields(
2104 &self,
2105 pattern: &ast::RecordPat,
2106 ) -> Vec<(Field, Type<'db>)> {
2107 self.analyze(pattern.syntax())
2108 .and_then(|it| it.record_pattern_missing_fields(self.db, pattern))
2109 .unwrap_or_default()
2110 }
2111
2112 pub fn record_pattern_matched_fields(
2113 &self,
2114 pattern: &ast::RecordPat,
2115 ) -> Vec<(Field, Type<'db>)> {
2116 self.analyze(pattern.syntax())
2117 .and_then(|it| it.record_pattern_matched_fields(self.db, pattern))
2118 .unwrap_or_default()
2119 }
2120
2121 fn with_ctx<F: FnOnce(&mut SourceToDefCtx<'_, '_>) -> T, T>(&self, f: F) -> T {
2122 let mut ctx = SourceToDefCtx { db: self.db, cache: &mut self.s2d_cache.borrow_mut() };
2123 f(&mut ctx)
2124 }
2125
2126 pub fn to_def<T: ToDef>(&self, src: &T) -> Option<T::Def> {
2127 let src = self.find_file(src.syntax()).with_value(src);
2128 T::to_def(self, src)
2129 }
2130
2131 pub fn to_def2<T: ToDef>(&self, src: InFile<&T>) -> Option<T::Def> {
2132 T::to_def(self, src)
2133 }
2134
2135 fn file_to_module_defs(&self, file: FileId) -> impl Iterator<Item = Module> {
2136 self.with_ctx(|ctx| ctx.file_to_def(file).to_owned()).into_iter().map(Module::from)
2137 }
2138
2139 fn hir_file_to_module_defs(&self, file: HirFileId) -> impl Iterator<Item = Module> {
2140 self.file_to_module_defs(file.original_file_respecting_includes(self.db).file_id(self.db))
2142 }
2143
2144 pub fn scope(&self, node: &SyntaxNode) -> Option<SemanticsScope<'db>> {
2145 self.analyze_no_infer(node).map(
2146 |SourceAnalyzer { file_id, resolver, infer_body, .. }| SemanticsScope {
2147 db: self.db,
2148 file_id,
2149 resolver,
2150 infer_body,
2151 },
2152 )
2153 }
2154
2155 pub fn scope_at_offset(
2156 &self,
2157 node: &SyntaxNode,
2158 offset: TextSize,
2159 ) -> Option<SemanticsScope<'db>> {
2160 self.analyze_with_offset_no_infer(node, offset).map(
2161 |SourceAnalyzer { file_id, resolver, infer_body, .. }| SemanticsScope {
2162 db: self.db,
2163 file_id,
2164 resolver,
2165 infer_body,
2166 },
2167 )
2168 }
2169
2170 pub fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>> {
2172 let res = def.source(self.db)?;
2174 self.cache(find_root(res.value.syntax()), res.file_id);
2175 Some(res)
2176 }
2177
2178 pub fn source_with_range<Def: HasSource>(
2179 &self,
2180 def: Def,
2181 ) -> Option<InFile<(TextRange, Option<Def::Ast>)>> {
2182 let res = def.source_with_range(self.db)?;
2183 self.parse_or_expand(res.file_id);
2184 Some(res)
2185 }
2186
2187 pub fn store_owner_for(&self, node: InFile<&SyntaxNode>) -> Option<ExpressionStoreOwner> {
2188 let container = self.with_ctx(|ctx| ctx.find_container(node))?;
2189 container.as_expression_store_owner().map(|id| id.into())
2190 }
2191
2192 fn populate_anon_const_cache_for<'a>(
2193 &self,
2194 cache: &'a mut DefAnonConstsMap,
2195 def: DefWithoutBodyWithAnonConsts,
2196 ) -> &'a ExprToAnonConst {
2197 cache.entry(def).or_insert_with(|| match def {
2198 Either::Left(def) => {
2199 let all_anon_consts =
2200 AnonConstId::all_from_signature(self.db, def).into_iter().flatten().copied();
2201 all_anon_consts
2202 .map(|anon_const| (anon_const.loc(self.db).expr, anon_const))
2203 .collect()
2204 }
2205 Either::Right(def) => {
2206 let all_anon_consts =
2207 self.db.field_types_with_diagnostics(def).defined_anon_consts().iter().copied();
2208 all_anon_consts
2209 .map(|anon_const| (anon_const.loc(self.db).expr, anon_const))
2210 .collect()
2211 }
2212 })
2213 }
2214
2215 fn find_anon_const_for_root_expr_in_signature(
2216 &self,
2217 def: DefWithoutBodyWithAnonConsts,
2218 root_expr: ExprId,
2219 ) -> Option<AnonConstId> {
2220 let mut cache = self.signature_anon_consts_cache.borrow_mut();
2221 let anon_consts_map = self.populate_anon_const_cache_for(&mut cache, def);
2222 anon_consts_map.get(&root_expr).copied()
2223 }
2224
2225 pub(crate) fn infer_body_for_expr_or_pat(
2226 &self,
2227 def: ExpressionStoreOwnerId,
2228 store: &ExpressionStore,
2229 node: ExprOrPatId,
2230 ) -> Option<InferBodyId> {
2231 let handle_def_without_body = |def| {
2232 let root_expr = match node {
2233 ExprOrPatId::ExprId(expr) => store.find_root_for_expr(expr),
2234 ExprOrPatId::PatId(pat) => store.find_root_for_pat(pat),
2235 };
2236 let anon_const = self.find_anon_const_for_root_expr_in_signature(def, root_expr)?;
2237 Some(anon_const.into())
2238 };
2239 match def {
2240 ExpressionStoreOwnerId::Signature(def) => handle_def_without_body(Either::Left(def)),
2241 ExpressionStoreOwnerId::Body(def) => Some(def.into()),
2242 ExpressionStoreOwnerId::VariantFields(def) => {
2243 handle_def_without_body(Either::Right(def))
2244 }
2245 }
2246 }
2247
2248 fn with_all_infers_for_store(
2249 &self,
2250 owner: ExpressionStoreOwnerId,
2251 callback: &mut dyn FnMut(&'db InferenceResult),
2252 ) {
2253 let mut handle_def_without_body = |def| {
2254 let mut cache = self.signature_anon_consts_cache.borrow_mut();
2255 let map = self.populate_anon_const_cache_for(&mut cache, def);
2256 for &anon_const in map.values() {
2257 callback(InferenceResult::of(self.db, anon_const));
2258 }
2259 };
2260 match owner {
2261 ExpressionStoreOwnerId::Signature(def) => handle_def_without_body(Either::Left(def)),
2262 ExpressionStoreOwnerId::Body(def) => {
2263 callback(InferenceResult::of(self.db, def));
2264 }
2265 ExpressionStoreOwnerId::VariantFields(def) => {
2266 handle_def_without_body(Either::Right(def))
2267 }
2268 }
2269 }
2270
2271 fn analyze(&self, node: &SyntaxNode) -> Option<SourceAnalyzer<'db>> {
2273 let node = self.find_file(node);
2274 self.analyze_impl(node, None, true)
2275 }
2276
2277 fn analyze_no_infer(&self, node: &SyntaxNode) -> Option<SourceAnalyzer<'db>> {
2279 let node = self.find_file(node);
2280 self.analyze_impl(node, None, false)
2281 }
2282
2283 fn analyze_with_offset_no_infer(
2284 &self,
2285 node: &SyntaxNode,
2286 offset: TextSize,
2287 ) -> Option<SourceAnalyzer<'db>> {
2288 let node = self.find_file(node);
2289 self.analyze_impl(node, Some(offset), false)
2290 }
2291
2292 fn analyze_impl(
2293 &self,
2294 node: InFile<&SyntaxNode>,
2295 offset: Option<TextSize>,
2296 infer: bool,
2298 ) -> Option<SourceAnalyzer<'db>> {
2299 let _p = tracing::info_span!("SemanticsImpl::analyze_impl").entered();
2300
2301 let container = self.with_ctx(|ctx| ctx.find_container(node))?;
2302
2303 let resolver = match container {
2304 ChildContainer::DefWithBodyId(def) => {
2305 return Some(if infer {
2306 SourceAnalyzer::new_for_body(self.db, def, node, offset)
2307 } else {
2308 SourceAnalyzer::new_for_body_no_infer(self.db, def, node, offset)
2309 });
2310 }
2311 ChildContainer::VariantId(def) => {
2312 return Some(SourceAnalyzer::new_variant_body(
2313 self.db, self, def, node, offset, infer,
2314 ));
2315 }
2316 ChildContainer::TraitId(it) => {
2317 return Some(if infer {
2318 SourceAnalyzer::new_generic_def(self.db, self, it.into(), node, offset)
2319 } else {
2320 SourceAnalyzer::new_generic_def_no_infer(self.db, self, it.into(), node, offset)
2321 });
2322 }
2323 ChildContainer::ImplId(it) => {
2324 return Some(if infer {
2325 SourceAnalyzer::new_generic_def(self.db, self, it.into(), node, offset)
2326 } else {
2327 SourceAnalyzer::new_generic_def_no_infer(self.db, self, it.into(), node, offset)
2328 });
2329 }
2330 ChildContainer::EnumId(it) => {
2331 return Some(if infer {
2332 SourceAnalyzer::new_generic_def(self.db, self, it.into(), node, offset)
2333 } else {
2334 SourceAnalyzer::new_generic_def_no_infer(self.db, self, it.into(), node, offset)
2335 });
2336 }
2337 ChildContainer::GenericDefId(it) => {
2338 return Some(if infer {
2339 SourceAnalyzer::new_generic_def(self.db, self, it, node, offset)
2340 } else {
2341 SourceAnalyzer::new_generic_def_no_infer(self.db, self, it, node, offset)
2342 });
2343 }
2344 ChildContainer::ModuleId(it) => it.resolver(self.db),
2345 };
2346 Some(SourceAnalyzer::new_for_resolver(resolver, node))
2347 }
2348
2349 fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) {
2350 SourceToDefCache::cache(
2351 &mut self.s2d_cache.borrow_mut().root_to_file_cache,
2352 root_node,
2353 file_id,
2354 );
2355 }
2356
2357 pub fn assert_contains_node(&self, node: &SyntaxNode) {
2358 self.find_file(node);
2359 }
2360
2361 fn lookup(&self, root_node: &SyntaxNode) -> Option<HirFileId> {
2362 let cache = self.s2d_cache.borrow();
2363 cache.root_to_file_cache.get(root_node).copied()
2364 }
2365
2366 fn wrap_node_infile<N: AstNode>(&self, node: N) -> InFile<N> {
2367 let InFile { file_id, .. } = self.find_file(node.syntax());
2368 InFile::new(file_id, node)
2369 }
2370
2371 fn wrap_token_infile(&self, token: SyntaxToken) -> InFile<SyntaxToken> {
2372 let InFile { file_id, .. } = self.find_file(&token.parent().unwrap());
2373 InFile::new(file_id, token)
2374 }
2375
2376 fn find_file<'node>(&self, node: &'node SyntaxNode) -> InFile<&'node SyntaxNode> {
2378 let root_node = find_root(node);
2379 let file_id = self.lookup(&root_node).unwrap_or_else(|| {
2380 panic!(
2381 "\n\nFailed to lookup {:?} in this Semantics.\n\
2382 Make sure to only query nodes derived from this instance of Semantics.\n\
2383 root node: {:?}\n\
2384 known nodes: {}\n\n",
2385 node,
2386 root_node,
2387 self.s2d_cache
2388 .borrow()
2389 .root_to_file_cache
2390 .keys()
2391 .map(|it| format!("{it:?}"))
2392 .collect::<Vec<_>>()
2393 .join(", ")
2394 )
2395 });
2396 InFile::new(file_id, node)
2397 }
2398
2399 pub fn is_inside_unsafe(&self, expr: &ast::Expr) -> bool {
2401 let Some(enclosing_item) =
2402 expr.syntax().ancestors().find_map(Either::<ast::Item, ast::Variant>::cast)
2403 else {
2404 return false;
2405 };
2406
2407 let def = match &enclosing_item {
2408 Either::Left(ast::Item::Fn(it)) if it.unsafe_token().is_some() => return true,
2409 Either::Left(ast::Item::Fn(it)) => (|| match self.to_def(it)?.id {
2410 AnyFunctionId::FunctionId(id) => Some(DefWithBodyId::FunctionId(id)),
2411 AnyFunctionId::BuiltinDeriveImplMethod { .. } => None,
2412 })(),
2413 Either::Left(ast::Item::Const(it)) => {
2414 self.to_def(it).map(<_>::into).map(DefWithBodyId::ConstId)
2415 }
2416 Either::Left(ast::Item::Static(it)) => {
2417 self.to_def(it).map(<_>::into).map(DefWithBodyId::StaticId)
2418 }
2419 Either::Left(_) => None,
2420 Either::Right(it) => self.to_def(it).map(<_>::into).map(DefWithBodyId::VariantId),
2421 };
2422 let Some(def) = def else { return false };
2423 let enclosing_node = enclosing_item.as_ref().either(|i| i.syntax(), |v| v.syntax());
2424
2425 let (body, source_map) = Body::with_source_map(self.db, def);
2426
2427 let file_id = self.find_file(expr.syntax()).file_id;
2428
2429 let Some(mut parent) = expr.syntax().parent() else { return false };
2430 loop {
2431 if &parent == enclosing_node {
2432 break false;
2433 }
2434
2435 if let Some(parent) = ast::Expr::cast(parent.clone())
2436 && let Some(ExprOrPatId::ExprId(expr_id)) =
2437 source_map.node_expr(InFile { file_id, value: &parent })
2438 && let Expr::Unsafe { .. } = body[expr_id]
2439 {
2440 break true;
2441 }
2442
2443 let Some(parent_) = parent.parent() else { break false };
2444 parent = parent_;
2445 }
2446 }
2447
2448 pub fn impl_generated_from_derive(&self, impl_: Impl) -> Option<Adt> {
2449 let id = match impl_.id {
2450 AnyImplId::ImplId(id) => id,
2451 AnyImplId::BuiltinDeriveImplId(id) => return Some(id.loc(self.db).adt.into()),
2452 };
2453 let source = hir_def::src::HasSource::ast_ptr(id.loc(self.db), self.db);
2454 let mut file_id = source.file_id;
2455 let adt_ast_id = loop {
2456 let macro_call = file_id.macro_file()?;
2457 match macro_call.loc(self.db).kind {
2458 hir_expand::MacroCallKind::Derive { ast_id, .. } => break ast_id,
2459 hir_expand::MacroCallKind::FnLike { ast_id, .. } => file_id = ast_id.file_id,
2460 hir_expand::MacroCallKind::Attr { ast_id, .. } => file_id = ast_id.file_id,
2461 }
2462 };
2463 let adt_source = adt_ast_id.to_in_file_node(self.db);
2464 self.cache(adt_source.value.syntax().ancestors().last().unwrap(), adt_source.file_id);
2465 ToDef::to_def(self, adt_source.as_ref())
2466 }
2467
2468 pub fn locals_used(
2469 &self,
2470 element: Either<&ast::Expr, &ast::StmtList>,
2471 text_range: TextRange,
2472 ) -> Option<FxIndexSet<Local>> {
2473 let sa = self.analyze(element.either(|e| e.syntax(), |s| s.syntax()))?;
2474 let infer_body = sa.infer_body?;
2475 let store = sa.store()?;
2476 let mut resolver = sa.resolver.clone();
2477 let def = resolver.expression_store_owner()?;
2478
2479 let is_not_generated = |path: &Path| {
2480 !path.mod_path().and_then(|path| path.as_ident()).is_some_and(Name::is_generated)
2481 };
2482
2483 let exprs = element.either(
2484 |e| vec![e.clone()],
2485 |stmts| {
2486 let mut exprs: Vec<_> = stmts
2487 .statements()
2488 .filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
2489 .filter_map(|stmt| match stmt {
2490 ast::Stmt::ExprStmt(expr_stmt) => expr_stmt.expr().map(|e| vec![e]),
2491 ast::Stmt::Item(_) => None,
2492 ast::Stmt::LetStmt(let_stmt) => {
2493 let init = let_stmt.initializer();
2494 let let_else = let_stmt
2495 .let_else()
2496 .and_then(|le| le.block_expr())
2497 .map(ast::Expr::BlockExpr);
2498
2499 match (init, let_else) {
2500 (Some(i), Some(le)) => Some(vec![i, le]),
2501 (Some(i), _) => Some(vec![i]),
2502 (_, Some(le)) => Some(vec![le]),
2503 _ => None,
2504 }
2505 }
2506 })
2507 .flatten()
2508 .collect();
2509
2510 if let Some(tail_expr) = stmts.tail_expr()
2511 && text_range.contains_range(tail_expr.syntax().text_range())
2512 {
2513 exprs.push(tail_expr);
2514 }
2515 exprs
2516 },
2517 );
2518 let mut exprs: Vec<_> =
2519 exprs.into_iter().filter_map(|e| sa.expr_id(e).and_then(|e| e.as_expr())).collect();
2520
2521 let mut locals: FxIndexSet<Local> = FxIndexSet::default();
2522 let mut add_to_locals_used = |id, parent_expr| {
2523 let path = match id {
2524 ExprOrPatId::ExprId(expr_id) => {
2525 if let Expr::Path(path) = &store[expr_id] {
2526 Some(path)
2527 } else {
2528 None
2529 }
2530 }
2531 ExprOrPatId::PatId(_) => None,
2532 };
2533
2534 if let Some(path) = path
2535 && is_not_generated(path)
2536 {
2537 let _ = resolver.update_to_inner_scope(self.db, def, parent_expr);
2538 let hygiene = store.expr_or_pat_path_hygiene(id);
2539 resolver.resolve_path_in_value_ns_fully(self.db, path, hygiene).inspect(|value| {
2540 if let ValueNs::LocalBinding(id) = value {
2541 locals.insert(Local {
2542 parent: def,
2543 parent_infer: infer_body,
2544 binding_id: *id,
2545 });
2546 }
2547 });
2548 }
2549 };
2550
2551 while let Some(expr_id) = exprs.pop() {
2552 if let Expr::Assignment { target, .. } = store[expr_id] {
2553 store.walk_pats(target, &mut |id| {
2554 add_to_locals_used(ExprOrPatId::PatId(id), expr_id)
2555 });
2556 };
2557 store.walk_child_exprs(expr_id, |id| {
2558 exprs.push(id);
2559 });
2560
2561 add_to_locals_used(ExprOrPatId::ExprId(expr_id), expr_id)
2562 }
2563
2564 Some(locals)
2565 }
2566
2567 pub fn get_failed_obligations(&self, token: SyntaxToken) -> Option<String> {
2568 let node = token.parent()?;
2569 let node = self.find_file(&node);
2570
2571 let container = self.with_ctx(|ctx| ctx.find_container(node))?;
2572
2573 match container {
2574 ChildContainer::DefWithBodyId(def) => {
2575 thread_local! {
2576 static RESULT: RefCell<Vec<ProofTreeData>> = const { RefCell::new(Vec::new()) };
2577 }
2578 infer_query_with_inspect(
2579 self.db,
2580 def,
2581 Some(|infer_ctxt, _obligation, result, proof_tree| {
2582 if result.is_err()
2583 && let Some(tree) = proof_tree
2584 {
2585 let data =
2586 dump_proof_tree_structured(tree, hir_ty::Span::Dummy, infer_ctxt);
2587 RESULT.with(|ctx| ctx.borrow_mut().push(data));
2588 }
2589 }),
2590 );
2591 let data: Vec<ProofTreeData> =
2592 RESULT.with(|data| data.borrow_mut().drain(..).collect());
2593 let data = serde_json::to_string_pretty(&data).unwrap_or_else(|_| "[]".to_owned());
2594 Some(data)
2595 }
2596 _ => None,
2597 }
2598 }
2599}
2600
2601fn macro_call_to_macro_id(
2603 ctx: &mut SourceToDefCtx<'_, '_>,
2604 macro_call_id: MacroCallId,
2605) -> Option<MacroId> {
2606 let db: &dyn ExpandDatabase = ctx.db;
2607 let loc = macro_call_id.loc(db);
2608
2609 match loc.def.ast_id() {
2610 Either::Left(it) => {
2611 let node = match it.file_id {
2612 HirFileId::FileId(file_id) => {
2613 it.to_ptr(db).to_node(&file_id.parse(db).syntax_node())
2614 }
2615 HirFileId::MacroFile(macro_file) => {
2616 let expansion_info = ctx.cache.get_or_insert_expansion(ctx.db, macro_file);
2617 it.to_ptr(db).to_node(&expansion_info.expanded().value)
2618 }
2619 };
2620 ctx.macro_to_def(InFile::new(it.file_id, &node))
2621 }
2622 Either::Right(it) => {
2623 let node = match it.file_id {
2624 HirFileId::FileId(file_id) => {
2625 it.to_ptr(db).to_node(&file_id.parse(db).syntax_node())
2626 }
2627 HirFileId::MacroFile(macro_file) => {
2628 let expansion_info = ctx.cache.get_or_insert_expansion(ctx.db, macro_file);
2629 it.to_ptr(db).to_node(&expansion_info.expanded().value)
2630 }
2631 };
2632 ctx.proc_macro_to_def(InFile::new(it.file_id, &node))
2633 }
2634 }
2635}
2636
2637pub trait ToDef: AstNode + Clone {
2638 type Def;
2639 fn to_def(sema: &SemanticsImpl<'_>, src: InFile<&Self>) -> Option<Self::Def>;
2640}
2641
2642macro_rules! to_def_impls {
2643 ($(($def:path, $ast:path, $meth:ident)),* ,) => {$(
2644 impl ToDef for $ast {
2645 type Def = $def;
2646 fn to_def(sema: &SemanticsImpl<'_>, src: InFile<&Self>) -> Option<Self::Def> {
2647 sema.with_ctx(|ctx| ctx.$meth(src)).map(<$def>::from)
2648 }
2649 }
2650 )*}
2651}
2652
2653to_def_impls![
2654 (crate::Module, ast::Module, module_to_def),
2655 (crate::Module, ast::SourceFile, source_file_to_def),
2656 (crate::Struct, ast::Struct, struct_to_def),
2657 (crate::Enum, ast::Enum, enum_to_def),
2658 (crate::Union, ast::Union, union_to_def),
2659 (crate::Trait, ast::Trait, trait_to_def),
2660 (crate::Impl, ast::Impl, impl_to_def),
2661 (crate::TypeAlias, ast::TypeAlias, type_alias_to_def),
2662 (crate::Const, ast::Const, const_to_def),
2663 (crate::Static, ast::Static, static_to_def),
2664 (crate::Function, ast::Fn, fn_to_def),
2665 (crate::Field, ast::RecordField, record_field_to_def),
2666 (crate::Field, ast::TupleField, tuple_field_to_def),
2667 (crate::EnumVariant, ast::Variant, enum_variant_to_def),
2668 (crate::TypeParam, ast::TypeParam, type_param_to_def),
2669 (crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def),
2670 (crate::ConstParam, ast::ConstParam, const_param_to_def),
2671 (crate::GenericParam, ast::GenericParam, generic_param_to_def),
2672 (crate::Macro, ast::Macro, macro_to_def),
2673 (crate::Local, ast::SelfParam, self_param_to_def),
2674 (crate::Label, ast::Label, label_to_def),
2675 (crate::Adt, ast::Adt, adt_to_def),
2676 (crate::ExternCrateDecl, ast::ExternCrate, extern_crate_to_def),
2677 (crate::InlineAsmOperand, ast::AsmOperandNamed, asm_operand_to_def),
2678 (crate::ExternBlock, ast::ExternBlock, extern_block_to_def),
2679 (MacroCallId, ast::MacroCall, macro_call_to_macro_call),
2680];
2681
2682impl ToDef for ast::IdentPat {
2683 type Def = crate::Local;
2684
2685 fn to_def(sema: &SemanticsImpl<'_>, src: InFile<&Self>) -> Option<Self::Def> {
2686 sema.with_ctx(|ctx| ctx.bind_pat_to_def(src, sema))
2687 }
2688}
2689
2690fn find_root(node: &SyntaxNode) -> SyntaxNode {
2691 node.ancestors().last().unwrap()
2692}
2693
2694#[derive(Debug)]
2714pub struct SemanticsScope<'db> {
2715 pub db: &'db dyn HirDatabase,
2716 infer_body: Option<InferBodyId>,
2717 file_id: HirFileId,
2718 resolver: Resolver<'db>,
2719}
2720
2721impl<'db> SemanticsScope<'db> {
2722 pub fn file_id(&self) -> HirFileId {
2723 self.file_id
2724 }
2725
2726 pub fn module(&self) -> Module {
2727 Module { id: self.resolver.module() }
2728 }
2729
2730 pub fn krate(&self) -> Crate {
2731 Crate { id: self.resolver.krate() }
2732 }
2733
2734 pub fn containing_function(&self) -> Option<Function> {
2736 self.resolver.expression_store_owner().and_then(|owner| match owner {
2737 ExpressionStoreOwnerId::Body(DefWithBodyId::FunctionId(id)) => Some(id.into()),
2738 _ => None,
2739 })
2740 }
2741
2742 pub fn expression_store_owner(&self) -> Option<ExpressionStoreOwner> {
2743 self.resolver.expression_store_owner().map(Into::into)
2744 }
2745
2746 pub(crate) fn resolver(&self) -> &Resolver<'db> {
2747 &self.resolver
2748 }
2749
2750 pub fn visible_traits(&self) -> VisibleTraits {
2752 let resolver = &self.resolver;
2753 VisibleTraits(resolver.traits_in_scope(self.db))
2754 }
2755
2756 pub fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
2758 let scope = self.resolver.names_in_scope(self.db);
2759 for (name, entries) in scope {
2760 for entry in entries {
2761 let def = match entry {
2762 resolver::ScopeDef::ModuleDef(it) => ScopeDef::ModuleDef(it.into()),
2763 resolver::ScopeDef::Unknown => ScopeDef::Unknown,
2764 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
2765 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
2766 resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(id.into()),
2767 resolver::ScopeDef::Local(binding_id) => {
2768 match (self.resolver.expression_store_owner(), self.infer_body) {
2769 (Some(parent), Some(parent_infer)) => {
2770 ScopeDef::Local(Local { parent, parent_infer, binding_id })
2771 }
2772 _ => continue,
2773 }
2774 }
2775 resolver::ScopeDef::Label(label_id) => {
2776 match self.resolver.expression_store_owner() {
2777 Some(parent) => ScopeDef::Label(Label { parent, label_id }),
2778 None => continue,
2779 }
2780 }
2781 };
2782 f(name.clone(), def)
2783 }
2784 }
2785 }
2786
2787 pub fn can_use_trait_methods(&self, t: Trait) -> bool {
2789 self.resolver.traits_in_scope(self.db).contains(&t.id)
2790 }
2791
2792 pub fn speculative_resolve(&self, ast_path: &ast::Path) -> Option<PathResolution> {
2795 let mut kind = PathKind::Plain;
2796 let mut segments = vec![];
2797 let mut first = true;
2798 for segment in ast_path.segments() {
2799 if first {
2800 first = false;
2801 if segment.coloncolon_token().is_some() {
2802 kind = PathKind::Abs;
2803 }
2804 }
2805
2806 let Some(k) = segment.kind() else { continue };
2807 match k {
2808 ast::PathSegmentKind::Name(name_ref) => segments.push(name_ref.as_name()),
2809 ast::PathSegmentKind::Type { .. } => continue,
2810 ast::PathSegmentKind::SelfTypeKw => {
2811 segments.push(Name::new_symbol_root(sym::Self_))
2812 }
2813 ast::PathSegmentKind::SelfKw => kind = PathKind::Super(0),
2814 ast::PathSegmentKind::SuperKw => match kind {
2815 PathKind::Super(s) => kind = PathKind::Super(s + 1),
2816 PathKind::Plain => kind = PathKind::Super(1),
2817 PathKind::Crate | PathKind::Abs | PathKind::DollarCrate(_) => continue,
2818 },
2819 ast::PathSegmentKind::CrateKw => kind = PathKind::Crate,
2820 }
2821 }
2822
2823 resolve_hir_path(
2824 self.db,
2825 &self.resolver,
2826 self.infer_body,
2827 &Path::BarePath(Interned::new(ModPath::from_segments(kind, segments))),
2828 HygieneId::ROOT,
2829 None,
2830 )
2831 }
2832
2833 pub fn resolve_mod_path(&self, path: &ModPath) -> impl Iterator<Item = ItemInNs> + use<> {
2834 let items = self.resolver.resolve_module_path_in_items(self.db, path);
2835 items.iter_items().map(|(item, _)| item.into())
2836 }
2837
2838 pub fn assoc_type_shorthand_candidates(
2841 &self,
2842 resolution: &PathResolution,
2843 mut cb: impl FnMut(TypeAlias),
2844 ) {
2845 let (Some(def), Some(resolution)) = (self.resolver.generic_def(), resolution.in_type_ns())
2846 else {
2847 return;
2848 };
2849 hir_ty::associated_type_shorthand_candidates(self.db, def, resolution, |_, id| {
2850 cb(id.into());
2851 false
2852 });
2853 }
2854
2855 pub fn generic_def(&self) -> Option<crate::GenericDef> {
2856 self.resolver.generic_def().map(|id| id.into())
2857 }
2858
2859 pub fn extern_crates(&self) -> impl Iterator<Item = (Name, Module)> + '_ {
2860 self.resolver.extern_crates_in_scope().map(|(name, id)| (name, Module { id }))
2861 }
2862
2863 pub fn extern_crate_decls(&self) -> impl Iterator<Item = Name> + '_ {
2864 self.resolver.extern_crate_decls_in_scope(self.db)
2865 }
2866
2867 pub fn has_same_self_type(&self, other: &SemanticsScope<'_>) -> bool {
2868 self.resolver.impl_def() == other.resolver.impl_def()
2869 }
2870}
2871
2872#[derive(Debug)]
2873pub struct VisibleTraits(pub FxHashSet<TraitId>);
2874
2875impl ops::Deref for VisibleTraits {
2876 type Target = FxHashSet<TraitId>;
2877
2878 fn deref(&self) -> &Self::Target {
2879 &self.0
2880 }
2881}
2882
2883struct RenameConflictsVisitor<'a> {
2884 db: &'a dyn HirDatabase,
2885 owner: ExpressionStoreOwnerId,
2886 resolver: Resolver<'a>,
2887 body: &'a ExpressionStore,
2888 to_be_renamed: BindingId,
2889 new_name: Symbol,
2890 old_name: Symbol,
2891 conflicts: FxHashSet<BindingId>,
2892}
2893
2894impl RenameConflictsVisitor<'_> {
2895 fn resolve_path(&mut self, node: ExprOrPatId, path: &Path) {
2896 if let Path::BarePath(path) = path
2897 && let Some(name) = path.as_ident()
2898 {
2899 if *name.symbol() == self.new_name {
2900 if let Some(conflicting) = self.resolver.rename_will_conflict_with_renamed(
2901 self.db,
2902 name,
2903 path,
2904 self.body.expr_or_pat_path_hygiene(node),
2905 self.to_be_renamed,
2906 ) {
2907 self.conflicts.insert(conflicting);
2908 }
2909 } else if *name.symbol() == self.old_name
2910 && let Some(conflicting) = self.resolver.rename_will_conflict_with_another_variable(
2911 self.db,
2912 name,
2913 path,
2914 self.body.expr_or_pat_path_hygiene(node),
2915 &self.new_name,
2916 self.to_be_renamed,
2917 )
2918 {
2919 self.conflicts.insert(conflicting);
2920 }
2921 }
2922 }
2923
2924 fn rename_conflicts(&mut self, expr: ExprId) {
2925 match &self.body[expr] {
2926 Expr::Path(path) => {
2927 let guard = self.resolver.update_to_inner_scope(self.db, self.owner, expr);
2928 self.resolve_path(expr.into(), path);
2929 self.resolver.reset_to_guard(guard);
2930 }
2931 _ => {}
2932 }
2933
2934 self.body.walk_child_exprs(expr, |expr| self.rename_conflicts(expr));
2935 }
2936}