1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

show struct dependency cycles in error & update module cycle error (#2517)

This commit is contained in:
joe-conigliaro 2019-10-24 15:53:39 +11:00 committed by GitHub
parent f14425ec18
commit baf49b2523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 23 deletions

View File

@ -361,7 +361,9 @@ fn sort_structs(types []Type) []Type {
// sort graph
dep_graph_sorted := dep_graph.resolve()
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
mut types_sorted := []Type

View File

@ -11,7 +11,6 @@ struct DepGraphNode {
mut:
name string
deps []string
last_cycle string
}
struct DepGraph {
@ -84,13 +83,8 @@ pub fn(graph &DepGraph) resolve() &DepGraph {
if ready_set.size() == 0 {
mut g := new_dep_graph()
g.acyclic = false
ndk := node_deps.keys()
for name, _ in node_deps {
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
g.nodes << node_names[name]
}
return g
}
@ -112,19 +106,31 @@ pub fn(graph &DepGraph) last_node() DepGraphNode {
return graph.nodes[graph.nodes.len-1]
}
pub fn(graph &DepGraph) last_cycle() string {
return graph.last_node().last_cycle
pub fn(graph &DepGraph) display() string {
mut out := '\n'
for node in graph.nodes {
for dep in node.deps {
out += ' * $node.name -> $dep\n'
}
}
return out
}
pub fn(graph &DepGraph) display() {
for i:=0; i<graph.nodes.len; i++ {
node := graph.nodes[i]
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 {
mut out := ' * $node.name -> $dep'
if !graph.acyclic && i == graph.nodes.len-1 && dep == node.last_cycle {
out += ' <-- last cycle'
}
println(out)
if !(dep in node_names) { continue }
dn := node_names[dep]
if node.name in dn.deps {
out += ' * $node.name -> $dep\n'
}
}
}
return out
}

View File

@ -733,8 +733,7 @@ pub fn (v &V) resolve_deps() &DepGraph {
dep_graph.from_import_tables(v.table.file_imports)
deps_resolved := dep_graph.resolve()
if !deps_resolved.acyclic {
deps_resolved.display()
verror('import cycle detected')
verror('import cycle detected between the following modules: \n' + deps_resolved.display_cycles())
}
return deps_resolved
}