Fix a bug when synthetic AST node were searched in the AST ID map and caused panics

This commit is contained in:
Chayim Refael Friedman 2024-11-25 14:52:58 +02:00
parent 9224ec4497
commit b66bc91b4b
3 changed files with 80 additions and 4 deletions

View file

@ -34,7 +34,7 @@ use intern::Symbol;
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::{smallvec, SmallVec};
use span::{EditionedFileId, FileId, HirFileIdRepr, SyntaxContextId};
use span::{AstIdMap, EditionedFileId, FileId, HirFileIdRepr, SyntaxContextId};
use stdx::TupleExt;
use syntax::{
algo::skip_trivia_token,
@ -42,6 +42,7 @@ use syntax::{
AstNode, AstToken, Direction, SyntaxKind, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange,
TextSize,
};
use triomphe::Arc;
use crate::{
db::HirDatabase,
@ -1973,10 +1974,16 @@ impl SemanticsScope<'_> {
/// Resolve a path as-if it was written at the given scope. This is
/// necessary a heuristic, as it doesn't take hygiene into account.
pub fn speculative_resolve(&self, ast_path: &ast::Path) -> Option<PathResolution> {
let root = ast_path.syntax().ancestors().last().unwrap();
let ast_id_map = Arc::new(AstIdMap::from_source(&root));
let (mut types_map, mut types_source_map) =
(TypesMap::default(), TypesSourceMap::default());
let mut ctx =
LowerCtx::new(self.db.upcast(), self.file_id, &mut types_map, &mut types_source_map);
let mut ctx = LowerCtx::for_synthetic_ast(
self.db.upcast(),
ast_id_map,
&mut types_map,
&mut types_source_map,
);
let path = Path::from_src(&mut ctx, ast_path.clone())?;
resolve_hir_path(
self.db,