Merge attempt

This commit is contained in:
Sam Mohr 2024-08-19 23:34:05 -07:00
parent 7c15c16ae0
commit 12df3a04de
No known key found for this signature in database
GPG key ID: EA41D161A3C1BC99
236 changed files with 9053 additions and 6428 deletions

View file

@ -14,7 +14,6 @@ roc_ident = { path = "../ident" }
roc_region = { path = "../region" }
bumpalo.workspace = true
snafu.workspace = true
static_assertions.workspace = true
[features]

View file

@ -95,6 +95,10 @@ pub enum CalledVia {
/// This call is the result of desugaring a Task.await from `!` syntax
/// e.g. Stdout.line! "Hello" becomes Task.await (Stdout.line "Hello") \{} -> ...
BangSuffix,
/// This call is the result of desugaring a Result.try from `?` syntax
/// e.g. Dict.get? items "key" becomes Result.try (Dict.get items "key") \item -> ...
QuestionSuffix,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@ -114,6 +118,23 @@ impl std::fmt::Display for UnaryOp {
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Suffix {
/// (!), e.g. (Stdin.line!)
Bang,
/// (?), e.g. (parseData? data)
Question,
}
impl std::fmt::Display for Suffix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Suffix::Bang => write!(f, "!"),
Suffix::Question => write!(f, "?"),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinOp {
// highest precedence

View file

@ -1,30 +1,45 @@
use snafu::{Backtrace, Snafu};
use crate::symbol::IdentId;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
#[derive(Debug)]
pub enum ModuleError {
#[snafu(display(
"ModuleIdNotFound: I could not find the ModuleId {} in Interns.all_ident_ids: {}.",
module_id,
all_ident_ids
))]
ModuleIdNotFound {
module_id: String,
all_ident_ids: String,
backtrace: Backtrace,
},
#[snafu(display(
"IdentIdNotFound: I could not find IdentId {:?} in ident_ids {:?}.",
ident_id,
ident_ids_str
))]
IdentIdNotFound {
ident_id: IdentId,
ident_ids_str: String,
backtrace: Backtrace,
},
}
impl std::fmt::Display for ModuleError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ModuleIdNotFound {
module_id,
all_ident_ids,
} => {
write!(
f,
"ModuleIdNotFound: I could not find the ModuleId {} in Interns.all_ident_ids: {}.",
module_id,
all_ident_ids
)
}
Self::IdentIdNotFound {
ident_id,
ident_ids_str,
} => {
write!(
f,
"IdentIdNotFound: I could not find IdentId {:?} in ident_ids {:?}.",
ident_id, ident_ids_str
)
}
}
}
}
impl std::error::Error for ModuleError {}
pub type ModuleResult<T, E = ModuleError> = std::result::Result<T, E>;

View file

