Support shorthand ability implementation syntax

This commit is contained in:
Ayaz Hafiz 2022-07-18 13:56:42 -04:00
parent 870294b564
commit c2154ac311
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
5 changed files with 71 additions and 22 deletions

View file

@ -30,6 +30,12 @@ pub struct Scope {
/// Identifiers that are imported (and introduced in the header)
imports: Vec<(Ident, Symbol, Region)>,
/// Shadows of an ability member, for example a local specialization of `eq` for the ability
/// member `Eq has eq : a, a -> Bool` gets a shadow symbol it can use for its implementation.
///
/// Only one shadow of an ability member is permitted per scope.
shadows: VecMap<Symbol, Loc<Symbol>>,
/// Identifiers that are in scope, and defined in the current module
pub locals: ScopedIdentIds,
}
@ -51,6 +57,7 @@ impl Scope {
locals: ScopedIdentIds::from_ident_ids(home, initial_ident_ids),
aliases: VecMap::default(),
abilities_store: starting_abilities_store,
shadows: VecMap::default(),
imports,
}
}
@ -59,6 +66,10 @@ impl Scope {
self.lookup_str(ident.as_str(), region)
}
pub fn lookup_ability_member_shadow(&self, member: Symbol) -> Option<Symbol> {
self.shadows.get(&member).map(|loc_shadow| loc_shadow.value)
}
pub fn add_docs_imports(&mut self) {
self.imports
.push(("Dict".into(), Symbol::DICT_DICT, Region::zero()));
@ -306,11 +317,26 @@ impl Scope {
.iter()
.any(|(_, members)| members.iter().any(|m| *m == original_symbol))
{
// TODO: remove register_specializing_symbol
self.abilities_store
.register_specializing_symbol(shadow_symbol, original_symbol);
match self.shadows.get(&original_symbol) {
Some(loc_original_shadow) => {
// Duplicate shadow of an ability members; that's illegal.
let shadow = Loc {
value: ident.clone(),
region,
};
Err((loc_original_shadow.region, shadow, shadow_symbol))
}
None => {
// TODO: remove register_specializing_symbol
self.abilities_store
.register_specializing_symbol(shadow_symbol, original_symbol);
Ok((shadow_symbol, Some(original_symbol)))
self.shadows
.insert(original_symbol, Loc::at(region, shadow_symbol));
Ok((shadow_symbol, Some(original_symbol)))
}
}
} else {
// This is an illegal shadow.
let shadow = Loc {