Skip to content

Commit 230df0c

Browse files
authored
Make attribute target mismatch a warning and not an error. (#18492)
* make attribute targets mismatches a warning and not an error. * release notes * update tests * update baselines * Update baselines * Move attribute form logic to an AP * Fix merge
1 parent 66e1637 commit 230df0c

File tree

11 files changed

+162
-163
lines changed

11 files changed

+162
-163
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.300.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* Warning for "useless null handling" works with piped syntax constructs now ([PR #18331](https://github.com/dotnet/fsharp/pull/18331))
5555
* Make indent in generated overridden member code depend on the context, not fix to 4. ([PR #18341](https://github.com/dotnet/fsharp/pull/18341))
5656
* Adjust caller info attribute error message range ([PR #18388](https://github.com/dotnet/fsharp/pull/18388))
57+
* Make attribute targets mismatch a warning and not an error ([PR #18492](https://github.com/dotnet/fsharp/pull/18492))
5758

5859
### Breaking Changes
5960
* 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))

src/Compiler/Checking/Expressions/CheckExpressions.fs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,6 +3187,24 @@ let BuildRecdFieldSet g m objExpr (rfinfo: RecdFieldInfo) argExpr =
31873187
let wrap, objExpr, _readonly, _writeonly = mkExprAddrOfExpr g boxity false DefinitelyMutates objExpr None m
31883188
wrap (mkRecdFieldSetViaExprAddr (objExpr, rfinfo.RecdFieldRef, rfinfo.TypeInst, argExpr, m) )
31893189

3190+
// This is used to check the target of an attribute with the form of
3191+
// Long Form: [<target:attribute-name(arguments)>]
3192+
// Short Form: [<attribute-name(arguments)>]
3193+
let (|LongFormAttrTarget|UnrecognizedLongAttrTarget|ShortFormAttributeTarget|) (targetIndicator: Ident option) =
3194+
match targetIndicator with
3195+
| Some id when id.idText = "assembly" -> LongFormAttrTarget AttributeTargets.Assembly
3196+
| Some id when id.idText = "module" -> LongFormAttrTarget AttributeTargets.Module
3197+
| Some id when id.idText = "return" -> LongFormAttrTarget AttributeTargets.ReturnValue
3198+
| Some id when id.idText = "field" -> LongFormAttrTarget AttributeTargets.Field
3199+
| Some id when id.idText = "property" -> LongFormAttrTarget AttributeTargets.Property
3200+
| Some id when id.idText = "method" -> LongFormAttrTarget AttributeTargets.Method
3201+
| Some id when id.idText = "param" -> LongFormAttrTarget AttributeTargets.Parameter
3202+
| Some id when id.idText = "type" -> LongFormAttrTarget AttributeTargets.TyconDecl
3203+
| Some id when id.idText = "constructor" -> LongFormAttrTarget AttributeTargets.Constructor
3204+
| Some id when id.idText = "event" -> LongFormAttrTarget AttributeTargets.Event
3205+
| Some id -> UnrecognizedLongAttrTarget id
3206+
| None -> ShortFormAttributeTarget
3207+
31903208
//-------------------------------------------------------------------------
31913209
// Helpers dealing with named and optional args at callsites
31923210
//-------------------------------------------------------------------------
@@ -11344,30 +11362,21 @@ and TcAttributeEx canFail (cenv: cenv) (env: TcEnv) attrTgt attrEx (synAttr: Syn
1134411362
(validOnDefault, inheritedDefault)
1134511363
| _ ->
1134611364
(validOnDefault, inheritedDefault)
11347-
let possibleTgts = enum validOn &&& attrTgt
11348-
let directedTgts =
11365+
let attributeTargets = enum validOn &&& attrTgt
11366+
let directedTargets =
1134911367
match targetIndicator with
11350-
| Some id when id.idText = "assembly" -> AttributeTargets.Assembly
11351-
| Some id when id.idText = "module" -> AttributeTargets.Module
11352-
| Some id when id.idText = "return" -> AttributeTargets.ReturnValue
11353-
| Some id when id.idText = "field" -> AttributeTargets.Field
11354-
| Some id when id.idText = "property" -> AttributeTargets.Property
11355-
| Some id when id.idText = "method" -> AttributeTargets.Method
11356-
| Some id when id.idText = "param" -> AttributeTargets.Parameter
11357-
| Some id when id.idText = "type" -> AttributeTargets.TyconDecl
11358-
| Some id when id.idText = "constructor" -> AttributeTargets.Constructor
11359-
| Some id when id.idText = "event" -> AttributeTargets.Event
11360-
| Some id ->
11361-
errorR(Error(FSComp.SR.tcUnrecognizedAttributeTarget(), id.idRange))
11362-
possibleTgts
11363-
// mask explicit targets
11364-
| _ -> possibleTgts &&& ~~~ attrEx
11365-
let constrainedTgts = possibleTgts &&& directedTgts
11366-
if constrainedTgts = enum 0 then
11367-
if (directedTgts = AttributeTargets.Assembly || directedTgts = AttributeTargets.Module) then
11368+
| LongFormAttrTarget attrTarget -> attrTarget
11369+
| UnrecognizedLongAttrTarget attrTarget ->
11370+
errorR(Error(FSComp.SR.tcUnrecognizedAttributeTarget(), attrTarget.idRange))
11371+
attributeTargets
11372+
| ShortFormAttributeTarget -> attributeTargets &&& ~~~ attrEx
11373+
11374+
let constrainedTargets = attributeTargets &&& directedTargets
11375+
if constrainedTargets = enum 0 then
11376+
if (directedTargets = AttributeTargets.Assembly || directedTargets = AttributeTargets.Module) then
1136811377
error(Error(FSComp.SR.tcAttributeIsNotValidForLanguageElementUseDo(), mAttr))
1136911378
else
11370-
error(Error(FSComp.SR.tcAttributeIsNotValidForLanguageElement(), mAttr))
11379+
warning(Error(FSComp.SR.tcAttributeIsNotValidForLanguageElement(), mAttr))
1137111380

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

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

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

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

11443-
[ (constrainedTgts, attrib) ], false
11452+
[ (constrainedTargets, attrib) ], false
1144411453

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

0 commit comments

Comments
 (0)