@ -1,10 +1,9 @@
use crate::ident::{Ident, Lowercase, ModuleName};
use crate::module_err::{IdentIdNotFoundSnafu, ModuleIdNotFoundSnafu, ModuleResult};
use crate::module_err::{ModuleError, ModuleResult};
use roc_collections::{SmallStringInterner, VecMap};
use roc_error_macros::internal_error;
use roc_ident::IdentStr;
use roc_region::all::Region;
use snafu::OptionExt;
use std::num::NonZeroU32;
use std::{fmt, u32};
@ -324,7 +323,7 @@ pub fn get_module_ident_ids<'a>(
) -> ModuleResult<&'a IdentIds> {
all_ident_ids
.get(module_id)
.with_context(|| ModuleIdNotFoundSnafu {
.ok_or_else(|| ModuleError::ModuleIdNotFound {
module_id: format!("{module_id:?}"),
all_ident_ids: format!("{all_ident_ids:?}"),
})
@ -336,9 +335,10 @@ pub fn get_module_ident_ids_mut<'a>(
) -> ModuleResult<&'a mut IdentIds> {
all_ident_ids
.get_mut(module_id)
.with_context(|| ModuleIdNotFoundSnafu {
.ok_or_else(|| ModuleError::ModuleIdNotFound {
module_id: format!("{module_id:?}"),
all_ident_ids: "I could not return all_ident_ids here because of borrowing issues.",
all_ident_ids: "I could not return all_ident_ids here because of borrowing issues."
.into(),
})
}
@ -627,67 +627,12 @@ impl ModuleIds {
pub fn available_modules(&self) -> impl Iterator<Item = &ModuleName> {
self.by_id.iter()
}
}
#[derive(Debug, Clone)]
pub struct ScopeModules {
modules: VecMap<ModuleName, ModuleId>,
sources: VecMap<ModuleId, ScopeModuleSource>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScopeModuleSource {
Builtin,
Current,
Import(Region),
}
impl ScopeModules {
pub fn get_id(&self, module_name: &ModuleName) -> Option<ModuleId> {
self.modules.get(module_name).copied()
}
pub fn has_id(&self, module_id: ModuleId) -> bool {
self.sources.contains_key(&module_id)
}
pub fn available_names(&self) -> impl Iterator<Item = &ModuleName> {
self.modules.keys()
}
pub fn insert(
&mut self,
module_name: ModuleName,
module_id: ModuleId,
region: Region,
) -> Result<(), ScopeModuleSource> {
if let Some(existing_module_id) = self.modules.get(&module_name) {
if *existing_module_id == module_id {
return Ok(());
}
return Err(*self.sources.get(existing_module_id).unwrap());
}
self.modules.insert(module_name, module_id);
self.sources
.insert(module_id, ScopeModuleSource::Import(region));
Ok(())
}
pub fn len(&self) -> usize {
debug_assert_eq!(self.modules.len(), self.sources.len());
self.modules.len()
}
pub fn is_empty(&self) -> bool {
debug_assert_eq!(self.modules.is_empty(), self.sources.is_empty());
self.modules.is_empty()
}
pub fn truncate(&mut self, len: usize) {
self.modules.truncate(len);
self.sources.truncate(len);
pub fn iter(&self) -> impl ExactSizeIterator<Item = (ModuleId, &ModuleName)> {
self.by_id
.iter()
.enumerate()
.map(|(index, name)| (ModuleId::from_zero_indexed(index), name))
}
}
@ -782,7 +727,7 @@ impl IdentIds {
pub fn get_name_str_res(&self, ident_id: IdentId) -> ModuleResult<&str> {
self.get_name(ident_id)
.with_context(|| IdentIdNotFoundSnafu {
.ok_or_else(|| ModuleError::IdentIdNotFound {
ident_id,
ident_ids_str: format!("{self:?}"),
})
@ -1031,32 +976,6 @@ macro_rules! define_builtins {
}
}
impl ScopeModules {
pub fn new(home_id: ModuleId, home_name: ModuleName) -> Self {
// +1 because the user will be compiling at least 1 non-builtin module!
let capacity = $total + 1;
let mut modules = VecMap::with_capacity(capacity);
let mut sources = VecMap::with_capacity(capacity);
modules.insert(home_name, home_id);
sources.insert(home_id, ScopeModuleSource::Current);
let mut insert_both = |id: ModuleId, name_str: &'static str| {
let name: ModuleName = name_str.into();
modules.insert(name, id);
sources.insert(id, ScopeModuleSource::Builtin);
};
$(
insert_both(ModuleId::$module_const, $module_name);
)+
ScopeModules { modules, sources }
}
}
impl<'a> Default for PackageModuleIds<'a> {
fn default() -> Self {
// +1 because the user will be compiling at least 1 non-builtin module!
@ -1556,6 +1475,8 @@ define_builtins! {
5 RESULT_WITH_DEFAULT: "withDefault"
6 RESULT_TRY: "try"
7 RESULT_IS_OK: "isOk"
8 RESULT_MAP_BOTH: "mapBoth"
9 RESULT_MAP_TWO: "map2"
}
8 DICT: "Dict" => {
0 DICT_DICT: "Dict" exposed_type=true // the Dict.Dict type alias