Skip to content

Commit

Permalink
Minor bits and pieces (#5066)
Browse files Browse the repository at this point in the history
* Use std deprecation for int rather than a hack in the parser

Signed-off-by: Nick Cameron <[email protected]>

* Don't allow an epsilon when converting floats to ints for property access

Signed-off-by: Nick Cameron <[email protected]>

* Fixup tests

Signed-off-by: Nick Cameron <[email protected]>

---------

Signed-off-by: Nick Cameron <[email protected]>
  • Loading branch information
nrc authored Jan 16, 2025
1 parent 38513a1 commit c147a21
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 37 deletions.
1 change: 0 additions & 1 deletion docs/kcl/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ layout: manual
* [`hollow`](kcl/hollow)
* [`import`](kcl/import)
* [`inch`](kcl/inch)
* [`int`](kcl/int)
* [`lastSegX`](kcl/lastSegX)
* [`lastSegY`](kcl/lastSegY)
* [`legAngX`](kcl/legAngX)
Expand Down
2 changes: 2 additions & 0 deletions docs/kcl/int.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ excerpt: "Convert a number to an integer."
layout: manual
---

**WARNING:** This function is deprecated.

Convert a number to an integer.

DEPRECATED use floor(), ceil(), or round().
Expand Down
2 changes: 1 addition & 1 deletion docs/kcl/std.json
Original file line number Diff line number Diff line change
Expand Up @@ -87204,7 +87204,7 @@
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"deprecated": true,
"examples": [
"n = int(ceil(5 / 2))\nassertEqual(n, 3, 0.0001, \"5/2 = 2.5, rounded up makes 3\")\n// Draw n cylinders.\nstartSketchOn('XZ')\n |> circle({ center = [0, 0], radius = 2 }, %)\n |> extrude(5, %)\n |> patternTransform(n, fn(id) {\n return { translate = [4 * id, 0, 0] }\n }, %)"
]
Expand Down
10 changes: 4 additions & 6 deletions src/wasm-lib/kcl/src/execution/exec_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ use crate::{

use super::cad_op::{OpArg, Operation};

const FLOAT_TO_INT_MAX_DELTA: f64 = 0.01;

impl BinaryPart {
#[async_recursion]
pub async fn get_result(&self, exec_state: &mut ExecState, ctx: &ExecutorContext) -> Result<KclValue, KclError> {
Expand Down Expand Up @@ -974,10 +972,9 @@ fn jvalue_to_prop(value: &KclValue, property_sr: Vec<SourceRange>, name: &str) -
if num < 0.0 {
return make_err(format!("'{num}' is negative, so you can't index an array with it"))
}
let nearest_int = num.round();
let delta = num-nearest_int;
if delta < FLOAT_TO_INT_MAX_DELTA {
Ok(Property::UInt(nearest_int as usize))
let nearest_int = crate::try_f64_to_usize(num);
if let Some(nearest_int) = nearest_int {
Ok(Property::UInt(nearest_int))
} else {
make_err(format!("'{num}' is not an integer, so you can't index an array with it"))
}
Expand All @@ -988,6 +985,7 @@ fn jvalue_to_prop(value: &KclValue, property_sr: Vec<SourceRange>, name: &str) -
}
}
}

impl Property {
fn type_name(&self) -> &'static str {
match self {
Expand Down
29 changes: 0 additions & 29 deletions src/wasm-lib/kcl/src/parsing/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use crate::{
token::{Token, TokenSlice, TokenType},
PIPE_OPERATOR, PIPE_SUBSTITUTION_OPERATOR,
},
unparser::ExprContext,
SourceRange,
};

Expand Down Expand Up @@ -2611,23 +2610,6 @@ fn fn_call(i: &mut TokenSlice) -> PResult<Node<CallExpression>> {
}
let end = preceded(opt(whitespace), close_paren).parse_next(i)?.end;

// This should really be done with resolved names, but we don't have warning support there
// so we'll hack this in here.
if fn_name.name == "int" {
assert_eq!(args.len(), 1);
let mut arg_str = args[0].recast(&crate::FormatOptions::default(), 0, ExprContext::Other);
if arg_str.contains('.') && !arg_str.ends_with(".0") {
arg_str = format!("round({arg_str})");
}
ParseContext::warn(CompilationError::with_suggestion(
SourceRange::new(fn_name.start, end, fn_name.module_id),
None,
"`int` function is deprecated. You may not need it at all. If you need to round, consider `round`, `ceil`, or `floor`.",
Some(("Remove call to `int`", arg_str)),
Tag::Deprecated,
));
}

Ok(Node {
start: fn_name.start,
end,
Expand Down Expand Up @@ -4346,17 +4328,6 @@ sketch001 = startSketchOn('XZ') |> startProfileAt([90.45, 119.09, %)"#;
assert_eq!(errs[0].apply_suggestion(some_program_string).unwrap(), "{ foo = bar }")
}

#[test]
fn warn_fn_int() {
let some_program_string = r#"int(1.0)
int(42.3)"#;
let (_, errs) = assert_no_err(some_program_string);
assert_eq!(errs.len(), 2);
let replaced = errs[1].apply_suggestion(some_program_string).unwrap();
let replaced = errs[0].apply_suggestion(&replaced).unwrap();
assert_eq!(replaced, "1.0\nround(42.3)");
}

#[test]
fn warn_fn_decl() {
let some_program_string = r#"fn foo = () => {
Expand Down
1 change: 1 addition & 0 deletions src/wasm-lib/kcl/src/std/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub async fn int(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
#[stdlib {
name = "int",
tags = ["convert"],
deprecated = true,
}]
fn inner_int(num: f64) -> Result<f64, KclError> {
Ok(num)
Expand Down

0 comments on commit c147a21

Please sign in to comment.