Skip to content

Commit

Permalink
clarify declarations/locals and unnameable semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Jul 29, 2024
1 parent 6a421c7 commit cd8cff2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
17 changes: 4 additions & 13 deletions crates/dash_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,10 @@ impl<'interner> FunctionCompiler<'interner> {
fn add_external_to_func(&mut self, func_id: ScopeId, external_id: u16, is_nested_external: bool) -> usize {
let fun = self.function_stack.iter_mut().rev().find(|f| f.id == func_id);
let externals = &mut fun.unwrap().externals;
if let Some(id) = externals.iter().position(|ext| {
ext.id == external_id && {
// assert_eq!(ext.is_nested_external, is_nested_external);
// true
ext.is_nested_external == is_nested_external
}
}) {
if let Some(id) = externals
.iter()
.position(|ext| ext.id == external_id && ext.is_nested_external == is_nested_external)
{
id
} else {
externals.push(External {
Expand Down Expand Up @@ -348,7 +345,6 @@ impl<'interner> FunctionCompiler<'interner> {

/// This is the same as `find_local` but should be used
/// when type_infer must have discovered/registered the local variable in the current scope.
/// It would be *wrong* to go up scopes sometimes.
fn find_local_from_binding(&mut self, binding: Binding) -> u16 {
self.decl_to_slot.slot_from_local(binding.id)
}
Expand Down Expand Up @@ -1849,11 +1845,6 @@ impl<'interner> Visitor<Result<(), Error>> for FunctionCompiler<'interner> {
ib.current_function_mut()
.finally_labels
.push(finally.as_ref().map(|&(finally_id, _)| Label::Finally { finally_id }));
// if true {
// panic!(
// "add a test for ensuring we dont really need to enter a scope here because try_ is already a block stmt"
// );
// }
let res = ib.accept(*try_);
ib.current_function_mut().try_depth -= 1;
res?;
Expand Down
8 changes: 7 additions & 1 deletion crates/dash_middle/src/compiler/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ pub struct LimitExceededError;

#[derive(Debug, Clone)]
pub struct FunctionScope {
// TODO: document the difference between this and `declarations`
/// All locals that are declared in this function.
/// Every scope that this function encloses (including the function's `Scope` itself) will have its own declarations.
/// Some scopes may not have any declarations at all, such as unnameable locals.
/// There can also be multiple locals with the same name.
pub locals: Vec<Local>,
}
impl FunctionScope {
Expand Down Expand Up @@ -40,6 +43,9 @@ pub struct Scope {
pub kind: ScopeKind,
pub parent: Option<ScopeId>,
pub subscopes: Vec<ScopeId>,
/// All variable declarations in this scope.
/// It contains the identifier, as well as the local slot
/// (index into `functions` of the enclosing function scope).
pub declarations: Vec<(Symbol, u16)>,
}

Expand Down
3 changes: 2 additions & 1 deletion crates/dash_middle/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ pub enum VariableDeclarationKind {

/// Unnameable variables cannot be referred to by JavaScript code directly and are created by the compiler
///
/// TODO: be more detailed about the semantics here
/// Multiple unnamed variables of the same name can exist in a function's `locals` vec and all `find` operations
/// on the `ScopeGraph` will never consider unnamed variables.
#[display(fmt = "__intrinsic_var")]
Unnameable,
}
Expand Down

0 comments on commit cd8cff2

Please sign in to comment.