Rename symbol and module lookup struts

This commit is contained in:
Agus Zubiaga 2024-07-06 11:42:53 -03:00
parent 33cbaa5cd8
commit 63e722f61d
No known key found for this signature in database
5 changed files with 53 additions and 34 deletions

View file

@ -1,6 +1,6 @@
use crate::env::Env;
use crate::procedure::{QualifiedReference, References};
use crate::scope::{LookedupSymbol, PendingAbilitiesInScope, Scope};
use crate::scope::{PendingAbilitiesInScope, Scope, SymbolLookup};
use roc_collections::{ImMap, MutSet, SendMap, VecMap, VecSet};
use roc_module::ident::{Ident, Lowercase, TagName};
use roc_module::symbol::Symbol;
@ -386,7 +386,10 @@ pub(crate) fn make_apply_symbol(
// Look it up in scope!
match scope.lookup_str(ident, region) {
Ok(LookedupSymbol { symbol, params: _ }) => {
Ok(SymbolLookup {
symbol,
module_params: _,
}) => {
references.insert_type_lookup(symbol, QualifiedReference::Unqualified);
Ok(symbol)
}
@ -398,7 +401,10 @@ pub(crate) fn make_apply_symbol(
}
} else {
match env.qualified_lookup(scope, module_name, ident, region) {
Ok(LookedupSymbol { symbol, params: _ }) => {
Ok(SymbolLookup {
symbol,
module_params: _,
}) => {
references.insert_type_lookup(symbol, QualifiedReference::Qualified);
Ok(symbol)
}

View file

@ -507,7 +507,7 @@ fn canonicalize_alias<'a>(
#[macro_export]
macro_rules! params_in_abilities_unimplemented {
($lookup:expr) => {
match $lookup.params {
match $lookup.module_params {
None => $lookup.symbol,
Some(_) => unimplemented!("params in abilities"),
}

View file

@ -1,7 +1,7 @@
use std::path::Path;
use crate::procedure::References;
use crate::scope::{LookedupModule, LookedupSymbol, Scope};
use crate::scope::{ModuleLookup, Scope, SymbolLookup};
use bumpalo::Bump;
use roc_collections::{MutMap, VecSet};
use roc_module::ident::{Ident, ModuleName};
@ -78,7 +78,7 @@ impl<'a> Env<'a> {
module_name_str: &str,
ident: &str,
region: Region,
) -> Result<LookedupSymbol, RuntimeError> {
) -> Result<SymbolLookup, RuntimeError> {
debug_assert!(
!module_name_str.is_empty(),
"Called env.qualified_lookup with an unqualified ident: {ident:?}"
@ -112,7 +112,7 @@ impl<'a> Env<'a> {
module_id: ModuleId,
ident: &str,
region: Region,
) -> Result<LookedupSymbol, RuntimeError> {
) -> Result<SymbolLookup, RuntimeError> {
if let Some(module) = scope.modules.lookup_by_id(&module_id) {
self.qualified_lookup_help(scope, module, ident, region)
} else {
@ -124,10 +124,10 @@ impl<'a> Env<'a> {
fn qualified_lookup_help(
&mut self,
scope: &Scope,
module: LookedupModule,
module: ModuleLookup,
ident: &str,
region: Region,
) -> Result<LookedupSymbol, RuntimeError> {
) -> Result<SymbolLookup, RuntimeError> {
let is_type_name = ident.starts_with(|c: char| c.is_uppercase());
// You can do qualified lookups on your own module, e.g.
@ -143,7 +143,7 @@ impl<'a> Env<'a> {
self.qualified_value_lookups.insert(symbol);
}
Ok(LookedupSymbol::no_params(symbol))
Ok(SymbolLookup::no_params(symbol))
}
None => {
let error = RuntimeError::LookupNotInScope {

View file

@ -10,7 +10,7 @@ use crate::num::{
use crate::params_in_abilities_unimplemented;
use crate::pattern::{canonicalize_pattern, BindingsFromPattern, Pattern, PermitShadows};
use crate::procedure::{QualifiedReference, References};
use crate::scope::{LookedupSymbol, Scope};
use crate::scope::{Scope, SymbolLookup};
use crate::traverse::{walk_expr, Visitor};
use roc_collections::soa::Index;
use roc_collections::{SendMap, VecMap, VecSet};
@ -1934,7 +1934,13 @@ fn canonicalize_var_lookup(
(can_expr, output)
}
fn lookup_to_expr(LookedupSymbol { symbol, params }: LookedupSymbol, var: Variable) -> Expr {
fn lookup_to_expr(
SymbolLookup {
symbol,
module_params: params,
}: SymbolLookup,
var: Variable,
) -> Expr {
if let Some(params) = params {
Expr::ParamsVar {
symbol,

View file

@ -76,7 +76,7 @@ impl Scope {
}
}
pub fn lookup(&self, ident: &Ident, region: Region) -> Result<LookedupSymbol, RuntimeError> {
pub fn lookup(&self, ident: &Ident, region: Region) -> Result<SymbolLookup, RuntimeError> {
self.lookup_str(ident.as_str(), region)
}
@ -91,7 +91,7 @@ impl Scope {
.push(("Set".into(), Symbol::SET_SET, Region::zero()));
}
pub fn lookup_str(&self, ident: &str, region: Region) -> Result<LookedupSymbol, RuntimeError> {
pub fn lookup_str(&self, ident: &str, region: Region) -> Result<SymbolLookup, RuntimeError> {
use ContainsIdent::*;
match self.scope_contains_ident(ident) {
@ -205,14 +205,14 @@ impl Scope {
}
}
fn has_imported_symbol(&self, ident: &str) -> Option<(LookedupSymbol, Region)> {
fn has_imported_symbol(&self, ident: &str) -> Option<(SymbolLookup, Region)> {
self.imported_symbols
.iter()
.find_map(|(import, symbol, original_region)| {
if ident == import.as_str() {
match self.modules.lookup_by_id(&symbol.module_id()) {
Some(module) => Some((module.into_symbol(*symbol), *original_region)),
None => Some((LookedupSymbol::no_params(*symbol), *original_region)),
None => Some((SymbolLookup::no_params(*symbol), *original_region)),
}
} else {
None
@ -394,9 +394,13 @@ impl Scope {
region: Region,
) -> Result<(), (Symbol, Region)> {
match self.scope_contains_ident(ident.as_str()) {
ContainsIdent::InScope(LookedupSymbol { symbol, params: _ }, region) => {
Err((symbol, region))
}
ContainsIdent::InScope(
SymbolLookup {
symbol,
module_params: _,
},
region,
) => Err((symbol, region)),
ContainsIdent::NotPresent | ContainsIdent::NotInScope(_) => {
self.imported_symbols.push((ident, symbol, region));
Ok(())
@ -541,7 +545,7 @@ pub fn create_alias(
#[derive(Debug)]
enum ContainsIdent {
InScope(LookedupSymbol, Region),
InScope(SymbolLookup, Region),
NotInScope(IdentId),
NotPresent,
}
@ -582,7 +586,7 @@ impl ScopedIdentIds {
let index = ident_id.index();
if self.in_scope[index] {
return InScope(
LookedupSymbol::no_params(Symbol::new(self.home, ident_id)),
SymbolLookup::no_params(Symbol::new(self.home, ident_id)),
self.regions[index],
);
} else {
@ -699,21 +703,21 @@ impl ScopeModules {
}
}
pub fn lookup(&self, module_name: &ModuleName) -> Option<LookedupModule> {
pub fn lookup(&self, module_name: &ModuleName) -> Option<ModuleLookup> {
self.names
.iter()
.position(|name| name == module_name)
.map(|index| LookedupModule {
.map(|index| ModuleLookup {
id: self.ids[index],
params: self.params[index],
})
}
pub fn lookup_by_id(&self, module_id: &ModuleId) -> Option<LookedupModule> {
pub fn lookup_by_id(&self, module_id: &ModuleId) -> Option<ModuleLookup> {
self.ids
.iter()
.position(|id| id == module_id)
.map(|index| LookedupModule {
.map(|index| ModuleLookup {
id: self.ids[index],
params: self.params[index],
})
@ -765,14 +769,17 @@ impl ScopeModules {
}
#[derive(Debug, Clone)]
pub struct LookedupSymbol {
pub struct SymbolLookup {
pub symbol: Symbol,
pub params: Option<Symbol>,
pub module_params: Option<Symbol>,
}
impl LookedupSymbol {
impl SymbolLookup {
pub fn new(symbol: Symbol, params: Option<Symbol>) -> Self {
Self { symbol, params }
Self {
symbol,
module_params: params,
}
}
pub fn no_params(symbol: Symbol) -> Self {
@ -780,18 +787,18 @@ impl LookedupSymbol {
}
}
pub struct LookedupModule {
pub struct ModuleLookup {
pub id: ModuleId,
pub params: Option<Symbol>,
}
impl LookedupModule {
pub fn into_symbol(&self, symbol: Symbol) -> LookedupSymbol {
impl ModuleLookup {
pub fn into_symbol(&self, symbol: Symbol) -> SymbolLookup {
debug_assert_eq!(symbol.module_id(), self.id);
LookedupSymbol {
SymbolLookup {
symbol,
params: self.params,
module_params: self.params,
}
}
}