fix #18886 crash on ambiguous proc cast (#20472)

* fix #18886 crash on ambiguous proc cast

* follow suggestion
This commit is contained in:
Bung
2022-10-10 17:09:54 +08:00
committed by GitHub
parent 0f2775aacf
commit db3d2971cf
3 changed files with 29 additions and 2 deletions

View File

@@ -488,17 +488,24 @@ proc errorUseQualifier*(c: PContext; info: TLineInfo; s: PSym) =
var amb: bool
discard errorUseQualifier(c, info, s, amb)
proc errorUseQualifier(c: PContext; info: TLineInfo; candidates: seq[PSym]) =
proc errorUseQualifier(c: PContext; info: TLineInfo; candidates: seq[PSym]; prefix = "use one of") =
var err = "ambiguous identifier: '" & candidates[0].name.s & "'"
var i = 0
for candidate in candidates:
if i == 0: err.add " -- use one of the following:\n"
if i == 0: err.add " -- $1 the following:\n" % prefix
else: err.add "\n"
err.add " " & candidate.owner.name.s & "." & candidate.name.s
err.add ": " & typeToString(candidate.typ)
inc i
localError(c.config, info, errGenerated, err)
proc errorUseQualifier*(c: PContext; info:TLineInfo; choices: PNode) =
var candidates = newSeq[PSym](choices.len)
let prefix = if choices[0].typ.kind != tyProc: "use one of" else: "you need a helper proc to disambiguate"
for i, n in choices:
candidates[i] = n.sym
errorUseQualifier(c, info, candidates, prefix)
proc errorUndeclaredIdentifier*(c: PContext; info: TLineInfo; name: string, extra = "") =
var err = "undeclared identifier: '" & name & "'" & extra
if c.recursiveDep.len > 0:

View File

@@ -371,6 +371,8 @@ proc semCast(c: PContext, n: PNode): PNode =
checkSonsLen(n, 2, c.config)
let targetType = semTypeNode(c, n[0], nil)
let castedExpr = semExprWithType(c, n[1])
if castedExpr.kind == nkClosedSymChoice:
errorUseQualifier(c, n[1].info, castedExpr)
if tfHasMeta in targetType.flags:
localError(c.config, n[0].info, "cannot cast to a non concrete type: '$1'" % $targetType)
if not isCastable(c, targetType, castedExpr.typ, n.info):

18
tests/errmsgs/t18886.nim Normal file
View File

@@ -0,0 +1,18 @@
discard """
cmd: "nim check --hints:off $file"
errormsg: ""
nimout: '''
t18886.nim(18, 24) Error: ambiguous identifier: 'bar' -- you need a helper proc to disambiguate the following:
t18886.bar: proc (i: ptr int){.noSideEffect, gcsafe, locks: 0.}
t18886.bar: proc (i: ptr char){.noSideEffect, gcsafe, locks: 0.}
'''
"""
type Foo = (proc(_: pointer): void)
proc bar(i: ptr[int]) = discard
proc bar(i: ptr[char]) = discard
let fooBar = cast[Foo](bar)