Skip to content

Make attribute target mismatch a warning and not an error. #18492

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

Merged
merged 20 commits into from
May 14, 2025
Merged
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
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* Warning for "useless null handling" works with piped syntax constructs now ([PR #18331](https://github.com/dotnet/fsharp/pull/18331))
* Make indent in generated overridden member code depend on the context, not fix to 4. ([PR #18341](https://github.com/dotnet/fsharp/pull/18341))
* Adjust caller info attribute error message range ([PR #18388](https://github.com/dotnet/fsharp/pull/18388))
* Make attribute targets mismatch a warning and not an error ([PR #18492](https://github.com/dotnet/fsharp/pull/18492))

### Breaking Changes
* Struct unions with overlapping fields now generate mappings needed for reading via reflection ([Issue #18121](https://github.com/dotnet/fsharp/issues/17797), [PR #18274](https://github.com/dotnet/fsharp/pull/17877))
57 changes: 33 additions & 24 deletions src/Compiler/Checking/Expressions/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3187,6 +3187,24 @@ let BuildRecdFieldSet g m objExpr (rfinfo: RecdFieldInfo) argExpr =
let wrap, objExpr, _readonly, _writeonly = mkExprAddrOfExpr g boxity false DefinitelyMutates objExpr None m
wrap (mkRecdFieldSetViaExprAddr (objExpr, rfinfo.RecdFieldRef, rfinfo.TypeInst, argExpr, m) )

// This is used to check the target of an attribute with the form of
// Long Form: [<target:attribute-name(arguments)>]
// Short Form: [<attribute-name(arguments)>]
let (|LongFormAttrTarget|UnrecognizedLongAttrTarget|ShortFormAttributeTarget|) (targetIndicator: Ident option) =
match targetIndicator with
| Some id when id.idText = "assembly" -> LongFormAttrTarget AttributeTargets.Assembly
| Some id when id.idText = "module" -> LongFormAttrTarget AttributeTargets.Module
| Some id when id.idText = "return" -> LongFormAttrTarget AttributeTargets.ReturnValue
| Some id when id.idText = "field" -> LongFormAttrTarget AttributeTargets.Field
| Some id when id.idText = "property" -> LongFormAttrTarget AttributeTargets.Property
| Some id when id.idText = "method" -> LongFormAttrTarget AttributeTargets.Method
| Some id when id.idText = "param" -> LongFormAttrTarget AttributeTargets.Parameter
| Some id when id.idText = "type" -> LongFormAttrTarget AttributeTargets.TyconDecl
| Some id when id.idText = "constructor" -> LongFormAttrTarget AttributeTargets.Constructor
| Some id when id.idText = "event" -> LongFormAttrTarget AttributeTargets.Event
| Some id -> UnrecognizedLongAttrTarget id
| None -> ShortFormAttributeTarget

//-------------------------------------------------------------------------
// Helpers dealing with named and optional args at callsites
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -11344,30 +11362,21 @@ and TcAttributeEx canFail (cenv: cenv) (env: TcEnv) attrTgt attrEx (synAttr: Syn
(validOnDefault, inheritedDefault)
| _ ->
(validOnDefault, inheritedDefault)
let possibleTgts = enum validOn &&& attrTgt
let directedTgts =
let attributeTargets = enum validOn &&& attrTgt
let directedTargets =
match targetIndicator with
| Some id when id.idText = "assembly" -> AttributeTargets.Assembly
| Some id when id.idText = "module" -> AttributeTargets.Module
| Some id when id.idText = "return" -> AttributeTargets.ReturnValue
| Some id when id.idText = "field" -> AttributeTargets.Field
| Some id when id.idText = "property" -> AttributeTargets.Property
| Some id when id.idText = "method" -> AttributeTargets.Method
| Some id when id.idText = "param" -> AttributeTargets.Parameter
| Some id when id.idText = "type" -> AttributeTargets.TyconDecl
| Some id when id.idText = "constructor" -> AttributeTargets.Constructor
| Some id when id.idText = "event" -> AttributeTargets.Event
| Some id ->
errorR(Error(FSComp.SR.tcUnrecognizedAttributeTarget(), id.idRange))
possibleTgts
// mask explicit targets
| _ -> possibleTgts &&& ~~~ attrEx
let constrainedTgts = possibleTgts &&& directedTgts
if constrainedTgts = enum 0 then
if (directedTgts = AttributeTargets.Assembly || directedTgts = AttributeTargets.Module) then
| LongFormAttrTarget attrTarget -> attrTarget
| UnrecognizedLongAttrTarget attrTarget ->
errorR(Error(FSComp.SR.tcUnrecognizedAttributeTarget(), attrTarget.idRange))
attributeTargets
| ShortFormAttributeTarget -> attributeTargets &&& ~~~ attrEx

let constrainedTargets = attributeTargets &&& directedTargets
if constrainedTargets = enum 0 then
if (directedTargets = AttributeTargets.Assembly || directedTargets = AttributeTargets.Module) then
error(Error(FSComp.SR.tcAttributeIsNotValidForLanguageElementUseDo(), mAttr))
else
error(Error(FSComp.SR.tcAttributeIsNotValidForLanguageElement(), mAttr))
warning(Error(FSComp.SR.tcAttributeIsNotValidForLanguageElement(), mAttr))

match ResolveObjectConstructor cenv.nameResolver env.DisplayEnv mAttr ad ty with
| Exception _ when canFail = TcCanFail.IgnoreAllErrors || canFail = TcCanFail.IgnoreMemberResoutionError -> [ ], true
Expand Down Expand Up @@ -11428,19 +11437,19 @@ and TcAttributeEx canFail (cenv: cenv) (env: TcEnv) attrTgt attrEx (synAttr: Syn
if isStruct then error (Error(FSComp.SR.tcCustomAttributeMustBeReferenceType(), m))
if args.Length <> ilMethRef.ArgTypes.Length then error (Error(FSComp.SR.tcCustomAttributeArgumentMismatch(), m))
let args = args |> List.map mkAttribExpr
Attrib(tcref, ILAttrib ilMethRef, args, namedAttribArgMap, isAppliedToGetterOrSetter, Some constrainedTgts, m)
Attrib(tcref, ILAttrib ilMethRef, args, namedAttribArgMap, isAppliedToGetterOrSetter, Some constrainedTargets, m)

| Expr.App (InnerExprPat(ExprValWithPossibleTypeInst(vref, _, _, _)), _, _, args, _) ->
let args = args |> List.collect (function Expr.Const (Const.Unit, _, _) -> [] | expr -> tryDestRefTupleExpr expr) |> List.map mkAttribExpr
Attrib(tcref, FSAttrib vref, args, namedAttribArgMap, isAppliedToGetterOrSetter, Some constrainedTgts, mAttr)
Attrib(tcref, FSAttrib vref, args, namedAttribArgMap, isAppliedToGetterOrSetter, Some constrainedTargets, mAttr)

| _ ->
error (Error(FSComp.SR.tcCustomAttributeMustInvokeConstructor(), mAttr))

| _ ->
error(Error(FSComp.SR.tcAttributeExpressionsMustBeConstructorCalls(), mAttr))

[ (constrainedTgts, attrib) ], false
[ (constrainedTargets, attrib) ], false

and TcAttributesWithPossibleTargetsEx canFail (cenv: cenv) env attrTgt attrEx synAttribs =

Expand Down
Loading