-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: Fix a bug when synthetic AST node were searched in the AST ID map and caused panics #18555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,14 +34,15 @@ 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, | ||
ast::{self, HasAttrs as _, HasGenericParams, IsString as _}, | ||
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a blocker for this, but I wonder if this is a good API here. It feels brittle passing an ast node that isn't even from an actual file/expansion and then lowering it. |
||
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, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of this i have to say, I'd rather we pass the original file id even if it shouldn't ever be used. If we do use it and trigger a panic it will end up confusing devs that don't know of this part
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use the original file we can also trigger panics, only they will be less consistent and harder to debug. That was my motivation for doing it, although I agree it's not great.
Also note that the file ID is private to
LowerCtx
, so it should be easier to protect.