Fix broken constants in scopes (#9679)

This commit is contained in:
Jakub Žádník
2023-07-14 00:02:05 +03:00
committed by GitHub
parent 30904bd095
commit e66139e6bb
8 changed files with 55 additions and 48 deletions

View File

@ -233,9 +233,6 @@ impl EngineState {
for item in delta_overlay.vars.into_iter() {
existing_overlay.vars.insert(item.0, item.1);
}
for item in delta_overlay.constants.into_iter() {
existing_overlay.constants.insert(item.0, item.1);
}
for item in delta_overlay.modules.into_iter() {
existing_overlay.modules.insert(item.0, item.1);
}
@ -727,16 +724,6 @@ impl EngineState {
output
}
pub fn find_constant(&self, var_id: VarId, removed_overlays: &[Vec<u8>]) -> Option<&Value> {
for overlay_frame in self.active_overlays(removed_overlays).rev() {
if let Some(val) = overlay_frame.constants.get(&var_id) {
return Some(val);
}
}
None
}
pub fn get_span_contents(&self, span: &Span) -> &[u8] {
for (contents, start, finish) in &self.file_contents {
if span.start >= *start && span.end <= *finish {
@ -1639,23 +1626,13 @@ impl<'a> StateWorkingSet<'a> {
}
}
pub fn add_constant(&mut self, var_id: VarId, val: Value) {
self.last_overlay_mut().constants.insert(var_id, val);
}
pub fn find_constant(&self, var_id: VarId) -> Option<&Value> {
let mut removed_overlays = vec![];
for scope_frame in self.delta.scope.iter().rev() {
for overlay_frame in scope_frame.active_overlays(&mut removed_overlays).rev() {
if let Some(val) = overlay_frame.constants.get(&var_id) {
return Some(val);
}
}
pub fn set_variable_const_val(&mut self, var_id: VarId, val: Value) {
let num_permanent_vars = self.permanent_state.num_vars();
if var_id < num_permanent_vars {
panic!("Internal error: attempted to set into permanent state from working set")
} else {
self.delta.vars[var_id - num_permanent_vars].const_val = Some(val);
}
self.permanent_state
.find_constant(var_id, &removed_overlays)
}
pub fn get_variable(&self, var_id: VarId) -> &Variable {

View File

@ -1,4 +1,4 @@
use crate::{DeclId, ModuleId, OverlayId, Value, VarId};
use crate::{DeclId, ModuleId, OverlayId, VarId};
use std::collections::HashMap;
pub static DEFAULT_OVERLAY_NAME: &str = "zero";
@ -177,7 +177,6 @@ impl ScopeFrame {
#[derive(Debug, Clone)]
pub struct OverlayFrame {
pub vars: HashMap<Vec<u8>, VarId>,
pub constants: HashMap<VarId, Value>,
pub predecls: HashMap<Vec<u8>, DeclId>, // temporary storage for predeclarations
pub decls: HashMap<Vec<u8>, DeclId>,
pub modules: HashMap<Vec<u8>, ModuleId>,
@ -190,7 +189,6 @@ impl OverlayFrame {
pub fn from_origin(origin: ModuleId, prefixed: bool) -> Self {
Self {
vars: HashMap::new(),
constants: HashMap::new(),
predecls: HashMap::new(),
decls: HashMap::new(),
modules: HashMap::new(),

View File

@ -1,10 +1,11 @@
use crate::{Span, Type};
use crate::{Span, Type, Value};
#[derive(Clone, Debug)]
pub struct Variable {
pub declaration_span: Span,
pub ty: Type,
pub mutable: bool,
pub const_val: Option<Value>,
}
impl Variable {
@ -13,6 +14,7 @@ impl Variable {
declaration_span,
ty,
mutable,
const_val: None,
}
}
}