Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/gnomon-db/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ mod tests {
let r = expect_single_decl(&result);
let uid_name = FieldName::new(&db, "uid".to_string());
let blamed_value = r.get(&db, &uid_name).unwrap();
assert_eq!(blamed_value.blame.path.0.len(), 1);
assert_eq!(blamed_value.blame.path.segments(&db).len(), 1);
}

// ── Every expression lowering ────────────────────────────────
Expand Down
8 changes: 4 additions & 4 deletions crates/gnomon-db/src/eval/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn desugar_every<'db>(
"by_year_day",
Value::List(vec![Blamed {
value: Value::Integer(year_day),
blame: blame.clone(),
blame: *blame,
}]),
));
}
Expand All @@ -156,7 +156,7 @@ pub fn desugar_every<'db>(
"by_day",
Value::List(vec![Blamed {
value: Value::Record(nday_record),
blame: blame.clone(),
blame: *blame,
}]),
));
} else {
Expand Down Expand Up @@ -199,7 +199,7 @@ pub(super) fn make_record<'db>(
field_name,
Blamed {
value: value.clone(),
blame: blame.clone(),
blame: *blame,
},
);
}
Expand Down Expand Up @@ -241,7 +241,7 @@ mod tests {
let decl_id = DeclId::new(db, source, 0, DeclKind::Event);
Blame {
decl: decl_id,
path: FieldPath::root(),
path: FieldPath::root(db),
}
}

Expand Down
16 changes: 8 additions & 8 deletions crates/gnomon-db/src/eval/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod tests {
let source = SourceFile::new(db, "/test".into(), String::new());
Blame {
decl: DeclId::new(db, source, 0, DeclKind::Expr),
path: FieldPath::root(),
path: FieldPath::root(db),
}
}

Expand All @@ -88,15 +88,15 @@ mod tests {
FieldName::new(&db, "name".to_string()),
Blamed {
value: Value::String("hello".to_string()),
blame: blame.clone(),
blame,
},
);
record.insert(
&db,
FieldName::new(&db, "count".to_string()),
Blamed {
value: Value::Integer(42),
blame: blame.clone(),
blame,
},
);

Expand Down Expand Up @@ -129,15 +129,15 @@ mod tests {
FieldName::new(&db, "uid".to_string()),
Blamed {
value: Value::String("cal-1".to_string()),
blame: blame.clone(),
blame,
},
);
props.insert(
&db,
FieldName::new(&db, "type".to_string()),
Blamed {
value: Value::String("calendar".to_string()),
blame: blame.clone(),
blame,
},
);

Expand All @@ -147,23 +147,23 @@ mod tests {
FieldName::new(&db, "type".to_string()),
Blamed {
value: Value::String("event".to_string()),
blame: blame.clone(),
blame,
},
);
entry.insert(
&db,
FieldName::new(&db, "title".to_string()),
Blamed {
value: Value::String("Standup".to_string()),
blame: blame.clone(),
blame,
},
);

let calendar = Calendar {
properties: props,
entries: vec![Blamed {
value: entry,
blame: blame.clone(),
blame,
}],
foreign_import: false,
};
Expand Down
4 changes: 2 additions & 2 deletions crates/gnomon-db/src/eval/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn import_value_to_value<'db>(
.into_iter()
.map(|v| Blamed {
value: import_value_to_value(db, v, blame),
blame: blame.clone(),
blame: *blame,
})
.collect();
Value::List(blamed_items)
Expand All @@ -46,7 +46,7 @@ fn import_record_to_record<'db>(
field_name,
Blamed {
value: import_value_to_value(db, val, blame),
blame: blame.clone(),
blame: *blame,
},
);
}
Expand Down
48 changes: 31 additions & 17 deletions crates/gnomon-db/src/eval/interned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,50 @@ impl fmt::Debug for FieldName<'_> {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PathSegment<'db> {
Field(FieldName<'db>),
/// A segment of a [`FieldPath`]. Uses [`salsa::Id`] instead of [`FieldName`]
/// to keep the type `'static`, allowing `FieldPath` to be salsa-interned.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PathSegment {
Field(salsa::Id),
Index(usize),
}

/// A path into a record structure, e.g. `[Field("alerts"), Index(0), Field("trigger")]`.
///
/// Not salsa-interned because it contains `FieldName<'db>` values that carry a
/// non-`'static` lifetime.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FieldPath<'db>(pub Vec<PathSegment<'db>>);
/// Salsa-interned for O(1) cloning — paths are stored in every [`super::types::Blame`]
/// value, so cheap clones matter.
#[salsa::interned]
pub struct FieldPath<'db> {
#[returns(ref)]
pub segments: Vec<PathSegment>,
}

