Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Const values don't short circuit. #19773

Open
treeform opened this issue May 6, 2022 · 2 comments
Open

Const values don't short circuit. #19773

treeform opened this issue May 6, 2022 · 2 comments

Comments

@treeform
Copy link
Contributor

treeform commented May 6, 2022

If you have a const string it will not short-circuit and will not compile:

Example

const s1 = ""
if s1.len == 0 or s1[0] == '/':
  echo "root"

Current Output

Error: index out of bounds, the container is empty""[0]

Expected Output

root

I would expect it to work similar to let or var.

let s2 = ""
if s2.len == 0 or s2[0] == '/':
  echo "root"

var s3 = ""
if s3.len == 0 or s3[0] == '/':
  echo "root"

output:

root
root

That works fine.

The const make it work more like some sort of substitution:

if "".len == 0 or ""[0] == '/':
  echo "root"

output:

Error: index out of bounds, the container is emptys1[0]

Same thing happens with static values:

proc foo(s2: static string) =
  if s2.len == 0 or s2[0] == '/':
    echo "root"
foo ""

output:

Error: index out of bounds, the container is emptys1[0]
$ nim -v
Nim Compiler Version 1.7.1 [Linux: amd64]
Compiled at 2022-05-05
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 278ecad973c6581aeea0a6ff9372109b0dd6df5e
active boot switches: -d:release

Related issue: related #19492

@ghost
Copy link

ghost commented May 6, 2022

See #14631 and #13541 as well

@ringabout ringabout removed their assignment Aug 6, 2022
@ringabout
Copy link
Member

It seems very hard to make semcheck short circuir, I have no idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants