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

templates/macros use no expected types when return types are specified #24298

Merged
merged 3 commits into from
Oct 13, 2024

Conversation

ringabout
Copy link
Member

@ringabout ringabout commented Oct 13, 2024

fixes #24296
fixes #24295

Templates use expectedType for type inference. It's justified that when templates don't have an actual return type, i.e., untyped etc.

When the return type of templates is specified, we should not infer the type

template g(): string = ""

let c: cstring = g()

In this example, it is not reasonable to annotate the templates expression with the cstring type before the fitNode check with its specified return type.

@ringabout
Copy link
Member Author

ringabout commented Oct 13, 2024

It seems to break templates that present int literals:

Before my PR, the type inference seemed to treat int literals differently:

block: # bug #24295
  template g(_: int): int = 12
  proc foo(x: uint, y: uint) =
    echo (x, y)

  let c: uint = 0.g() # type mismatch: got 'uint' for '12' but expected 'int'
  foo(12, g(0)) # compiles

block:
  proc foe(): int = 12
  proc foo(x: uint, y: uint) =
    echo (x, y)
  let c: uint = foe() # type mismatch: got 'int' for 'foe()' but expected 'uint'
  foo(12, foe()) # type mismatch: got 'int' for 'foe()' but expected 'uint'

@ringabout
Copy link
Member Author

It seems that in old Nim versions, templates just inline expressions without annotating the int literals with types

@ringabout ringabout changed the title templates/macros use return types as expected types templates/macros use no expected types when return types are specified Oct 13, 2024
@Araq Araq merged commit 80e6b35 into devel Oct 13, 2024
22 checks passed
@Araq Araq deleted the pr_type_inference branch October 13, 2024 17:54
Copy link
Contributor

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from 80e6b35

Hint: mm: orc; opt: speed; options: -d:release
175125 lines; 8.410s; 655.227MiB peakmem

narimiran pushed a commit that referenced this pull request Oct 23, 2024
#24298)

fixes #24296
fixes #24295

Templates use `expectedType` for type inference. It's justified that
when templates don't have an actual return type, i.e., `untyped` etc.

When the return type of templates is specified, we should not infer the
type

```nim
template g(): string = ""

let c: cstring = g()
```
In this example, it is not reasonable to annotate the templates
expression with the `cstring` type before the `fitNode` check with its
specified return type.

(cherry picked from commit 80e6b35)
narimiran pushed a commit that referenced this pull request Jan 14, 2025
#24298)

fixes #24296
fixes #24295

Templates use `expectedType` for type inference. It's justified that
when templates don't have an actual return type, i.e., `untyped` etc.

When the return type of templates is specified, we should not infer the
type

```nim
template g(): string = ""

let c: cstring = g()
```
In this example, it is not reasonable to annotate the templates
expression with the `cstring` type before the `fitNode` check with its
specified return type.

(cherry picked from commit 80e6b35)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment