show struct dependency cycles in error & update module cycle error (#2517)
parent
f14425ec18
commit
baf49b2523
|
@ -361,7 +361,9 @@ fn sort_structs(types []Type) []Type {
|
||||||
// sort graph
|
// sort graph
|
||||||
dep_graph_sorted := dep_graph.resolve()
|
dep_graph_sorted := dep_graph.resolve()
|
||||||
if !dep_graph_sorted.acyclic {
|
if !dep_graph_sorted.acyclic {
|
||||||
verror('error: cgen.sort_structs() DGNAC.\nplease create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
verror('cgen.sort_structs(): the following structs form a dependancy cycle:\n' +
|
||||||
|
dep_graph_sorted.display_cycles() +
|
||||||
|
'\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
||||||
}
|
}
|
||||||
// sort types
|
// sort types
|
||||||
mut types_sorted := []Type
|
mut types_sorted := []Type
|
||||||
|
|
|
@ -11,7 +11,6 @@ struct DepGraphNode {
|
||||||
mut:
|
mut:
|
||||||
name string
|
name string
|
||||||
deps []string
|
deps []string
|
||||||
last_cycle string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DepGraph {
|
struct DepGraph {
|
||||||
|
@ -84,13 +83,8 @@ pub fn(graph &DepGraph) resolve() &DepGraph {
|
||||||
if ready_set.size() == 0 {
|
if ready_set.size() == 0 {
|
||||||
mut g := new_dep_graph()
|
mut g := new_dep_graph()
|
||||||
g.acyclic = false
|
g.acyclic = false
|
||||||
ndk := node_deps.keys()
|
for name, _ in node_deps {
|
||||||
for name, _ in node_deps {
|
g.nodes << node_names[name]
|
||||||
mut node := node_names[name]
|
|
||||||
if name == ndk[node_deps.size-1] {
|
|
||||||
node.last_cycle = node_deps[name].items[node_deps[name].items.len-1]
|
|
||||||
}
|
|
||||||
g.nodes << node
|
|
||||||
}
|
}
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
@ -112,19 +106,31 @@ pub fn(graph &DepGraph) last_node() DepGraphNode {
|
||||||
return graph.nodes[graph.nodes.len-1]
|
return graph.nodes[graph.nodes.len-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn(graph &DepGraph) last_cycle() string {
|
pub fn(graph &DepGraph) display() string {
|
||||||
return graph.last_node().last_cycle
|
mut out := '\n'
|
||||||
}
|
for node in graph.nodes {
|
||||||
|
|
||||||
pub fn(graph &DepGraph) display() {
|
|
||||||
for i:=0; i<graph.nodes.len; i++ {
|
|
||||||
node := graph.nodes[i]
|
|
||||||
for dep in node.deps {
|
for dep in node.deps {
|
||||||
mut out := ' * $node.name -> $dep'
|
out += ' * $node.name -> $dep\n'
|
||||||
if !graph.acyclic && i == graph.nodes.len-1 && dep == node.last_cycle {
|
|
||||||
out += ' <-- last cycle'
|
|
||||||
}
|
|
||||||
println(out)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn(graph &DepGraph) display_cycles() string {
|
||||||
|
mut node_names := map[string]DepGraphNode
|
||||||
|
for node in graph.nodes {
|
||||||
|
node_names[node.name] = node
|
||||||
|
}
|
||||||
|
mut out := '\n'
|
||||||
|
for node in graph.nodes {
|
||||||
|
for dep in node.deps {
|
||||||
|
if !(dep in node_names) { continue }
|
||||||
|
dn := node_names[dep]
|
||||||
|
if node.name in dn.deps {
|
||||||
|
out += ' * $node.name -> $dep\n'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -733,8 +733,7 @@ pub fn (v &V) resolve_deps() &DepGraph {
|
||||||
dep_graph.from_import_tables(v.table.file_imports)
|
dep_graph.from_import_tables(v.table.file_imports)
|
||||||
deps_resolved := dep_graph.resolve()
|
deps_resolved := dep_graph.resolve()
|
||||||
if !deps_resolved.acyclic {
|
if !deps_resolved.acyclic {
|
||||||
deps_resolved.display()
|
verror('import cycle detected between the following modules: \n' + deps_resolved.display_cycles())
|
||||||
verror('import cycle detected')
|
|
||||||
}
|
}
|
||||||
return deps_resolved
|
return deps_resolved
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue