Skip to content

Commit

Permalink
Include inline in trivia. (#14503)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Dec 28, 2022
1 parent 35971aa commit e30d14c
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 31 deletions.
16 changes: 12 additions & 4 deletions src/Compiler/SyntaxTree/ParseHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,9 @@ let (|GetIdent|SetIdent|OtherIdent|) (ident: Ident option) =

let mkSynMemberDefnGetSet
(parseState: IParseState)
(opt_inline: bool)
(opt_inline: range option)
(mWith: range)
(classDefnMemberGetSetElements: (bool * SynAttributeList list * (SynPat * range) * (range option * SynReturnInfo) option * range option * SynExpr * range) list)
(classDefnMemberGetSetElements: (range option * SynAttributeList list * (SynPat * range) * (range option * SynReturnInfo) option * range option * SynExpr * range) list)
(mAnd: range option)
(mWhole: range)
(propertyNameBindingPat: SynPat)
Expand All @@ -441,15 +441,15 @@ let mkSynMemberDefnGetSet

let tryMkSynMemberDefnMember
(
optInline,
mOptInline: range option,
optAttrs: SynAttributeList list,
(bindingPat, mBindLhs),
optReturnType,
mEquals,
expr,
mExpr
) : (SynMemberDefn * Ident option) option =
let optInline = opt_inline || optInline
let optInline = Option.isSome opt_inline || Option.isSome mOptInline
// optional attributes are only applied to getters and setters
// the "top level" attrs will be applied to both
let optAttrs =
Expand All @@ -469,6 +469,7 @@ let mkSynMemberDefnGetSet
let trivia: SynBindingTrivia =
{
LeadingKeyword = leadingKeyword
InlineKeyword = mOptInline
EqualsRange = mEquals
}

Expand Down Expand Up @@ -729,6 +730,7 @@ let mkSynMemberDefnGetSet
if getOrSet.idText = "get" then
let trivia =
{
InlineKeyword = opt_inline
WithKeyword = mWith
GetKeyword = Some getOrSet.idRange
AndKeyword = None
Expand All @@ -739,6 +741,7 @@ let mkSynMemberDefnGetSet
else
let trivia =
{
InlineKeyword = opt_inline
WithKeyword = mWith
GetKeyword = None
AndKeyword = None
Expand All @@ -759,6 +762,7 @@ let mkSynMemberDefnGetSet

let trivia =
{
InlineKeyword = opt_inline
WithKeyword = mWith
GetKeyword = Some mGet
AndKeyword = mAnd
Expand All @@ -772,20 +776,23 @@ let mkSynMemberDefnGetSet
match getOrSet with
| GetIdent mGet ->
{
InlineKeyword = opt_inline
WithKeyword = mWith
GetKeyword = Some mGet
AndKeyword = mAnd
SetKeyword = None
}
| SetIdent mSet ->
{
InlineKeyword = opt_inline
WithKeyword = mWith
GetKeyword = None
AndKeyword = mAnd
SetKeyword = Some mSet
}
| OtherIdent ->
{
InlineKeyword = opt_inline
WithKeyword = mWith
AndKeyword = mAnd
GetKeyword = None
Expand Down Expand Up @@ -899,6 +906,7 @@ let mkSynDoBinding (vis: SynAccess option, mDo, expr, m) =
DebugPointAtBinding.NoneAtDo,
{
LeadingKeyword = SynLeadingKeyword.Do mDo
InlineKeyword = None
EqualsRange = None
}
)
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/SyntaxTree/ParseHelpers.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ val raiseParseErrorAt: range -> (int * string) -> 'a

val mkSynMemberDefnGetSet:
parseState: IParseState ->
opt_inline: bool ->
opt_inline: range option ->
mWith: range ->
classDefnMemberGetSetElements: (bool * SynAttributeList list * (SynPat * range) * (range option * SynReturnInfo) option * range option * SynExpr * range) list ->
classDefnMemberGetSetElements: (range option * SynAttributeList list * (SynPat * range) * (range option * SynReturnInfo) option * range option * SynExpr * range) list ->
mAnd: range option ->
mWhole: range ->
propertyNameBindingPat: SynPat ->
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/SyntaxTree/SyntaxTrivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,14 @@ type SynLeadingKeyword =
type SynBindingTrivia =
{
LeadingKeyword: SynLeadingKeyword
InlineKeyword: range option
EqualsRange: range option
}

static member Zero: SynBindingTrivia =
{
LeadingKeyword = SynLeadingKeyword.Synthetic
InlineKeyword = None
EqualsRange = None
}

Expand Down Expand Up @@ -288,13 +290,15 @@ type SynModuleOrNamespaceSigTrivia =
type SynValSigTrivia =
{
LeadingKeyword: SynLeadingKeyword
InlineKeyword: range option
WithKeyword: range option
EqualsRange: range option
}

static member Zero: SynValSigTrivia =
{
LeadingKeyword = SynLeadingKeyword.Synthetic
InlineKeyword = None
WithKeyword = None
EqualsRange = None
}
Expand All @@ -305,6 +309,7 @@ type SynTypeFunTrivia = { ArrowRange: range }
[<NoEquality; NoComparison>]
type SynMemberGetSetTrivia =
{
InlineKeyword: range option
WithKeyword: range
GetKeyword: range option
AndKeyword: range option
Expand Down
9 changes: 9 additions & 0 deletions src/Compiler/SyntaxTree/SyntaxTrivia.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ type SynBindingTrivia =
/// Used leading keyword of SynBinding
LeadingKeyword: SynLeadingKeyword

/// The syntax range of the `inline` keyword
InlineKeyword: range option

/// The syntax range of the `=` token.
EqualsRange: range option
}
Expand Down Expand Up @@ -362,6 +365,9 @@ type SynValSigTrivia =
/// but in case of `SynMemberDefn.AutoProperty` or `SynMemberDefn.AbstractSlot` it could be something else.
LeadingKeyword: SynLeadingKeyword

/// The syntax range of the `inline` keyword
InlineKeyword: range option

/// The syntax range of the `with` keyword
WithKeyword: range option

Expand All @@ -383,6 +389,9 @@ type SynTypeFunTrivia =
[<NoEquality; NoComparison>]
type SynMemberGetSetTrivia =
{
/// The syntax range of the `inline` keyword
InlineKeyword: range option

/// The syntax range of the `with` keyword
WithKeyword: range

Expand Down
38 changes: 19 additions & 19 deletions src/Compiler/pars.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ moduleSpfn:
valSpfn:
| opt_attributes opt_access VAL opt_attributes opt_inline opt_mutable opt_access nameop opt_explicitValTyparDecls COLON topTypeWithTypeConstraints optLiteralValueSpfn
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(), rhs parseState 2))
let attr1, attr2, isInline, isMutable, vis2, id, doc, explicitValTyparDecls, (ty, arity), (mEquals, konst: SynExpr option) = ($1), ($4), ($5), ($6), ($7), ($8), grabXmlDoc(parseState, $1, 1), ($9), ($11), ($12)
let attr1, attr2, isInline, isMutable, vis2, id, doc, explicitValTyparDecls, (ty, arity), (mEquals, konst: SynExpr option) = ($1), ($4), (Option.isSome $5), ($6), ($7), ($8), grabXmlDoc(parseState, $1, 1), ($9), ($11), ($12)
if not (isNil attr2) then errorR(Deprecated(FSComp.SR.parsAttributesMustComeBeforeVal(), rhs parseState 4))
let m =
rhs2 parseState 1 11
Expand All @@ -705,7 +705,7 @@ valSpfn:
| None -> m
| Some e -> unionRanges m e.Range
let mVal = rhs parseState 3
let trivia: SynValSigTrivia = { LeadingKeyword = SynLeadingKeyword.Val mVal; WithKeyword = None; EqualsRange = mEquals }
let trivia: SynValSigTrivia = { LeadingKeyword = SynLeadingKeyword.Val mVal; InlineKeyword = $5; WithKeyword = None; EqualsRange = mEquals }
let valSpfn = SynValSig((attr1@attr2), id, explicitValTyparDecls, ty, arity, isInline, isMutable, doc, vis2, konst, m, trivia)
SynModuleSigDecl.Val(valSpfn, m)
}
Expand Down Expand Up @@ -913,7 +913,7 @@ classSpfnMembersAtLeastOne:
classMemberSpfn:
| opt_attributes opt_access memberSpecFlags opt_inline opt_access nameop opt_explicitValTyparDecls COLON topTypeWithTypeConstraints classMemberSpfnGetSet optLiteralValueSpfn
{ if Option.isSome $2 then errorR(Error(FSComp.SR.parsVisibilityDeclarationsShouldComePriorToIdentifier(), rhs parseState 2))
let isInline, doc, vis2, id, explicitValTyparDecls, (ty, arity), (mEquals, optLiteralValue) = $4, grabXmlDoc(parseState, $1, 1), $5, $6, $7, $9, $11
let isInline, doc, vis2, id, explicitValTyparDecls, (ty, arity), (mEquals, optLiteralValue) = (Option.isSome $4), grabXmlDoc(parseState, $1, 1), $5, $6, $7, $9, $11
let mWith, (getSet, getSetRangeOpt) = $10
let getSetAdjuster arity = match arity, getSet with SynValInfo([], _), SynMemberKind.Member -> SynMemberKind.PropertyGet | _ -> getSet
let mWhole =
Expand All @@ -930,7 +930,7 @@ classMemberSpfn:

let flags, leadingKeyword = $3
let flags = flags (getSetAdjuster arity)
let trivia = { LeadingKeyword = leadingKeyword; WithKeyword = mWith; EqualsRange = mEquals }
let trivia = { LeadingKeyword = leadingKeyword; InlineKeyword = $4; WithKeyword = mWith; EqualsRange = mEquals }
let valSpfn = SynValSig($1, id, explicitValTyparDecls, ty, arity, isInline, false, doc, vis2, optLiteralValue, mWhole, trivia)
let trivia: SynMemberSigMemberTrivia = { GetSetKeywords = getSetRangeOpt }
SynMemberSig.Member(valSpfn, flags, mWhole, trivia) }
Expand Down Expand Up @@ -970,7 +970,7 @@ classMemberSpfn:
let mNew = rhs parseState 3
let m = unionRanges (rhs parseState 1) ty.Range |> unionRangeWithXmlDoc doc
let isInline = false
let trivia: SynValSigTrivia = { LeadingKeyword = SynLeadingKeyword.New mNew; WithKeyword = None; EqualsRange = None }
let trivia: SynValSigTrivia = { LeadingKeyword = SynLeadingKeyword.New mNew; InlineKeyword = None; WithKeyword = None; EqualsRange = None }
let valSpfn = SynValSig ($1, (SynIdent(mkSynId (rhs parseState 3) "new", None)), noInferredTypars, ty, valSynInfo, isInline, false, doc, vis, None, m, trivia)
SynMemberSig.Member(valSpfn, CtorMemberFlags, m, SynMemberSigMemberTrivia.Zero) }

Expand Down Expand Up @@ -1740,8 +1740,8 @@ memberCore:
let memFlagsBuilder, leadingKeyword = flagsBuilderAndLeadingKeyword
let memberFlags = memFlagsBuilder SynMemberKind.Member
let mWholeBindLhs = (mBindLhs, attrs) ||> unionRangeWithListBy (fun (a: SynAttributeList) -> a.Range)
let trivia: SynBindingTrivia = { LeadingKeyword = leadingKeyword; EqualsRange = Some mEquals }
let binding = mkSynBinding (xmlDoc, bindingPat) (vis, $1, false, mWholeBindLhs, DebugPointAtBinding.NoneAtInvisible, optReturnType, $5, mRhs, [], attrs, Some memberFlags, trivia)
let trivia: SynBindingTrivia = { LeadingKeyword = leadingKeyword; InlineKeyword = $1; EqualsRange = Some mEquals }
let binding = mkSynBinding (xmlDoc, bindingPat) (vis, (Option.isSome $1), false, mWholeBindLhs, DebugPointAtBinding.NoneAtInvisible, optReturnType, $5, mRhs, [], attrs, Some memberFlags, trivia)
let memberRange = unionRanges rangeStart mRhs |> unionRangeWithXmlDoc xmlDoc
[ SynMemberDefn.Member (binding, memberRange) ]) }

