Minor bits and pieces (#5066)
* Use std deprecation for int rather than a hack in the parser Signed-off-by: Nick Cameron <nrc@ncameron.org> * Don't allow an epsilon when converting floats to ints for property access Signed-off-by: Nick Cameron <nrc@ncameron.org> * Fixup tests Signed-off-by: Nick Cameron <nrc@ncameron.org> --------- Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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().
|
||||
|
||||
@ -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 }, %)"
|
||||
]
|
||||
|
||||
@ -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> {
|
||||
@ -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"))
|
||||
}
|
||||
@ -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 {
|
||||
|
||||
@ -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<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,
|
||||
@ -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 = () => {
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user