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

implements https://github.com/nim-lang/RFCs/issues/380; WIP #20520

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ proc initCandidateSymbols(c: PContext, headSymbol: PNode,
result[0].scope, diagnostics)
best.state = csNoMatch

proc scopeExtension(c: PContext; n: PNode): PSym =
# This is a particularly elegant way to implement
# https://github.com/nim-lang/RFCs/issues/380.
# If there is at least one argument `a` passed to `f` we
# use the module where `a`'s type comes from in addition to
# other candidates.
if n.len <= 1: return nil
var arg = n[1]
if arg.kind == nkExprEqExpr: arg = arg[1]
if arg.kind in {nkStmtList, nkStmtListExpr, nkDo, nkElse, nkOfBranch, nkElifBranch, nkExceptBranch}:
return nil

if arg.typ.isNil:
arg = c.semOperand(c, arg, {efDetermineType, efAllowStmt})
if n[1].kind == nkExprEqExpr:
n[1][1] = arg
else:
n[1] = arg
var t = arg.typ
result = nil
if t != nil:
t = t.skipTypes({tyVar, tyAlias, tySink, tyTypeDesc, tyGenericInst, tyGenericBody, tyStatic})
if t.kind == tyGenericInvocation:
t = t[0]
if t.kind in {tyEnum, tyObject, tyDistinct} and t.owner != nil and
t.owner.kind == skModule and t.owner != c.module:
result = t.owner

proc pickBestCandidate(c: PContext, headSymbol: PNode,
n, orig: PNode,
initialBinding: PNode,
Expand All @@ -65,6 +93,14 @@ proc pickBestCandidate(c: PContext, headSymbol: PNode,
errorsEnabled: bool, flags: TExprFlags) =
var o: TOverloadIter
var sym = initOverloadIter(o, c, headSymbol)

if sym != nil:
if sym.typ != nil and sym.typ.len > 1 and (
sym.typ[1].kind == tyUntyped or (sym.typ[1].kind == tyVarargs and sym.typ[1][0].kind == tyUntyped)):
discard
else:
discard scopeExtension(c, n)

var scope = o.lastOverloadScope
# Thanks to the lazy semchecking for operands, we need to check whether
# 'initCandidate' modifies the symbol table (via semExpr).
Expand Down
3 changes: 2 additions & 1 deletion compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2868,7 +2868,8 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType
s = f.sym
break
if s == nil:
let checks = if efNoEvaluateGeneric in flags:
let checks =
if efNoEvaluateGeneric in flags:
{checkUndeclared, checkPureEnumFields}
elif efInCall in flags:
{checkUndeclared, checkModule, checkPureEnumFields}
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/asyncdispatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
## asynchronous API, to disable the latter.

import os, tables, strutils, times, heapqueue, options, asyncstreams
import options, math, std/monotimes
import math, std/monotimes
import asyncfutures except callSoon

import nativesockets, net, deques
Expand Down