Skip to content

[parser] Improve type errors for boolean literals - #16408

Merged
fdncred merged 1 commit into
nushell:mainfrom
kaathewisegit:boolean-type-error
Aug 11, 2025
Merged

fdncred merged 1 commit into
nushell:mainfrom
kaathewisegit:boolean-type-error

Conversation

@kaathewisegit

@kaathewisegit kaathewisegit commented Aug 11, 2025 •

Copy link
Copy Markdown
Contributor

Currently, strings true and false are intercepted early in parsing. Previously, if they didn't match the expected shape, the parse error said "expected non-boolean value". This PR changes the message to say "expected ".

Because the parsing error requires a &'static str I had to add a to_str(&self) -> &'static str method on SyntaxShape. It's a bit crude for more complex shapes: it simply says keyword, list, table, and so on for them, without exposing the underlying structure.

Fixes #16406

Release notes summary

Type errors involving boolean literals are now more precise.

@github-actions github-actions Bot added the A:parser Issues related to parsing label Aug 11, 2025
Comment thread crates/nu-parser/src/parser.rs Resolved
@fdncred
fdncred merged commit 751ef6e into nushell:main Aug 11, 2025
@fdncred

fdncred commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.107.0 milestone Aug 11, 2025
@blindFS

blindFS commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

I still feel it strange to type check boolean values in that place while not doing the same for inf and nan.

@kaathewisegit

Copy link
Copy Markdown
Contributor Author

@blindFS they are checked, actually, but only for some variations. Which causes errors to diverge depending on capitalization:

~> def test [port: int] {}
~> test NaN
Error: nu::parser::type_mismatch

  × Type mismatch.
   ╭─[entry #8:1:6]
 1 │ test NaN
   ·      ─┬─
   ·       ╰── expected int, found float
   ╰────

~> test nan
Error: nu::parser::parse_mismatch

  × Parse mismatch during operation.
   ╭─[entry #9:1:6]
 1 │ test nan
   ·      ─┬─
   ·       ╰── expected int
   ╰────

b"-inf" | b"inf" | b"NaN" => {
return parse_float(working_set, span);
}

NaN returns a float which is caught later on in parse_internal_call:

let arg = if !type_compatible(&positional.shape.to_type(), &arg.ty) {
working_set.error(ParseError::TypeMismatch(
positional.shape.to_type(),
arg.ty,
arg.span,
));
Expression::garbage(working_set, arg.span)

But nan passes through and gets delegated to parse_int in case of this function.

Oh, and I think I found an error

@blindFS

blindFS commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

@kaathewisegit Thanks for doing the experiments
The nan/NaN diff is another issue of course.

My main confusion is why we don't put the reserved keyword values check into the SyntaxShape::Any case?

@WindSoilder WindSoilder added the notes:fixes Noted in "Bug fixes" section label Aug 22, 2025
@132ikl 132ikl added the notes:ready Indicates Ready for Release notes label Aug 31, 2025
132ikl pushed a commit that referenced this pull request Oct 16, 2025
…6415)

A PoC PR regarding the topic discussed in #16408

# Description

Better readability in my opinion.

# User-Facing Changes

Fixes the inconsistent parsing of `inf`/`INF`, `NaN`/`nan`

# Tests + Formatting

# After Submitting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confusing type error for boolean values

5 participants