47 Commits

Author SHA1 Message Date
ringabout
294b1566e7 fixes #23952; Size/Signedness issues with unordered enums (#24356)
fixes #23952

It reorders `type Foo = enum A, B = -1` to `type Foo = enum B = -1, A`
so that `firstOrd` etc. continue to work.
2024-10-25 23:03:17 +08:00
metagn
f223f016f3 show symchoices as ambiguous in overload type mismatches (#24077)
fixes #23397

All ambiguous symbols generate symchoices for call arguments since
#23123. So, if a type mismatch receives a symchoice node for an
argument, we now treat it as an ambiguous identifier and list the
ambiguous symbols in the error message.
2024-09-09 09:50:45 +02:00
metagn
2311049b27 don't require symbol with enum type to be constant in fitNode (#23999)
fixes #23998

In `fitNode` the first symbol of a symchoice that expects an enum type
with the same enum type is given as the result of the `fitNode`. But
`getConstExpr` is also called on it, which will return a `nil` node for
nodes that aren't constant but have the enum type, like variables or
proc parameters. Instead we just return the node directly since it's
already typed.

Normally, this `if` branch in `fitNode` shouldn't exist since
`paramTypesMatch` handles it, but the way pure enum symbols work makes
it really impractical to check their ambiguity, which `paramTypesMatch`
won't like. If it causes problems for regular enums we can restrict this
branch to just pure enums until they are hopefully eventually removed.
2024-08-22 07:19:43 +02:00
metagn
a354b18fe1 always lookup pure enum symbols if expected type is enum (#23976)
fixes #23689

Normally pure enum symbols only "exist" in lookup if nothing else with
the same name is in scope. But if an expression is expected to be an
enum type, we know that ambiguity can be resolved between different
symbols based on their type, so we can include the normally inaccessible
pure enum fields in the ambiguity resolution in the case that the
expected enum type is actually a pure enum. This handles the use case in
the issue of the type inference for enums reverted in #23588.

I know pure enums are supposed to be on their way out so this might seem
excessive, but the `pure` pragma can't be removed in the code in the
issue due to a redefinition error, they have to be separated into
different modules. Normal enums can still resolve the ambiguity here
though. I always think about making a list of all the remaining use
cases for pure enums and I always forget.

Will close #23694 if CI passes
2024-08-17 16:50:48 +02:00
ringabout
c91b33aaba re-enable tests (#23591) 2024-05-10 16:17:58 +02:00
ringabout
42486e1b2f unordered enum for better interoperability with C (#23585)
ref https://forum.nim-lang.org/t/11564
```nim
block: # unordered enum
  block:
    type
      unordered_enum = enum
        a = 1
        b = 0

    doAssert (ord(a), ord(b)) == (1, 0)

  block:
    type
      unordered_enum = enum
        a = 1
        b = 0
        c

    doAssert (ord(a), ord(b), ord(c)) == (1, 0, 2)

  block:
    type
      unordered_enum = enum
        a = 100
        b
        c = 50
        d

    doAssert (ord(a), ord(b), ord(c), ord(d)) == (100, 101, 50, 51)

  block:
    type
      unordered_enum = enum
        a = 7
        b = 6
        c = 5
        d

    doAssert (ord(a), ord(b), ord(c), ord(d)) == (7, 6, 5, 8)
```
2024-05-10 10:32:07 +02:00
metagn
b280100499 ambiguous identifier resolution (#23123)
fixes #23002, fixes #22841, refs comments in #23097

When an identifier is ambiguous in scope (i.e. multiple imports contain
symbols with the same name), attempt resolving it through type inference
(by creating a symchoice). To do this efficiently, `qualifiedLookUp` had
to be broken up so that `semExpr` can access the ambiguous candidates
directly (now obtained directly via `lookUpCandidates`).

This fixes the linked issues, but an example like:

```nim
let on = 123

{.warning[ProveInit]: on.}
```

will still fail, since `on` is unambiguously the local `let` symbol here
(this is also true for `proc on` but `proc` symbols generate symchoices
anyway).

Type symbols are not considered to not confuse the type inference. This
includes the change in sigmatch, up to this point symchoices with
nonoverloadable symbols could be created, they just wouldn't be
considered during disambiguation. Now every proper symbol except types
are considered in disambiguation, so the correct symbols must be picked
during the creation of the symchoice node. I remember there being a
violating case of this in the compiler, but this was very likely fixed
by excluding type symbols as CI seems to have found no issues.

The pure enum ambiguity test was disabled because ambiguous pure enums
now behave like overloadable enums with this behavior, so we get a
longer error message for `echo amb` like `type mismatch: got <MyEnum |
OtherEnum> but expected T`
2024-01-01 12:21:19 +01:00
Jake Leahy
8cad6ac048 Don't try and get enum value if its invalid (#22997)
Currently running `nimsuggest`/`check` on this code causes the compiler
to raise an exception

```nim
type
  Test = enum
    A = 9.0 
```

```
assertions.nim(34)       raiseAssert
Error: unhandled exception: int128.nim(69, 11) `arg.sdata(3) == 0` out of range [AssertionDefect]
```

Issue was the compiler still trying to get the ordinal value even if it
wasn't an ordinal
2023-11-28 09:38:10 +01:00
metagn
5f9038a5d7 make expressions opt in to symchoices (#22716)
refs #22605

Sym choice nodes are now only allowed to pass through semchecking if
contexts ask for them to (with `efAllowSymChoice`). Otherwise they are
resolved or treated as ambiguous. The contexts that can receive
symchoices in this PR are:

* Call operands and addresses and emulations of such, which will subject
them to overload resolution which will resolve them or fail.
* Type conversion operands only for routine symchoices for type
disambiguation syntax (like `(proc (x: int): int)(foo)`), which will
resolve them or fail.
* Proc parameter default values both at the declaration and during
generic instantiation, which undergo type narrowing and so will resolve
them or fail.

This means unless these contexts mess up sym choice nodes should never
leave the semchecking stage. This serves as a blueprint for future
improvements to intermediate symbol resolution.

Some tangential changes are also in this PR:

1. The `AmbiguousEnum` hint is removed, it was always disabled by
default and since #22606 it only started getting emitted after the
symchoice was soundly resolved.
2. Proc setter syntax (`a.b = c` becoming `` `b=`(a, c) ``) used to
fully type check the RHS before passing the transformed call node to
proc overloading. Now it just passes the original node directly so proc
overloading can deal with its typechecking.
2023-09-18 06:39:22 +02:00
metagn
480e98c479 resolve unambiguous enum symchoices from local scope, error on rest (#22606)
fixes #22598, properly fixes #21887 and fixes test case issue number

When an enum field sym choice has to choose a type, check if its name is
ambiguous in the local scope, then check if the first symbol found in
the local scope is the first symbol in the sym choice. If so, choose
that symbol. Otherwise, give an ambiguous identifier error.

The dependence on the local scope implies this will always give
ambiguity errors for unpicked enum symchoices from generics and
templates and macros from other scopes. We can change `not
isAmbiguous(...) and foundSym == first` to `not (isAmbiguous(...) and
foundSym == first)` to make it so they never give ambiguity errors, and
always pick the first symbol in the symchoice. I can do this if this is
preferred, but no code from CI seems affected.
2023-09-03 13:59:03 +02:00
ringabout
ab4d044a81 fixes #21887; Type conversion on overloaded enum field does not always call (#21908)
* fixes #21887; Type conversion on overloaded enum field does not always call

* remove comments

* add a test case

* restrict it to enums
2023-05-26 09:24:43 +02:00
ringabout
761b927e47 fixes #21863; Incorrect enum field access can cause internal error (#21886)
fixes 21863; Incorrect enum field access can cause internal error
2023-05-24 07:43:30 +02:00
ringabout
8cf5643621 fixes #21280; Enum with int64.high() value crashes compiler (#21285)
* fixes #21280; Enum with int64.high() value crashes compiler

* Update tests/enum/tenum.nim

* Update tests/enum/tenum.nim

* fixes tests

* Update tests/enum/tenum.nim

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-05-06 21:38:17 +02:00
ringabout
51f410e1d5 megatest now checks refc too (#21341)
* megatest now checks refc too

* fixes refc
2023-02-09 16:14:39 -05:00
ringabout
81b7f9108f fixes #21207; reports redefinition error in the definition of enums (#21217)
* fixes #21207;  reports redefinition in the enums

* add a test
2023-01-03 08:15:10 +01:00
ringabout
93b085a57a closes #12589; add testcase (#20769)
* fixes #12589; add testcase

* fixes i386

* i386
2022-11-06 11:46:51 +01:00
metagn
cfff454cf9 closed ambiguous enum defaults to first overload (#20457)
* closed ambiguous enum defaults to first overload

* add warning

* turn to hint

* work around config
2022-10-01 13:30:23 +02:00
metagn
5ebd1248df overloadable enums no longer experimental (#20298)
depends on #20126
2022-09-05 23:38:38 +02:00
metagn
a6189fbb98 only allow enums to overload enums + extra test (#20126)
mirror behavior without overloadableEnums
2022-09-03 09:53:31 +02:00
flywind
1807de38e5 add testcase for #16462 (#19692) 2022-04-07 20:57:50 +02:00
Andreas Rumpf
6f15af41a7 fixes a regression caused by overloadable enums even though they're opt-in (#18970) 2021-10-07 15:07:24 +02:00
Andreas Rumpf
06ff0e9624 fixes #18769 (#18790) 2021-09-03 15:18:55 +02:00
flywind
f0c6593412 add testcase for overloadable_enums (#18722)
* add testcase for overloadable_enums

* link
2021-08-21 08:21:06 +02:00
Andreas Rumpf
a273ea70e8 implements overloadable enum values; WIP (#18470)
* implements overloadable enum values
* simpler code
2021-07-28 12:46:28 +02:00
Timothee Cour
ef121f3b99 followup #17876: remove annoying enum name clashes in tests/enum/tenum.nim (#18291) 2021-06-17 16:19:16 -07:00
Timothee Cour
840e13deb7 type with same name in different scope now works (#17710)
* type with same name in different scope now works
* fix tests/enum/tenum.nim which was wrong because it was affected by this bug
2021-04-14 08:50:15 +02:00
flywind
27eb19cc48 fix #15145 (#15816) 2020-11-02 08:56:51 +01:00
cooldome
f8cac6bbbc fix #15752 (#15754) 2020-10-28 00:09:26 +01:00
Jasper Jenkins
9474a818af fix enumtostr crash for enum-range (#13035) 2020-01-05 09:18:14 +01:00
Miran
e7878c0d08 add tests for recently closed issues (#10722) 2019-02-23 10:41:35 +01:00
Arne Döring
28394153ab 32 bit fixes (#10608) 2019-02-13 23:30:14 +01:00
Neelesh Chandola
c5ad4c10cb Deprecated pragma is now supported on enum fields (#10113)
* {.deprecated.} pragma is now supported for enum fields
* Add tests
* Simplify code
2018-12-30 09:43:59 +01:00
Arne Döring
1105d03644 require errormsg to be specified before file. 2018-12-11 21:23:21 +01:00
Timothee Cour
eb946f37a7 fixes #8671; show helpful msg (lookup symbol, eg iterator) on 'attempting to call undeclared routine' error (#8786) 2018-10-14 01:16:04 +02:00
Miran
3c9fcc4c30 Merge tests into a larger file (part 2 of ∞) (#9335)
* merge controlflow tests

* merge distinct tests

* merge enum tests

* merge fields tests

* merge implicit tests

* merge iter issues tests
2018-10-13 14:58:31 +02:00
Araq
2f7b979e38 fixes #8066 2018-08-31 00:30:19 +02:00
Jacek Sieka
72dfe176f5 remove dead code elimination option (#7669) 2018-04-23 11:02:38 +02:00
Silvio
51cd3bd86f Fixes #5062 (#5527); JS: holes in enums 2017-03-15 10:33:37 +01:00
Araq
700c024e13 fixes #5148 2016-12-29 19:32:43 +01:00
Yuriy Glukhov
f46d40cb37 Added a test case 2016-05-18 13:51:07 +03:00
Aman Gupta
2240fd3f3f add tfile/tline assertions for template expansion file/line 2015-10-06 15:47:07 -07:00
Adam Strzelecki
e80465dacf tests: Trim .nim files trailing whitespace
via OSX: find . -name '*.nim' -exec sed -i '' -E 's/[[:space:]]+$//' {} +
2015-09-04 23:04:32 +02:00
Dominik Picheta
ad6ad5d8d0 s/procedure/routine/ in tests. 2015-06-05 14:53:49 +01:00
Dominik Picheta
22f6017ab8 Fixes #2584
Better compiler errors for accessing undeclared fields, calling undeclared
procedures and procedure fields.
2015-06-04 13:55:48 +01:00
Araq
ee57bb3e3e fixes --gc:none regression; made some tests green 2015-03-10 12:32:47 +01:00
Dominik Picheta
53b8d6281d Fixes tenumitems test. 2014-08-16 01:15:36 +01:00
Araq
20b5f31c03 new tester; all tests categorized 2014-01-13 02:10:03 +01:00