wcc: kind-filter type-position name resolution so a value can't shadow a same-named type (#225)
resolve_typename used the kind-blind scope_lookup_prefer, so a same-named value binding (param/let) in a closer scope hid the type it shadowed, wrongly rejecting valid Hare like 'fn f(off: off)'. wwstage already separates type/value namespaces; this aligns the cstage frontend up. New scope_lookup_type skips non-SK_TYPE syms and keeps scanning, preserving same-module preference. Byte-id-neutral: the new branch fires only on the old 'unknown type' error path.
This commit is contained in:
@@ -65,7 +65,9 @@ resolve_typename(Checker *c, Node *n)
|
||||
const char *nm = n->str;
|
||||
Type *bi = lookup_builtin(nm);
|
||||
if (bi) return bi;
|
||||
Sym *s = scope_lookup_prefer(c->cur, c->cur_mod, nm);
|
||||
/* #225: kind-filtered so a same-named value binding (param/let/fn)
|
||||
* in a closer scope can't hide the type binding it shadows. */
|
||||
Sym *s = scope_lookup_type(c->cur, c->cur_mod, nm);
|
||||
if (s == NULL && nm) {
|
||||
/* module-qualified: io.stream → strip the last dot prefix
|
||||
* and look up the leaf, filtering on the importing module's
|
||||
|
||||
@@ -116,6 +116,34 @@ scope_lookup_prefer(Scope *s, const char *mod, const char *name)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* scope_lookup_type — kind-filtered bare-leaf lookup for type position.
|
||||
*
|
||||
* Same FNV bucket + hashnext chain + parent walk and same-module
|
||||
* preference as scope_lookup_prefer, but skips every Sym whose kind
|
||||
* isn't SK_TYPE and KEEPS scanning — so it returns the innermost
|
||||
* SK_TYPE of `name`, looking past a same-named value binding (SK_VAR/
|
||||
* SK_PARAM/SK_FN) that shadows it in a closer scope. ww keeps type and
|
||||
* value namespaces separate (wwstage already does; #225 conformance
|
||||
* gap): a param `off` must not hide the global `type off`.
|
||||
*/
|
||||
Sym *
|
||||
scope_lookup_type(Scope *s, const char *mod, const char *name)
|
||||
{
|
||||
for (Scope *p = s; p; p = p->parent) {
|
||||
u64 h = hashstr(name) % p->nbuckets;
|
||||
Sym *fallback = NULL;
|
||||
for (Sym *b = p->buckets[h]; b; b = b->hashnext) {
|
||||
if (b->kind != SK_TYPE) continue;
|
||||
if (strcmp(b->name, name) != 0) continue;
|
||||
if (mod && b->mod && strcmp(b->mod, mod) == 0) return b;
|
||||
if (fallback == NULL) fallback = b;
|
||||
}
|
||||
if (fallback) return fallback;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Sym *
|
||||
scope_define(Scope *s, const char *name, Skind k, Type *t, Node *decl)
|
||||
{
|
||||
|
||||
@@ -526,6 +526,7 @@ Sym *scope_lookup(Scope*, const char *name); /* walk up parents */
|
||||
Sym *scope_lookup_local(Scope*, const char *name);
|
||||
Sym *scope_lookup_in_module(Scope*, const char *mod, const char *name);
|
||||
Sym *scope_lookup_prefer(Scope*, const char *mod, const char *name);
|
||||
Sym *scope_lookup_type(Scope*, const char *mod, const char *name);
|
||||
|
||||
/* ---- checker (check.c) -------------------------------------------- */
|
||||
typedef struct Checker Checker;
|
||||
|
||||
Reference in New Issue
Block a user