diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs index 3e3e90a72fe..ef55b0115f3 100644 --- a/src/Compiler/Checking/CheckExpressions.fs +++ b/src/Compiler/Checking/CheckExpressions.fs @@ -9406,6 +9406,16 @@ and TcLookupItemThen cenv overallTy env tpenv mObjExpr objExpr objExprTy delayed TcTraitItemThen cenv overallTy env (Some objExpr) traitInfo tpenv mItem delayed | Item.DelegateCtor _ -> error (Error (FSComp.SR.tcConstructorsCannotBeFirstClassValues(), mItem)) + + | Item.UnionCase(info, _) -> + let clashingName = + info.TyconRef.MembersOfFSharpTyconSorted |> List.tryFind (fun mem -> mem.DisplayNameCore = info.DisplayNameCore) + + match clashingName with + | None -> error (Error (FSComp.SR.tcSyntaxFormUsedOnlyWithRecordLabelsPropertiesAndFields(), mItem)) + | Some value -> + let kind = (if value.IsMember then "member" else "value") + error(NameClash(info.DisplayNameCore, kind, info.DisplayNameCore, mItem, FSComp.SR.typeInfoUnionCase(), info.DisplayNameCore, mItem)) // These items are not expected here - they can't be the result of a instance member dot-lookup "expr.Ident" | Item.ActivePatternResult _ @@ -9416,7 +9426,6 @@ and TcLookupItemThen cenv overallTy env tpenv mObjExpr objExpr objExprTy delayed | Item.ModuleOrNamespaces _ | Item.TypeVar _ | Item.Types _ - | Item.UnionCase _ | Item.UnionCaseField _ | Item.UnqualifiedType _ | Item.Value _ diff --git a/tests/FSharp.Compiler.ComponentTests/Language/DotLambdaTests.fs b/tests/FSharp.Compiler.ComponentTests/Language/DotLambdaTests.fs index 6dff28ab6a7..356189a4006 100644 --- a/tests/FSharp.Compiler.ComponentTests/Language/DotLambdaTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Language/DotLambdaTests.fs @@ -290,4 +290,31 @@ let _ = asQ.Select _.Length """ |> withLangVersion80 |> typecheck - |> shouldSucceed \ No newline at end of file + |> shouldSucceed + +[] + let ``Error when property has same name as DU case`` () = + Fsx """ +type MyId = + | IdA of int + | IdB of string + + member this.IdA = + match this with + | IdA x -> Some x + | _ -> None + + member this.IdX = + match this with + | IdB x -> Some x + | _ -> None + +let onlyIdX (ids: MyId list) = ids |> List.choose _.IdX +let onlyIdA (ids: MyId list) = ids |> List.choose _.IdA + """ + |> ignoreWarnings + |> compile + |> shouldFail + |> withDiagnostics [ + (Error 23, Line 17, Col 51, Line 17, Col 56, "The member 'IdA' can not be defined because the name 'IdA' clashes with the union case 'IdA' in this type or module") + ] \ No newline at end of file