Expand Down Expand Up @@ -1819,7 +1819,7 @@ classDefnMember:

| opt_attributes opt_access abstractMemberFlags opt_inline nameop opt_explicitValTyparDecls COLON topTypeWithTypeConstraints classMemberSpfnGetSet opt_ODECLEND
{ let ty, arity = $8
let isInline, doc, id, explicitValTyparDecls = $4, grabXmlDoc(parseState, $1, 1), $5, $6
let isInline, doc, id, explicitValTyparDecls = (Option.isSome $4), grabXmlDoc(parseState, $1, 1), $5, $6
let mWith, (getSet, getSetRangeOpt) = $9
let getSetAdjuster arity = match arity, getSet with SynValInfo([], _), SynMemberKind.Member -> SynMemberKind.PropertyGet | _ -> getSet
let mWhole =
Expand All @@ -1830,7 +1830,7 @@ classDefnMember:
|> unionRangeWithXmlDoc doc
if Option.isSome $2 then errorR(Error(FSComp.SR.parsAccessibilityModsIllegalForAbstract(), mWhole))
let mkFlags, leadingKeyword = $3
let trivia = { LeadingKeyword = leadingKeyword; WithKeyword = mWith; EqualsRange = None }
let trivia = { LeadingKeyword = leadingKeyword; InlineKeyword = $4; WithKeyword = mWith; EqualsRange = None }
let valSpfn = SynValSig($1, id, explicitValTyparDecls, ty, arity, isInline, false, doc, None, None, mWhole, trivia)
let trivia: SynMemberDefnAbstractSlotTrivia = { GetSetKeywords = getSetRangeOpt }
[ SynMemberDefn.AbstractSlot(valSpfn, mkFlags (getSetAdjuster arity), mWhole, trivia) ] }
Expand Down Expand Up @@ -1870,7 +1870,7 @@ classDefnMember:
let declPat = SynPat.LongIdent (SynLongIdent([mkSynId (rhs parseState 3) "new"], [], [None]), None, Some noInferredTypars, SynArgPats.Pats [$4], vis, rhs parseState 3)
// Check that 'SynPatForConstructorDecl' matches this correctly
assert (match declPat with SynPatForConstructorDecl _ -> true | _ -> false)
let synBindingTrivia: SynBindingTrivia = { LeadingKeyword = SynLeadingKeyword.New mNew; EqualsRange = Some mEquals }
let synBindingTrivia: SynBindingTrivia = { LeadingKeyword = SynLeadingKeyword.New mNew; InlineKeyword = None; EqualsRange = Some mEquals }
[ SynMemberDefn.Member(SynBinding (None, SynBindingKind.Normal, false, false, $1, xmlDoc, valSynData, declPat, None, expr, mWholeBindLhs, DebugPointAtBinding.NoneAtInvisible, synBindingTrivia), m) ] }

