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
99 changes: 68 additions & 31 deletions crates/forma-lang/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,45 @@ fn unknown_name_hint(name: &str, known: &[&str]) -> String {
format!("{suggestion}; accepted: {}", known.join(", "))
}

fn is_reserved_constant(name: &str) -> bool {
matches!(name, "true" | "True" | "false" | "False" | "pi" | "PI")
}

fn is_bare_selector_kw(id: &str) -> bool {
matches!(
id,
"vertical"
| "horizontal"
| "top_rim"
| "bottom_rim"
| "outer_rim"
| "rim"
| "sharp"
| "boundary"
| "all"
| "top"
| "bottom"
| "front"
| "back"
| "left"
| "right"
| "sides"
| "side"
| "x"
| "y"
| "z"
| "min"
| "center"
| "mid"
| "max"
)
}

fn is_builtin_name(name: &str) -> bool {
CALL_NAMES.contains(&name)
|| PIPE_OP_NAMES.contains(&name)
|| matches!(
name,
"def" | "for" | "in" | "range" | "true" | "True" | "false" | "False" | "pi" | "PI"
)
|| matches!(name, "def" | "for" | "in" | "range")
|| is_reserved_constant(name)
}

fn allowed_call_kwargs(name: &str) -> Option<&'static [&'static str]> {
Expand Down Expand Up @@ -520,6 +552,7 @@ struct Parser<'a> {
scene: &'a mut Scene,
vars: HashMap<String, Val>,
macros: HashMap<String, MacroDef>,
last_stmt_bare_selector: Option<(usize, String, String)>,
}

impl<'a> Parser<'a> {
Expand Down Expand Up @@ -573,11 +606,23 @@ impl<'a> Parser<'a> {
self.bump();
}
}
if let Some((line, name, sel)) = self.last_stmt_bare_selector.take() {
let fallback = self.scene.last_part_name().unwrap_or("none").to_string();
if self.scene.strict_manifold {
return Err(format!(
"line {line}: '{name} = {sel}' evaluates to selector keyword '{sel}', not a part (no part '{name}' was created; export would fall back to '{fallback}')"
));
}
self.scene.push_warning(format!(
"line {line}: '{name} = {sel}' evaluates to selector keyword '{sel}', not a part; exporting '{fallback}'"
));
}
Ok(())
}

fn parse_stmt(&mut self) -> Result<(), String> {
let stmt_line = self.line();
self.last_stmt_bare_selector = None;
// Check `def name(p1, p2=default) = expr`
if matches!(self.peek(), Tok::Ident(s) if s == "def")
&& !matches!(self.toks.get(self.pos + 1).map(|t| &t.0), Some(Tok::Eq))
Expand All @@ -587,9 +632,26 @@ impl<'a> Parser<'a> {
// Check `ID = expr`
if let Tok::Ident(name) = self.peek().clone() {
if self.pos + 1 < self.toks.len() && matches!(self.toks[self.pos + 1].0, Tok::Eq) {
if is_reserved_constant(&name) {
return Err(format!(
"line {stmt_line}: cannot assign to built-in constant '{name}'"
));
}
self.bump(); // ident
self.bump(); // '='
let rhs_start = self.pos;
let val = self.parse_pipe()?;
let rhs_end = self.pos;
if rhs_end == rhs_start + 1 {
if let Tok::Ident(ref rhs_id) = self.toks[rhs_start].0 {
if is_bare_selector_kw(rhs_id)
&& matches!(&val, Val::Str(s) if s == rhs_id)
{
self.last_stmt_bare_selector =
Some((stmt_line, name.clone(), rhs_id.clone()));
}
}
}
let stored_val = match val {
Val::Mesh(m) => {
if !name.starts_with('_') {
Expand Down Expand Up @@ -879,33 +941,7 @@ impl<'a> Parser<'a> {
Ok(v.clone())
} else if let Ok(m) = self.scene.get_part(&id) {
Ok(Val::Mesh((**m).clone()))
} else if matches!(
id.as_str(),
"vertical"
| "horizontal"
| "top_rim"
| "bottom_rim"
| "outer_rim"
| "rim"
| "sharp"
| "boundary"
| "all"
| "top"
| "bottom"
| "front"
| "back"
| "left"
| "right"
| "sides"
| "side"
| "x"
| "y"
| "z"
| "min"
| "center"
| "mid"
| "max"
) {
} else if is_bare_selector_kw(id.as_str()) {
Ok(Val::Str(id))
} else {
let mut candidates: Vec<String> = self.vars.keys().cloned().collect();
Expand Down Expand Up @@ -2055,6 +2091,7 @@ pub fn eval_pipe(scene: &mut Scene, src: &str) -> Result<(), String> {
scene,
vars,
macros: HashMap::new(),
last_stmt_bare_selector: None,
};
p.run()
}
54 changes: 54 additions & 0 deletions crates/forma-lang/tests/audit_regressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,60 @@ fn remove_part_keeps_last_modified_on_remaining_part_and_step_triangulates_nonpl
assert!(step.contains("CLOSED_SHELL"));
}

#[test]
fn reserved_constant_assignment_rejected_and_bare_selector_last_assignment_diagnosed() {
// 1. Assigning to `pi` / `PI` / `true` / `false` must fail cleanly instead of creating an unreferenceable part
for name in ["pi", "PI", "true", "false"] {
let mut s = Scene::new();
s.strict_manifold = true;
let err = eval_script(&mut s, Dialect::Pipe, &format!("{name} = box(2,2,1)")).unwrap_err();
assert!(
err.contains("cannot assign to built-in constant"),
"expected built-in constant error for '{name}', got: {err}"
);
}

// 2. Final assignment `result = all` under strict mode fails loudly instead of exporting `knob`
let mut s_strict = Scene::new();
s_strict.strict_manifold = true;
let err = eval_script(
&mut s_strict,
Dialect::Pipe,
"body = box(2,2,1)\nknob = sphere(0.3) | move(0,0,1)\nresult = all",
)
.unwrap_err();
assert!(
err.contains("selector keyword 'all'") && err.contains("knob"),
"expected selector assignment error, got: {err}"
);

// 3. Under non-strict mode, final bare selector assignment warns clearly
let mut s_lax = Scene::new();
s_lax.strict_manifold = false;
let obs = eval_script(
&mut s_lax,
Dialect::Pipe,
"body = box(2,2,1)\nknob = sphere(0.3) | move(0,0,1)\nresult = all",
)
.unwrap();
assert!(
obs.warnings.iter().any(|w| w.contains("selector keyword 'all'")),
"expected selector warning, got: {:?}",
obs.warnings
);

// 4. Intermediate bare selector variable followed by real mesh assignment still works cleanly
let mut s_ok = Scene::new();
s_ok.strict_manifold = true;
let obs_ok = eval_script(
&mut s_ok,
Dialect::Pipe,
"sel = vertical\np = box(2,2,1) | bevel(sel, 0.2)",
)
.unwrap();
assert!(obs_ok.ok);
}

#[test]
fn issue_80_subsurfed_loft_add_cone_left_and_right_valid_solids() {
use forma_core::boolean::has_self_intersections;
Expand Down
Loading