Files
hakorune/tools/hako_check/rules/rule_duplicate_method.hako

84 lines
2.4 KiB
Plaintext
Raw Normal View History

// tools/hako_check/rules/rule_duplicate_method.hako — HC013: Duplicate Method
// Detect methods with the same name and arity defined multiple times in the same box.
using selfhost.shared.common.string_helpers as Str
static box RuleDuplicateMethodBox {
method apply_ir(ir, path, out) {
local boxes = ir.get("boxes")
if boxes == null { return 0 }
// Check each box for duplicate methods
local bi = 0
while bi < boxes.size() {
local box_info = boxes.get(bi)
if box_info == null { bi = bi + 1; continue }
local box_name = box_info.get("name")
if box_name == null { bi = bi + 1; continue }
local methods = box_info.get("methods")
if methods == null { bi = bi + 1; continue }
// Track seen methods: {name/arity -> first_span}
local seen = new MapBox()
local mi = 0
while mi < methods.size() {
local method = methods.get(mi)
if method == null { mi = mi + 1; continue }
local name = method.get("name")
local arity = method.get("arity")
if name == null || arity == null { mi = mi + 1; continue }
// Create signature: name/arity
local sig = name + "/" + me._itoa(arity)
// Check if already seen
local first_span = seen.get(sig)
if first_span != null {
// Check if it's a [map/missing] error
local first_span_str = first_span + ""
if first_span_str.indexOf("[map/missing]") != 0 {
// Duplicate detected!
local span = method.get("span")
local line = (span != null) ? span : 1
out.push("[HC013] duplicate method definition: " + box_name + "." + sig + " at line " + me._itoa(line))
} else {
// First occurrence
local span = method.get("span")
seen.set(sig, span)
}
} else {
// First occurrence
local span = method.get("span")
seen.set(sig, span)
}
mi = mi + 1
}
bi = bi + 1
}
return out.size()
}
_itoa(n) {
local v = 0 + n
if v == 0 { return "0" }
local out = ""
local digits = "0123456789"
local tmp = ""
while v > 0 {
local d = v % 10
tmp = digits.substring(d, d+1) + tmp
v = v / 10
}
out = tmp
return out
}
}
static box RuleDuplicateMethodMain { method main(args) { return 0 } }