| opt_attributes opt_access STATIC typeKeyword tyconDefn
Expand Down Expand Up @@ -2669,7 +2669,7 @@ cPrototype:
let bindingPat = SynPat.LongIdent (SynLongIdent([nm], [], [None]), None, Some noInferredTypars, SynArgPats.Pats [SynPat.Tuple(false, args, argsm)], vis, nmm)
let mWholeBindLhs = (mBindLhs, attrs) ||> unionRangeWithListBy (fun (a: SynAttributeList) -> a.Range)
let xmlDoc = grabXmlDoc(parseState, attrs, 1)
let trivia = { LeadingKeyword = SynLeadingKeyword.Extern mExtern; EqualsRange = None }
let trivia = { LeadingKeyword = SynLeadingKeyword.Extern mExtern; InlineKeyword = None; EqualsRange = None }
let binding =
mkSynBinding
(xmlDoc, bindingPat)
Expand Down Expand Up @@ -2792,8 +2792,8 @@ localBinding:
let mWhole = (unionRanges leadingKeyword.Range mRhs, attrs) ||> unionRangeWithListBy (fun (a: SynAttributeList) -> a.Range)
let spBind = if IsDebugPointBinding bindingPat expr then DebugPointAtBinding.Yes mWhole else DebugPointAtBinding.NoneAtLet
let mWholeBindLhs = (mBindLhs, attrs) ||> unionRangeWithListBy (fun (a: SynAttributeList) -> a.Range)
let trivia: SynBindingTrivia = { LeadingKeyword = leadingKeyword; EqualsRange = Some mEquals }
mkSynBinding (xmlDoc, bindingPat) (vis, $1, $2, mWholeBindLhs, spBind, optReturnType, expr, mRhs, opts, attrs, None, trivia))
let trivia: SynBindingTrivia = { LeadingKeyword = leadingKeyword; InlineKeyword = $1; EqualsRange = Some mEquals }
mkSynBinding (xmlDoc, bindingPat) (vis, Option.isSome $1, $2, mWholeBindLhs, spBind, optReturnType, expr, mRhs, opts, attrs, None, trivia))
localBindingRange, localBindingBuilder }

