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

Typechecking can be bypassed for descendant of generics when a typedesc parameter or auto is used #18846

Open
alaviss opened this issue Sep 13, 2021 · 1 comment
Labels
Generics Inheritance Invalid Code Acceptance Everything related to compiler not complaining about invalid code Severe

Comments

@alaviss
Copy link
Collaborator

alaviss commented Sep 13, 2021

Example

type
  Either[T, U] = object of RootObj
    t: T
    u: U

  E = object of Either[float, string]

proc test(T: typedesc, e: Either[T, T]) =
  echo "matched ", typeof(e)
  echo e

proc test1[T](e: Either[T, auto]) =
  echo "matched ", typeof(e)
  echo e

proc test2[T](e: Either[T, T]) =
  echo "matched ", typeof(e)
  echo e

proc makeE(): E =
  result.t = -1.0
  result.u = "string"

test(int, makeE())
test1[int](makeE())

when false:
  discard "This doesn't compile as expected"
  test2[int](makeE())

Current Output

matched Either[system.int, system.int]
(t: -4616189618054758400, u: 140610450096208)
matched Either[system.int, system.string]
(t: -4616189618054758400, u: "string")

Expected Output

This should not compile at all

Additional Information

$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-09-12
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 5d1608c9764416e74b36f6a772f16a66d58ddc28
active boot switches: -d:release -d:nimUseLinenoise
@alaviss alaviss added Severe Generics Inheritance Invalid Code Acceptance Everything related to compiler not complaining about invalid code labels Sep 13, 2021
@bung87
Copy link
Collaborator

bung87 commented Jul 29, 2023

changes E = object of Either[float, string] to E = Either[float, string] will be CT error.

bung87 added a commit to bung87/Nim that referenced this issue Jul 29, 2023
…erics when a typedesc parameter or auto is used
bung87 added a commit to bung87/Nim that referenced this issue Jul 29, 2023
…erics when a typedesc parameter or auto is used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Generics Inheritance Invalid Code Acceptance Everything related to compiler not complaining about invalid code Severe
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants