jfecher/ante

Name resolution of imports fails in trait definitions

PROMETHIA-27 opened this issue · 1 comments

The declare() function for TraitDefinition looks up types at declaration time, which fails to find imported types, around line 1300 by calling convert_type():

for declaration in self.declarations.iter_mut() {
    let definition = || DefinitionKind::TraitDefinition(trustme::make_mut(self_pointer));
    resolver.resolve_declarations(declaration.lhs.as_mut(), cache, definition);
    resolver.auto_declare = true;
    let rhs = resolver.convert_type(cache, &declaration.rhs);
    resolver.auto_declare = false;
    declaration.typ = Some(rhs);
}

This breaks trait definitions attempting to use imported types like so:

import NonNull

trait Allocator a with
   alloc : U32 -> Maybe (NonNull U8)
   dealloc : NonNull U8

print <| size_of <| MkType : Type (NonNull U8)

Fixed in 7f7c664.

The issue was indeed that we were trying to resolve types during the declare pass. Moving this code into the define pass instead fixes the issue. Code also had to be added to re-insert the type variables declared in the declare pass back into scope for the define pass - otherwise each trait function would resolve with different type variables internally than what the overall trait has.