mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 11:52:19 +00:00
Support shorthand ability implementation syntax
This commit is contained in:
parent
870294b564
commit
c2154ac311
5 changed files with 71 additions and 22 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue