Skip to content
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

fix: fix stackoverflow on schema circle dep #1628

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,4 +1526,20 @@ mod tests {
2
);
}

#[test]
fn test_schema_circle_dep() {
let sess = Arc::new(ParseSession::default());

let path = "src/advanced_resolver/test_data/circle_dep/circle_dep.k"
.to_string()
.replace("/", &std::path::MAIN_SEPARATOR.to_string());
let mut program = load_program(sess.clone(), &[&path], None, None)
.unwrap()
.program;
let mut gs = GlobalState::default();
Namer::find_symbols(&program, &mut gs);
let node_ty_map = resolver::resolve_program(&mut program).node_ty_map;
AdvancedResolver::resolve_program(&program, &mut gs, node_ty_map).unwrap();
}
}
14 changes: 14 additions & 0 deletions kclvm/sema/src/advanced_resolver/test_data/circle_dep/circle_dep.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema A(A):
name: str

schema B(C):
name: str

schema C(B):
name: str


schema D:
mixin [D]

mixin D for D
96 changes: 69 additions & 27 deletions kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,15 +1078,6 @@ impl Symbol for SchemaSymbol {
match self.attributes.get(name) {
Some(attribute) => Some(*attribute),
None => {
if let Some(parent_schema) = self.parent_schema {
if let Some(attribute) =
data.get_symbol(parent_schema)?
.get_attribute(name, data, module_info)
{
return Some(attribute);
}
}

if let Some(for_host) = self.for_host {
if let Some(attribute) =
data.get_symbol(for_host)?
Expand All @@ -1105,6 +1096,25 @@ impl Symbol for SchemaSymbol {
}
}

if let Some(_) = self.parent_schema {
let mut parents = vec![];
parents.push(self.id.unwrap());
self.get_parents(data, &mut parents);
if parents.len() > 1 {
for parent_schema in &parents[1..] {
if let Some(parent_schema) = data.get_schema_symbol(*parent_schema) {
let parent_attr = parent_schema.get_self_attr(data, module_info);
for attr in parent_attr {
if let Some(attribute) = data.get_symbol(attr) {
if attribute.get_name() == name {
return Some(attr);
}
}
}
}
}
}
}
None
}
}
Expand All @@ -1115,24 +1125,17 @@ impl Symbol for SchemaSymbol {
data: &Self::SymbolData,
module_info: Option<&ModuleInfo>,
) -> Vec<SymbolRef> {
let mut result = vec![];
for attribute in self.attributes.values() {
result.push(*attribute);
}
if let Some(parent_schema) = self.parent_schema {
if let Some(parent) = data.get_symbol(parent_schema) {
result.append(&mut parent.get_all_attributes(data, module_info))
}
}

if let Some(for_host) = self.for_host {
if let Some(for_host) = data.get_symbol(for_host) {
result.append(&mut for_host.get_all_attributes(data, module_info))
}
}
for mixin in self.mixins.iter() {
if let Some(mixin) = data.get_symbol(*mixin) {
result.append(&mut mixin.get_all_attributes(data, module_info))
let mut result = self.get_self_attr(data, module_info);
if let Some(_) = self.parent_schema {
let mut parents = vec![];
parents.push(self.id.unwrap());
self.get_parents(data, &mut parents);
if parents.len() > 1 {
for parent in &parents[1..] {
if let Some(schema_symbol) = data.get_schema_symbol(*parent) {
result.append(&mut schema_symbol.get_self_attr(data, module_info))
}
}
}
}
result
Expand Down Expand Up @@ -1233,6 +1236,45 @@ impl SchemaSymbol {
r#ref: HashSet::default(),
}
}

pub fn get_parents(&self, data: &SymbolData, parents: &mut Vec<SymbolRef>) {
if let Some(parent_schema_ref) = self.parent_schema {
if let Some(parent_schema) = data.get_symbol(parent_schema_ref) {
if let Some(schema_def) = parent_schema.get_definition() {
if let Some(parent_schema) = data.get_schema_symbol(schema_def) {
// circular reference
if !parents.contains(&schema_def) {
parents.push(schema_def);
parent_schema.get_parents(data, parents);
}
}
}
}
}
}

pub fn get_self_attr(
&self,
data: &SymbolData,
module_info: Option<&ModuleInfo>,
) -> Vec<SymbolRef> {
let mut result = vec![];
for attribute in self.attributes.values() {
result.push(*attribute);
}

if let Some(for_host) = self.for_host {
if let Some(for_host) = data.get_symbol(for_host) {
result.append(&mut for_host.get_all_attributes(data, module_info))
}
}
for mixin in self.mixins.iter() {
if let Some(mixin) = data.get_symbol(*mixin) {
result.append(&mut mixin.get_all_attributes(data, module_info))
}
}
result
}
}

#[allow(unused)]
Expand Down
Loading