fix(semantic): allow root_node to be empty for empty trees. (#3084)

related to #3082, #3030 and #3069
This commit is contained in:
Ali Rezvani 2024-04-24 20:40:59 +03:30 committed by GitHub
parent 8de7399b08
commit 8d17ab36c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 2 deletions

View file

@ -52,7 +52,11 @@ impl Rule for NoBarrelFile {
fn run_once(&self, ctx: &LintContext<'_>) {
let semantic = ctx.semantic();
let module_record = semantic.module_record();
let Some(root) = ctx.nodes().iter().next() else { return };
let root_id = semantic.nodes().root();
if root_id == usize::MAX {
return;
}
let root = semantic.nodes().get_node(root_id);
let AstKind::Program(program) = root.kind() else { unreachable!() };

View file

@ -64,7 +64,7 @@ pub struct AstNodes<'a> {
impl<'a> Default for AstNodes<'a> {
fn default() -> Self {
Self {
root: AstNodeId::new(0),
root: AstNodeId::new(usize::MAX),
nodes: IndexVec::default(),
parent_ids: IndexVec::default(),
}
@ -110,6 +110,7 @@ impl<'a> AstNodes<'a> {
}
/// Get the root `AstNodeId`, It is always pointing to a `Program`.
/// Returns `None` if root node isn't set.
pub fn root(&self) -> AstNodeId {
self.root
}
@ -130,11 +131,13 @@ impl<'a> AstNodes<'a> {
}
/// Get the root node as immutable reference, It is always guaranteed to be a `Program`.
/// Returns `None` if root node isn't set.
pub fn root_node(&self) -> &AstNode<'a> {
self.get_node(self.root())
}
/// Get the root node as mutable reference, It is always guaranteed to be a `Program`.
/// Returns `None` if root node isn't set.
pub fn root_node_mut(&mut self) -> &mut AstNode<'a> {
self.get_node_mut(self.root())
}