fix(semantic): revert test code pushed to the main by accident. (#3085)

I'm terribly sorry, I've reverted the wrong commit in #3084.
This commit is contained in:
Ali Rezvani 2024-04-25 04:13:49 +03:30 committed by GitHub
parent 8d17ab36c3
commit dcb2528861
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 22 deletions

View file

@ -52,11 +52,11 @@ impl Rule for NoBarrelFile {
fn run_once(&self, ctx: &LintContext<'_>) {
let semantic = ctx.semantic();
let module_record = semantic.module_record();
let root_id = semantic.nodes().root();
if root_id == usize::MAX {
let Some(root) = semantic.nodes().root_node() else {
// Return early if the semantic's root node isn't set.
// It usually means we are running on an empty or invalid file.
return;
}
let root = semantic.nodes().get_node(root_id);
};
let AstKind::Program(program) = root.kind() else { unreachable!() };

View file

@ -54,23 +54,13 @@ impl<'a> AstNode<'a> {
}
/// Untyped AST nodes flattened into an vec
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct AstNodes<'a> {
root: AstNodeId,
root: Option<AstNodeId>,
nodes: IndexVec<AstNodeId, AstNode<'a>>,
parent_ids: IndexVec<AstNodeId, Option<AstNodeId>>,
}
impl<'a> Default for AstNodes<'a> {
fn default() -> Self {
Self {
root: AstNodeId::new(usize::MAX),
nodes: IndexVec::default(),
parent_ids: IndexVec::default(),
}
}
}
impl<'a> AstNodes<'a> {
pub fn iter(&self) -> impl Iterator<Item = &AstNode<'a>> + '_ {
self.nodes.iter()
@ -111,7 +101,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 {
pub fn root(&self) -> Option<AstNodeId> {
self.root
}
@ -124,7 +114,7 @@ impl<'a> AstNodes<'a> {
pub(super) unsafe fn set_root(&mut self, root: &AstNode<'a>) {
match root.kind() {
AstKind::Program(_) => {
self.root = root.id();
self.root = Some(root.id());
}
_ => unreachable!("Expected a `Program` node as the root of the tree."),
}
@ -132,14 +122,14 @@ 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())
pub fn root_node(&self) -> Option<&AstNode<'a>> {
self.root().map(|id| self.get_node(id))
}
/// 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())
pub fn root_node_mut(&mut self) -> Option<&mut AstNode<'a>> {
self.root().map(|id| self.get_node_mut(id))
}
/// Walk up the AST, iterating over each parent node.
@ -151,6 +141,7 @@ impl<'a> AstNodes<'a> {
std::iter::successors(Some(ast_node_id), |node_id| parent_ids[*node_id])
}
/// Adds an `AstNode` to the `AstNodes` tree and returns its `AstNodeId`.
pub fn add_node(&mut self, node: AstNode<'a>, parent_id: Option<AstNodeId>) -> AstNodeId {
let mut node = node;
let ast_node_id = self.parent_ids.push(parent_id);