impl fmt::Debug for FieldPath<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let id: salsa::Id = salsa::plumbing::AsId::as_id(self);
f.debug_tuple("FieldPath").field(&id).finish()
}
}

impl<'db> FieldPath<'db> {
pub fn root() -> Self {
Self(Vec::new())
pub fn root(db: &'db dyn crate::Db) -> Self {
FieldPath::new(db, Vec::new())
}

pub fn push(&self, segment: PathSegment<'db>) -> Self {
let mut segments = self.0.clone();
segments.push(segment);
Self(segments)
pub fn field(&self, db: &'db dyn crate::Db, name: FieldName<'db>) -> Self {
self.push(db, PathSegment::Field(salsa::plumbing::AsId::as_id(&name)))
}

pub fn field(&self, name: FieldName<'db>) -> Self {
self.push(PathSegment::Field(name))
pub fn index(&self, db: &'db dyn crate::Db, i: usize) -> Self {
self.push(db, PathSegment::Index(i))
}

pub fn index(&self, i: usize) -> Self {
self.push(PathSegment::Index(i))
fn push(&self, db: &'db dyn crate::Db, segment: PathSegment) -> Self {
let segs = self.segments(db);
let mut new = Vec::with_capacity(segs.len() + 1);
new.extend_from_slice(segs);
new.push(segment);
FieldPath::new(db, new)
}
}

Expand Down
29 changes: 15 additions & 14 deletions crates/gnomon-db/src/eval/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'db> LowerCtx<'db> {
let name = name_tok.text().to_string();
let decl_id = self.make_decl_id(0, DeclKind::Expr);
let value = match binding.value_expr() {
Some(expr) => self.lower_top_expr(&expr, decl_id, &FieldPath::root()),
Some(expr) => self.lower_top_expr(&expr, decl_id, &FieldPath::root(self.db)),
None => Value::Undefined,
};
self.env.push((name, value));
Expand All @@ -131,7 +131,7 @@ impl<'db> LowerCtx<'db> {
.enumerate()
.map(|(i, expr)| {
let decl_id = self.make_decl_id(i, decl_kind_for_expr(expr));
let value = self.lower_top_expr(expr, decl_id, &FieldPath::root());
let value = self.lower_top_expr(expr, decl_id, &FieldPath::root(self.db));
Blamed {
value,
blame: self.root_blame(decl_id),
Expand All @@ -142,13 +142,13 @@ impl<'db> LowerCtx<'db> {
} else {
// Single expression mode
let decl_id = self.make_decl_id(0, DeclKind::Expr);
self.lower_top_expr(&exprs[0], decl_id, &FieldPath::root())
self.lower_top_expr(&exprs[0], decl_id, &FieldPath::root(self.db))
}
}
}

fn lower_event(&mut self, ev: &ast::EventExpr, decl_id: DeclId<'db>) -> Record<'db> {
let base_path = FieldPath::root();
let base_path = FieldPath::root(self.db);

if ev.name().is_some() {
// r[impl decl.short-event.desugar+2]
Expand All @@ -173,7 +173,8 @@ impl<'db> LowerCtx<'db> {
self.insert_field(&mut record, "start", value, decl_id, &base_path);
}
if let Some(dur_token) = span.duration() {
let blame = self.make_blame(decl_id, &base_path.field(self.intern("duration")));
let blame = self
.make_blame(decl_id, &base_path.field(self.db, self.intern("duration")));
if let Some(value) =
desugar::desugar_duration(self.db, dur_token.text(), &blame)
{
Expand Down Expand Up @@ -210,7 +211,7 @@ impl<'db> LowerCtx<'db> {
}

fn lower_task(&mut self, task: &ast::TaskExpr, decl_id: DeclId<'db>) -> Record<'db> {
let base_path = FieldPath::root();
let base_path = FieldPath::root(self.db);

if task.name().is_some() {
// r[impl decl.short-task.desugar+2]
Expand Down Expand Up @@ -277,7 +278,7 @@ impl<'db> LowerCtx<'db> {
};

let field_name = self.intern(name_token.text());
let field_path = base_path.field(field_name);
let field_path = base_path.field(self.db, field_name);
let blame = self.make_blame(decl_id, &field_path);

let value = self.lower_top_expr(&value_expr, decl_id, &field_path);
Expand Down Expand Up @@ -556,7 +557,7 @@ impl<'db> LowerCtx<'db> {
) -> Value<'db> {
let mut items = Vec::new();
for (i, elem) in list.elements().enumerate() {
let elem_path = base_path.index(i);
let elem_path = base_path.index(self.db, i);
let blame = self.make_blame(decl_id, &elem_path);
let value = self.lower_top_expr(&elem, decl_id, &elem_path);
items.push(Blamed { value, blame });
Expand All @@ -571,7 +572,7 @@ impl<'db> LowerCtx<'db> {
base_path: &FieldPath<'db>,
field_name: &str,
) -> Option<Value<'db>> {
let blame = self.make_blame(decl_id, &base_path.field(self.intern(field_name)));
let blame = self.make_blame(decl_id, &base_path.field(self.db, self.intern(field_name)));
if let Some(datetime_token) = dt.datetime() {
desugar::desugar_datetime(self.db, datetime_token.text(), &blame)
} else {
Expand Down Expand Up @@ -783,7 +784,7 @@ impl<'db> LowerCtx<'db> {
}
ImportFormat::ICalendar => {
let decl_id = self.make_decl_id(0, DeclKind::Expr);
let blame = self.make_blame(decl_id, &FieldPath::root());
let blame = self.make_blame(decl_id, &FieldPath::root(self.db));
match super::import::translate_icalendar(self.db, &content, &blame) {
Ok(value) => value,
Err(msg) => {
Expand All @@ -794,7 +795,7 @@ impl<'db> LowerCtx<'db> {
}
ImportFormat::JSCalendar => {
let decl_id = self.make_decl_id(0, DeclKind::Expr);
let blame = self.make_blame(decl_id, &FieldPath::root());
let blame = self.make_blame(decl_id, &FieldPath::root(self.db));
match super::import::translate_jscalendar(self.db, &content, &blame) {
Ok(value) => value,
Err(msg) => {
Expand All @@ -820,14 +821,14 @@ impl<'db> LowerCtx<'db> {
fn root_blame(&self, decl_id: DeclId<'db>) -> Blame<'db> {
Blame {
decl: decl_id,
path: FieldPath::root(),
path: FieldPath::root(self.db),
}
}

fn make_blame(&self, decl_id: DeclId<'db>, path: &FieldPath<'db>) -> Blame<'db> {
Blame {
decl: decl_id,
path: path.clone(),
path: *path,
}
}

Expand All @@ -840,7 +841,7 @@ impl<'db> LowerCtx<'db> {
base_path: &FieldPath<'db>,
) {
let field_name = self.intern(name);
let blame = self.make_blame(decl_id, &base_path.field(field_name));
let blame = self.make_blame(decl_id, &base_path.field(self.db, field_name));
record.insert(self.db, field_name, Blamed { value, blame });
}

Expand Down
8 changes: 4 additions & 4 deletions crates/gnomon-db/src/eval/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn validate_calendar<'db>(
);
calendar.entries.push(Blamed {
value: r.clone(),
blame: item.blame.clone(),
blame: item.blame,
});
}
}
Expand Down Expand Up @@ -241,7 +241,7 @@ fn derive_uids<'db>(
uid_key,
Blamed {
value: Value::String(derived.to_string()),
blame: entry.blame.clone(),
blame: entry.blame,
},
);
}
Expand Down Expand Up @@ -289,15 +289,15 @@ fn flatten_to_records<'db>(

let default_blame = || super::types::Blame {
decl: DeclId::new(db, source, 0, DeclKind::Calendar),
path: FieldPath::root(),
path: FieldPath::root(db),
};

match value {
Value::Record(r) => {
let blame = r
.values()
.next()
.map(|b| b.blame.clone())
.map(|b| b.blame)
.unwrap_or_else(default_blame);
vec![(r, blame)]
}
Expand Down
9 changes: 6 additions & 3 deletions crates/gnomon-db/src/eval/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,21 @@ impl<'db> RenderWithDb<'db> for DeclId<'db> {
}
}

impl<'db> RenderWithDb<'db> for PathSegment<'db> {
impl<'db> RenderWithDb<'db> for PathSegment {
fn render_fmt(&self, f: &mut fmt::Formatter<'_>, db: &'db dyn Db) -> fmt::Result {
match self {
PathSegment::Field(name) => name.render_fmt(f, db),
PathSegment::Field(id) => {
let name: FieldName<'db> = salsa::plumbing::FromId::from_id(*id);
name.render_fmt(f, db)
}
PathSegment::Index(i) => write!(f, "[{i}]"),
}
}
}

impl<'db> RenderWithDb<'db> for FieldPath<'db> {
fn render_fmt(&self, f: &mut fmt::Formatter<'_>, db: &'db dyn Db) -> fmt::Result {
for (i, segment) in self.0.iter().enumerate() {
for (i, segment) in self.segments(db).iter().enumerate() {
if i > 0 && matches!(segment, PathSegment::Field(_)) {
write!(f, ".")?;
}
Expand Down
Loading
Loading