From c147a219f4b907c8bce03b723aaeee40a60e08fe Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Thu, 16 Jan 2025 14:00:32 +1300 Subject: [PATCH] Minor bits and pieces (#5066) * Use std deprecation for int rather than a hack in the parser Signed-off-by: Nick Cameron * Don't allow an epsilon when converting floats to ints for property access Signed-off-by: Nick Cameron * Fixup tests Signed-off-by: Nick Cameron --------- Signed-off-by: Nick Cameron --- docs/kcl/index.md | 1 - docs/kcl/int.md | 2 ++ docs/kcl/std.json | 2 +- src/wasm-lib/kcl/src/execution/exec_ast.rs | 10 +++----- src/wasm-lib/kcl/src/parsing/parser.rs | 29 ---------------------- src/wasm-lib/kcl/src/std/convert.rs | 1 + 6 files changed, 8 insertions(+), 37 deletions(-) diff --git a/docs/kcl/index.md b/docs/kcl/index.md index 21ee4d74c8..7c3bfafda3 100644 --- a/docs/kcl/index.md +++ b/docs/kcl/index.md @@ -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) diff --git a/docs/kcl/int.md b/docs/kcl/int.md index a2bbb37c65..f36abf66ad 100644 --- a/docs/kcl/int.md +++ b/docs/kcl/int.md @@ -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(). diff --git a/docs/kcl/std.json b/docs/kcl/std.json index ee33ee646c..9d0d8f4ea8 100644 --- a/docs/kcl/std.json +++ b/docs/kcl/std.json @@ -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 }, %)" ] diff --git a/src/wasm-lib/kcl/src/execution/exec_ast.rs b/src/wasm-lib/kcl/src/execution/exec_ast.rs index 71fc4dbff5..7cd187ee6b 100644 --- a/src/wasm-lib/kcl/src/execution/exec_ast.rs +++ b/src/wasm-lib/kcl/src/execution/exec_ast.rs @@ -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 { @@ -974,10 +972,9 @@ fn jvalue_to_prop(value: &KclValue, property_sr: Vec, 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")) } @@ -988,6 +985,7 @@ fn jvalue_to_prop(value: &KclValue, property_sr: Vec, name: &str) - } } } + impl Property { fn type_name(&self) -> &'static str { match self { diff --git a/src/wasm-lib/kcl/src/parsing/parser.rs b/src/wasm-lib/kcl/src/parsing/parser.rs index f98a32907c..b858289adf 100644 --- a/src/wasm-lib/kcl/src/parsing/parser.rs +++ b/src/wasm-lib/kcl/src/parsing/parser.rs @@ -30,7 +30,6 @@ use crate::{ token::{Token, TokenSlice, TokenType}, PIPE_OPERATOR, PIPE_SUBSTITUTION_OPERATOR, }, - unparser::ExprContext, SourceRange, }; @@ -2611,23 +2610,6 @@ fn fn_call(i: &mut TokenSlice) -> PResult> { } 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, @@ -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 = () => { diff --git a/src/wasm-lib/kcl/src/std/convert.rs b/src/wasm-lib/kcl/src/std/convert.rs index 22c851f910..6a7bf5a606 100644 --- a/src/wasm-lib/kcl/src/std/convert.rs +++ b/src/wasm-lib/kcl/src/std/convert.rs @@ -34,6 +34,7 @@ pub async fn int(_exec_state: &mut ExecState, args: Args) -> Result Result { Ok(num)