| opt_inline opt_mutable bindingPattern opt_topReturnTypeWithTypeConstraints EQUALS error
Expand All @@ -2807,8 +2807,8 @@ localBinding:
let zeroWidthAtEnd = mEquals.EndRange
let rhsExpr = arbExpr("localBinding1", zeroWidthAtEnd)
let spBind = if IsDebugPointBinding bindingPat rhsExpr then DebugPointAtBinding.Yes mWhole else DebugPointAtBinding.NoneAtLet
let trivia: SynBindingTrivia = { LeadingKeyword = leadingKeyword; EqualsRange = Some mEquals }
mkSynBinding (xmlDoc, bindingPat) (vis, $1, $2, mBindLhs, spBind, optReturnType, rhsExpr, mRhs, [], attrs, None, trivia))
let trivia: SynBindingTrivia = { LeadingKeyword = leadingKeyword; InlineKeyword = $1; EqualsRange = Some mEquals }
mkSynBinding (xmlDoc, bindingPat) (vis, Option.isSome $1, $2, mBindLhs, spBind, optReturnType, rhsExpr, mRhs, [], attrs, None, trivia))
mWhole, localBindingBuilder }

| opt_inline opt_mutable bindingPattern opt_topReturnTypeWithTypeConstraints recover
Expand All @@ -2820,9 +2820,9 @@ localBinding:
let localBindingBuilder =
(fun xmlDoc attrs vis (leadingKeyword: SynLeadingKeyword) ->
let spBind = DebugPointAtBinding.Yes (unionRanges leadingKeyword.Range mRhs)
let trivia = { LeadingKeyword = leadingKeyword; EqualsRange = None }
let trivia = { LeadingKeyword = leadingKeyword; InlineKeyword = $1; EqualsRange = None }
let rhsExpr = arbExpr("localBinding2", mRhs)
mkSynBinding (xmlDoc, bindingPat) (vis, $1, $2, mBindLhs, spBind, optReturnType, rhsExpr, mRhs, [], attrs, None, trivia))
mkSynBinding (xmlDoc, bindingPat) (vis, Option.isSome $1, $2, mBindLhs, spBind, optReturnType, rhsExpr, mRhs, [], attrs, None, trivia))
mWhole, localBindingBuilder }

/* A single expression with an optional type annotation, and an optional static optimization block */
Expand Down Expand Up @@ -5816,8 +5816,8 @@ opt_bar:
| /* EMPTY */ { }

opt_inline:
| INLINE { true }
| /* EMPTY */ { false }
| INLINE { Some (rhs parseState 1) }
| /* EMPTY */ { None }

opt_mutable:
| MUTABLE { true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
<Compile Include="..\service\SyntaxTreeTests\LeadingKeywordTests.fs">
<Link>SyntaxTree\LeadingKeywordTests.fs</Link>
</Compile>
<Compile Include="..\service\SyntaxTreeTests\ValTests.fs">
<Link>SyntaxTree\ValTests.fs</Link>
</Compile>
<Compile Include="..\service\FileSystemTests.fs">
<Link>FileSystemTests.fs</Link>
</Compile>
Expand Down
Loading

0 comments on commit e30d14c

Please sign in to comment.