Skip to content

Commit

Permalink
Add trivia to Nullness nodes in SyntaxTree. (#17745)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Sep 16, 2024
1 parent 006f91e commit e668b90
Show file tree
Hide file tree
Showing 30 changed files with 178 additions and 94 deletions.
2 changes: 2 additions & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.100.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
* Enable LanguageFeature.EnforceAttributeTargets in F# 9.0. ([Issue #17514](https://github.com/dotnet/fsharp/issues/17558), [PR #17516](https://github.com/dotnet/fsharp/pull/17558))
* Parser: better recovery for unfinished patterns ([PR #17231](https://github.com/dotnet/fsharp/pull/17231), [PR #17232](https://github.com/dotnet/fsharp/pull/17232)))
* Enable consuming generic arguments defined as `allows ref struct` in C# ([Issue #17597](https://github.com/dotnet/fsharp/issues/17597)
* Trivia for SynTypeConstraint.WhereTyparNotSupportsNull. ([Issue #17721](https://github.com/dotnet/fsharp/issues/17721), [PR #17745](https://github.com/dotnet/fsharp/pull/17745))
* Trivia for SynType.WithNull. ([Issue #17720](https://github.com/dotnet/fsharp/issues/17720), [PR #17745](https://github.com/dotnet/fsharp/pull/17745))

### Changed

Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Checking/Expressions/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4028,7 +4028,7 @@ let rec TcTyparConstraint ridx (cenv: cenv) newOk checkConstraints occ (env: TcE
| SynTypeConstraint.WhereTyparSupportsNull(tp, m) ->
TcSimpleTyparConstraint cenv env newOk tpenv tp m AddCxTypeUseSupportsNull

| SynTypeConstraint.WhereTyparNotSupportsNull(tp, m) ->
| SynTypeConstraint.WhereTyparNotSupportsNull(tp, m, _) ->
if g.langFeatureNullness then
TcSimpleTyparConstraint cenv env newOk tpenv tp m AddCxTypeDefnNotSupportsNull
else
Expand Down Expand Up @@ -4472,7 +4472,7 @@ and TcTypeOrMeasure kindOpt (cenv: cenv) newOk checkConstraints occ (iwsam: Warn
errorR(Error(FSComp.SR.parsInvalidLiteralInType(), m))
NewErrorType (), tpenv

| SynType.WithNull(innerTy, ambivalent, m) ->
| SynType.WithNull(innerTy, ambivalent, m, _) ->
let innerTyC, tpenv = TcTypeAndRecover cenv newOk checkConstraints occ WarnOnIWSAM.Yes env tpenv innerTy
let nullness = if ambivalent then KnownAmbivalentToNull else KnownWithNull
let tyWithNull = TcAddNullnessToType false cenv env nullness innerTyC m
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Service/ServiceParseTreeWalk.fs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ module SyntaxTraversal =
| SynType.Fun(argType = ty1; returnType = ty2) -> [ ty1; ty2 ] |> List.tryPick (traverseSynType path)
| SynType.MeasurePower(ty, _, _)
| SynType.HashConstraint(ty, _)
| SynType.WithNull(ty, _, _)
| SynType.WithNull(innerType = ty)
| SynType.WithGlobalConstraints(ty, _, _)
| SynType.Array(_, ty, _) -> traverseSynType path ty
| SynType.StaticConstantNamed(ty1, ty2, _)
Expand Down
8 changes: 4 additions & 4 deletions src/Compiler/Service/ServiceParsedInputOps.fs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ module ParsedInput =
| SynTypeConstraint.WhereTyparIsReferenceType(t, _) -> walkTypar t
| SynTypeConstraint.WhereTyparIsUnmanaged(t, _) -> walkTypar t
| SynTypeConstraint.WhereTyparSupportsNull(t, _) -> walkTypar t
| SynTypeConstraint.WhereTyparNotSupportsNull(t, _) -> walkTypar t
| SynTypeConstraint.WhereTyparNotSupportsNull(genericName = t) -> walkTypar t
| SynTypeConstraint.WhereTyparIsComparable(t, _) -> walkTypar t
| SynTypeConstraint.WhereTyparIsEquatable(t, _) -> walkTypar t
| SynTypeConstraint.WhereTyparSubtypeOfType(t, ty, _) -> walkTypar t |> Option.orElseWith (fun () -> walkType ty)
Expand Down Expand Up @@ -711,7 +711,7 @@ module ParsedInput =
| SynType.Array(_, t, _) -> walkType t
| SynType.Fun(argType = t1; returnType = t2) -> walkType t1 |> Option.orElseWith (fun () -> walkType t2)
| SynType.WithGlobalConstraints(t, _, _) -> walkType t
| SynType.WithNull(t, _, _)
| SynType.WithNull(innerType = t)
| SynType.HashConstraint(t, _) -> walkType t
| SynType.Or(t1, t2, _, _) -> walkType t1 |> Option.orElseWith (fun () -> walkType t2)
| SynType.MeasurePower(t, _, _) -> walkType t
Expand Down Expand Up @@ -1914,7 +1914,7 @@ module ParsedInput =
| SynTypeConstraint.WhereTyparIsReferenceType(t, _)
| SynTypeConstraint.WhereTyparIsUnmanaged(t, _)
| SynTypeConstraint.WhereTyparSupportsNull(t, _)
| SynTypeConstraint.WhereTyparNotSupportsNull(t, _)
| SynTypeConstraint.WhereTyparNotSupportsNull(genericName = t)
| SynTypeConstraint.WhereTyparIsComparable(t, _)
| SynTypeConstraint.WhereTyparIsEquatable(t, _) -> walkTypar t
| SynTypeConstraint.WhereTyparDefaultsToType(t, ty, _)
Expand Down Expand Up @@ -1976,7 +1976,7 @@ module ParsedInput =
| SynType.Array(_, t, _)
| SynType.HashConstraint(t, _)
| SynType.MeasurePower(t, _, _)
| SynType.WithNull(t, _, _)
| SynType.WithNull(innerType = t)
| SynType.Paren(t, _)
| SynType.SignatureParameter(usedType = t) -> walkType t
| SynType.Fun(argType = t1; returnType = t2)
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/SyntaxTree/SyntaxTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ type SynTypeConstraint =

| WhereTyparSupportsNull of typar: SynTypar * range: range

| WhereTyparNotSupportsNull of genericName: SynTypar * range: range
| WhereTyparNotSupportsNull of genericName: SynTypar * range: range * trivia: SynTypeConstraintWhereTyparNotSupportsNullTrivia

| WhereTyparIsComparable of typar: SynTypar * range: range

Expand Down Expand Up @@ -465,7 +465,7 @@ type SynType =

| StaticConstantNamed of ident: SynType * value: SynType * range: range

| WithNull of innerType: SynType * ambivalent: bool * range: range
| WithNull of innerType: SynType * ambivalent: bool * range: range * trivia: SynTypeWithNullTrivia

| Paren of innerType: SynType * range: range

Expand Down
7 changes: 5 additions & 2 deletions src/Compiler/SyntaxTree/SyntaxTree.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ type SynTypeConstraint =
| WhereTyparSupportsNull of typar: SynTypar * range: range

/// F# syntax is 'typar : null
| WhereTyparNotSupportsNull of genericName: SynTypar * range: range
| WhereTyparNotSupportsNull of
genericName: SynTypar *
range: range *
trivia: SynTypeConstraintWhereTyparNotSupportsNullTrivia

/// F# syntax is 'typar: comparison
| WhereTyparIsComparable of typar: SynTypar * range: range
Expand Down Expand Up @@ -527,7 +530,7 @@ type SynType =
/// F# syntax: ident=1 etc., used in static parameters to type providers
| StaticConstantNamed of ident: SynType * value: SynType * range: range

| WithNull of innerType: SynType * ambivalent: bool * range: range
| WithNull of innerType: SynType * ambivalent: bool * range: range * trivia: SynTypeWithNullTrivia

| Paren of innerType: SynType * range: range

Expand Down
6 changes: 6 additions & 0 deletions src/Compiler/SyntaxTree/SyntaxTrivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ type SynFieldTrivia =
[<NoEquality; NoComparison>]
type SynTypeOrTrivia = { OrKeyword: range }

[<NoEquality; NoComparison>]
type SynTypeWithNullTrivia = { BarRange: range }

[<NoEquality; NoComparison>]
type SynBindingReturnInfoTrivia = { ColonRange: range option }

Expand All @@ -429,3 +432,6 @@ type SynMeasureConstantTrivia =
LessRange: range
GreaterRange: range
}

[<NoEquality; NoComparison>]
type SynTypeConstraintWhereTyparNotSupportsNullTrivia = { ColonRange: range; NotRange: range }
19 changes: 19 additions & 0 deletions src/Compiler/SyntaxTree/SyntaxTrivia.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,14 @@ type SynTypeOrTrivia =
OrKeyword: range
}

/// Represents additional information for SynType.WithNull
[<NoEquality; NoComparison>]
type SynTypeWithNullTrivia =
{
/// The syntax range of the `|` token
BarRange: range
}

/// Represents additional information for SynBindingReturnInfo
[<NoEquality; NoComparison>]
type SynBindingReturnInfoTrivia =
Expand Down Expand Up @@ -545,3 +553,14 @@ type SynTyparDeclTrivia =
type SynMeasureConstantTrivia =
{ LessRange: range
GreaterRange: range }

/// Represents additional information for SynTypeConstraint.WhereTyparNotSupportsNull
[<NoEquality; NoComparison>]
type SynTypeConstraintWhereTyparNotSupportsNullTrivia =
{
/// The syntax range of `:`
ColonRange: range

/// The syntax range of `not`
NotRange: range
}
9 changes: 5 additions & 4 deletions src/Compiler/pars.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -2652,8 +2652,9 @@ typeConstraint:
{ SynTypeConstraint.WhereTyparSupportsNull($1, lhs parseState) }

| typar COLON IDENT NULL
{ if $3 <> "not" then reportParseErrorAt (rhs parseState 3) (FSComp.SR.parsUnexpectedIdentifier($3 + " (2)"))
SynTypeConstraint.WhereTyparNotSupportsNull($1, lhs parseState) }
{ if $3 <> "not" then reportParseErrorAt (rhs parseState 3) (FSComp.SR.parsUnexpectedIdentifier($3 + " (2)"))
let trivia : SynTypeConstraintWhereTyparNotSupportsNullTrivia = { ColonRange = rhs parseState 2; NotRange = rhs parseState 3 }
SynTypeConstraint.WhereTyparNotSupportsNull($1, lhs parseState, trivia) }

| typar COLON LPAREN classMemberSpfn rparen
{ let tp = $1
Expand Down Expand Up @@ -3241,7 +3242,7 @@ cType:
SynType.App(SynType.LongIdent(SynLongIdent([ident("nativeptr", m)], [], [ Some(IdentTrivia.OriginalNotation "*") ])), None, [$1], [], None, true, m) }

| cType BAR_JUST_BEFORE_NULL NULL
{ SynType.WithNull($1, false, lhs parseState) }
{ SynType.WithNull($1, false, lhs parseState, { BarRange = rhs parseState 2 }) }

| cType AMP
{ let m = lhs parseState
Expand Down Expand Up @@ -6193,7 +6194,7 @@ appTypeConPower:

appTypeCanBeNullable:
| appTypeWithoutNull BAR_JUST_BEFORE_NULL NULL
{ SynType.WithNull($1, false, lhs parseState) }
{ SynType.WithNull($1, false, lhs parseState, { BarRange = rhs parseState 2 }) }

| appTypeWithoutNull
{ $1 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9197,6 +9197,8 @@ FSharp.Compiler.Syntax.SynType+WithNull: Boolean ambivalent
FSharp.Compiler.Syntax.SynType+WithNull: Boolean get_ambivalent()
FSharp.Compiler.Syntax.SynType+WithNull: FSharp.Compiler.Syntax.SynType get_innerType()
FSharp.Compiler.Syntax.SynType+WithNull: FSharp.Compiler.Syntax.SynType innerType
FSharp.Compiler.Syntax.SynType+WithNull: FSharp.Compiler.SyntaxTrivia.SynTypeWithNullTrivia get_trivia()
FSharp.Compiler.Syntax.SynType+WithNull: FSharp.Compiler.SyntaxTrivia.SynTypeWithNullTrivia trivia
FSharp.Compiler.Syntax.SynType+WithNull: FSharp.Compiler.Text.Range get_range()
FSharp.Compiler.Syntax.SynType+WithNull: FSharp.Compiler.Text.Range range
FSharp.Compiler.Syntax.SynType: Boolean IsAnon
Expand Down Expand Up @@ -9264,7 +9266,7 @@ FSharp.Compiler.Syntax.SynType: FSharp.Compiler.Syntax.SynType NewStaticConstant
FSharp.Compiler.Syntax.SynType: FSharp.Compiler.Syntax.SynType NewTuple(Boolean, Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.SynTupleTypeSegment], FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynType: FSharp.Compiler.Syntax.SynType NewVar(FSharp.Compiler.Syntax.SynTypar, FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynType: FSharp.Compiler.Syntax.SynType NewWithGlobalConstraints(FSharp.Compiler.Syntax.SynType, Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Syntax.SynTypeConstraint], FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynType: FSharp.Compiler.Syntax.SynType NewWithNull(FSharp.Compiler.Syntax.SynType, Boolean, FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynType: FSharp.Compiler.Syntax.SynType NewWithNull(FSharp.Compiler.Syntax.SynType, Boolean, FSharp.Compiler.Text.Range, FSharp.Compiler.SyntaxTrivia.SynTypeWithNullTrivia)
FSharp.Compiler.Syntax.SynType: FSharp.Compiler.Syntax.SynType+Anon
FSharp.Compiler.Syntax.SynType: FSharp.Compiler.Syntax.SynType+AnonRecd
FSharp.Compiler.Syntax.SynType: FSharp.Compiler.Syntax.SynType+App
Expand Down Expand Up @@ -9350,6 +9352,8 @@ FSharp.Compiler.Syntax.SynTypeConstraint+WhereTyparIsValueType: FSharp.Compiler.
FSharp.Compiler.Syntax.SynTypeConstraint+WhereTyparIsValueType: FSharp.Compiler.Text.Range range
FSharp.Compiler.Syntax.SynTypeConstraint+WhereTyparNotSupportsNull: FSharp.Compiler.Syntax.SynTypar genericName
FSharp.Compiler.Syntax.SynTypeConstraint+WhereTyparNotSupportsNull: FSharp.Compiler.Syntax.SynTypar get_genericName()
FSharp.Compiler.Syntax.SynTypeConstraint+WhereTyparNotSupportsNull: FSharp.Compiler.SyntaxTrivia.SynTypeConstraintWhereTyparNotSupportsNullTrivia get_trivia()
FSharp.Compiler.Syntax.SynTypeConstraint+WhereTyparNotSupportsNull: FSharp.Compiler.SyntaxTrivia.SynTypeConstraintWhereTyparNotSupportsNullTrivia trivia
FSharp.Compiler.Syntax.SynTypeConstraint+WhereTyparNotSupportsNull: FSharp.Compiler.Text.Range get_range()
FSharp.Compiler.Syntax.SynTypeConstraint+WhereTyparNotSupportsNull: FSharp.Compiler.Text.Range range
FSharp.Compiler.Syntax.SynTypeConstraint+WhereTyparSubtypeOfType: FSharp.Compiler.Syntax.SynTypar get_typar()
Expand Down Expand Up @@ -9403,7 +9407,7 @@ FSharp.Compiler.Syntax.SynTypeConstraint: FSharp.Compiler.Syntax.SynTypeConstrai
FSharp.Compiler.Syntax.SynTypeConstraint: FSharp.Compiler.Syntax.SynTypeConstraint NewWhereTyparIsReferenceType(FSharp.Compiler.Syntax.SynTypar, FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynTypeConstraint: FSharp.Compiler.Syntax.SynTypeConstraint NewWhereTyparIsUnmanaged(FSharp.Compiler.Syntax.SynTypar, FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynTypeConstraint: FSharp.Compiler.Syntax.SynTypeConstraint NewWhereTyparIsValueType(FSharp.Compiler.Syntax.SynTypar, FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynTypeConstraint: FSharp.Compiler.Syntax.SynTypeConstraint NewWhereTyparNotSupportsNull(FSharp.Compiler.Syntax.SynTypar, FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynTypeConstraint: FSharp.Compiler.Syntax.SynTypeConstraint NewWhereTyparNotSupportsNull(FSharp.Compiler.Syntax.SynTypar, FSharp.Compiler.Text.Range, FSharp.Compiler.SyntaxTrivia.SynTypeConstraintWhereTyparNotSupportsNullTrivia)
FSharp.Compiler.Syntax.SynTypeConstraint: FSharp.Compiler.Syntax.SynTypeConstraint NewWhereTyparSubtypeOfType(FSharp.Compiler.Syntax.SynTypar, FSharp.Compiler.Syntax.SynType, FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynTypeConstraint: FSharp.Compiler.Syntax.SynTypeConstraint NewWhereTyparSupportsMember(FSharp.Compiler.Syntax.SynType, FSharp.Compiler.Syntax.SynMemberSig, FSharp.Compiler.Text.Range)
FSharp.Compiler.Syntax.SynTypeConstraint: FSharp.Compiler.Syntax.SynTypeConstraint NewWhereTyparSupportsNull(FSharp.Compiler.Syntax.SynTypar, FSharp.Compiler.Text.Range)
Expand Down Expand Up @@ -10573,6 +10577,12 @@ FSharp.Compiler.SyntaxTrivia.SynTyparDeclTrivia: Microsoft.FSharp.Collections.FS
FSharp.Compiler.SyntaxTrivia.SynTyparDeclTrivia: Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.Range] get_AmpersandRanges()
FSharp.Compiler.SyntaxTrivia.SynTyparDeclTrivia: System.String ToString()
FSharp.Compiler.SyntaxTrivia.SynTyparDeclTrivia: Void .ctor(Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.Text.Range])
FSharp.Compiler.SyntaxTrivia.SynTypeConstraintWhereTyparNotSupportsNullTrivia: FSharp.Compiler.Text.Range ColonRange
FSharp.Compiler.SyntaxTrivia.SynTypeConstraintWhereTyparNotSupportsNullTrivia: FSharp.Compiler.Text.Range NotRange
FSharp.Compiler.SyntaxTrivia.SynTypeConstraintWhereTyparNotSupportsNullTrivia: FSharp.Compiler.Text.Range get_ColonRange()
FSharp.Compiler.SyntaxTrivia.SynTypeConstraintWhereTyparNotSupportsNullTrivia: FSharp.Compiler.Text.Range get_NotRange()
FSharp.Compiler.SyntaxTrivia.SynTypeConstraintWhereTyparNotSupportsNullTrivia: System.String ToString()
FSharp.Compiler.SyntaxTrivia.SynTypeConstraintWhereTyparNotSupportsNullTrivia: Void .ctor(FSharp.Compiler.Text.Range, FSharp.Compiler.Text.Range)
FSharp.Compiler.SyntaxTrivia.SynTypeDefnLeadingKeyword+And: FSharp.Compiler.Text.Range Item
FSharp.Compiler.SyntaxTrivia.SynTypeDefnLeadingKeyword+And: FSharp.Compiler.Text.Range get_Item()
FSharp.Compiler.SyntaxTrivia.SynTypeDefnLeadingKeyword+StaticType: FSharp.Compiler.Text.Range get_staticRange()
Expand Down Expand Up @@ -10635,6 +10645,10 @@ FSharp.Compiler.SyntaxTrivia.SynTypeOrTrivia: FSharp.Compiler.Text.Range OrKeywo
FSharp.Compiler.SyntaxTrivia.SynTypeOrTrivia: FSharp.Compiler.Text.Range get_OrKeyword()
FSharp.Compiler.SyntaxTrivia.SynTypeOrTrivia: System.String ToString()
FSharp.Compiler.SyntaxTrivia.SynTypeOrTrivia: Void .ctor(FSharp.Compiler.Text.Range)
FSharp.Compiler.SyntaxTrivia.SynTypeWithNullTrivia: FSharp.Compiler.Text.Range BarRange
FSharp.Compiler.SyntaxTrivia.SynTypeWithNullTrivia: FSharp.Compiler.Text.Range get_BarRange()
FSharp.Compiler.SyntaxTrivia.SynTypeWithNullTrivia: System.String ToString()
FSharp.Compiler.SyntaxTrivia.SynTypeWithNullTrivia: Void .ctor(FSharp.Compiler.Text.Range)
FSharp.Compiler.SyntaxTrivia.SynUnionCaseTrivia: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range] BarRange
FSharp.Compiler.SyntaxTrivia.SynUnionCaseTrivia: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.Range] get_BarRange()
FSharp.Compiler.SyntaxTrivia.SynUnionCaseTrivia: System.String ToString()
Expand Down
Loading

0 comments on commit e668b90

Please sign in to comment.