diff --git a/compiler/lookups.nim b/compiler/lookups.nim index 63e4920f7a..a2f3d53a05 100644 --- a/compiler/lookups.nim +++ b/compiler/lookups.nim @@ -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: diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 2956ab4b65..d4d197534d 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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): diff --git a/tests/errmsgs/t18886.nim b/tests/errmsgs/t18886.nim new file mode 100644 index 0000000000..cad62d4316 --- /dev/null +++ b/tests/errmsgs/t18886.nim @@ -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) \ No newline at end of file