Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Update to Fantomas 4.6.5 (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
bamarsha authored Mar 1, 2022
1 parent 8c57aa0 commit c337d8d
Show file tree
Hide file tree
Showing 104 changed files with 1,941 additions and 1,969 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"fantomas-tool": {
"version": "4.5.0-beta-001",
"version": "4.6.5",
"commands": [
"fantomas"
]
Expand Down
8 changes: 0 additions & 8 deletions .fantomasignore

This file was deleted.

25 changes: 11 additions & 14 deletions src/QsCompiler/CSharpGeneration/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module internal DeclarationLocations =


type internal SyntaxTreeTransformation private (_private_) =
inherit SyntaxTreeTransformation<TransformationState>(new TransformationState(), TransformationOptions.NoRebuild)
inherit SyntaxTreeTransformation<TransformationState>(TransformationState(), TransformationOptions.NoRebuild)

new() as this =
new SyntaxTreeTransformation("_private_")
Expand Down Expand Up @@ -86,19 +86,16 @@ type CodegenContext =
let result = new Dictionary<string, (string * QsCallable) list>()

syntaxTree
|> Seq.collect
(fun ns ->
ns.Elements
|> Seq.choose
(function
| QsCallable c -> Some(ns, c)
| _ -> None))
|> Seq.iter
(fun (ns: QsNamespace, c: QsCallable) ->
if result.ContainsKey c.FullName.Name then
result.[c.FullName.Name] <- (ns.Name, c) :: (result.[c.FullName.Name])
else
result.[c.FullName.Name] <- [ ns.Name, c ])
|> Seq.collect (fun ns ->
ns.Elements
|> Seq.choose (function
| QsCallable c -> Some(ns, c)
| _ -> None))
|> Seq.iter (fun (ns, c) ->
if result.ContainsKey c.FullName.Name then
result.[c.FullName.Name] <- (ns.Name, c) :: result.[c.FullName.Name]
else
result.[c.FullName.Name] <- [ ns.Name, c ])

result.ToImmutableDictionary()

Expand Down
10 changes: 7 additions & 3 deletions src/QsCompiler/CSharpGeneration/EntryPoint.fs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,13 @@ let private entryPointClass context (entryPoint: QsCallable) =
``property-arrow_get`` typeName name [ ``public`` ] get (``=>`` value)

let nameProperty = string entryPoint.FullName |> literal |> property "Name" "string"
let summaryProperty = (PrintSummary entryPoint.Documentation false).Trim() |> literal |> property "Summary" "string"

let summaryProperty =
(PrintSummary entryPoint.Documentation false).Trim() |> literal |> property "Summary" "string"

let parameters = parameters context entryPoint.Documentation entryPoint.ArgumentTuple

let members : MemberDeclarationSyntax list =
let members: MemberDeclarationSyntax list =
[
nameProperty
summaryProperty
Expand Down Expand Up @@ -371,7 +374,8 @@ let private entryPointNamespace context name entryPoints =

/// Returns the driver settings object.
let private driverSettings context =
let newDriverSettings = driverNamespace + ".DriverSettings" |> ``type`` |> SyntaxFactory.ObjectCreationExpression
let newDriverSettings =
driverNamespace + ".DriverSettings" |> ``type`` |> SyntaxFactory.ObjectCreationExpression

let namedArg (name: string) expr =
SyntaxFactory.NameColon name |> (SyntaxFactory.Argument expr).WithNameColon
Expand Down
46 changes: 30 additions & 16 deletions src/QsCompiler/CSharpGeneration/SimulationCode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Microsoft.Quantum.QsCompiler.CsharpGeneration

#nowarn "46" // Backticks removed by Fantomas: https://github.com/fsprojects/fantomas/issues/2034

open System
open System.Collections.Generic
open System.Collections.Immutable
Expand Down Expand Up @@ -112,12 +114,10 @@ module SimulationCode =

let getTypeParameters types =
let findAll (t: ResolvedType) =
t.ExtractAll
(fun item ->
item.Resolution
|> function
| QsTypeKind.TypeParameter tp -> seq { yield tp }
| _ -> Enumerable.Empty())
t.ExtractAll (fun item ->
match item.Resolution with
| QsTypeKind.TypeParameter tp -> seq { tp }
| _ -> Seq.empty)

types |> Seq.collect findAll |> Seq.distinctBy (fun tp -> tp.Origin, tp.TypeName) |> Seq.toList

Expand Down Expand Up @@ -460,13 +460,15 @@ module SimulationCode =
| NewUdt (udt, args) -> buildNewUdt udt args // needs to be before CallLikeExpression!
| CallLikeExpression (op, args) -> buildApply ex.ResolvedType op args
| MissingExpr -> ident "_" :> ExpressionSyntax

and captureExpression (ex: TypedExpression) =
match ex.Expression with
| Identifier (s, _) when ex.InferredInformation.IsMutable ->
match ex.ResolvedType.Resolution with
| QsTypeKind.ArrayType _ -> invoke (buildId s <|?.|> (ident "Copy")) ``(`` [] ``)``
| _ -> buildExpression ex
| _ -> buildExpression ex

and buildNamedItem ex acc =
match acc with
| LocalVariable name ->
Expand All @@ -480,23 +482,27 @@ module SimulationCode =
| _ ->
// TODO: Diagnostics
failwith "Invalid identifier for named item"

and buildAddExpr (exType: ResolvedType) lhs rhs =
match exType.Resolution |> QArrayType with
| Some arrType -> arrType <.> (ident "Add", [ buildExpression lhs; buildExpression rhs ])
| _ -> ``((`` ((buildExpression lhs) <+> (buildExpression rhs)) ``))``

and buildInterpolatedString (s: string) (exs: ImmutableArray<TypedExpression>) =
if exs.Length <> 0 then
let exprs = exs |> Seq.map buildExpression |> Seq.toList
invoke (ident "String.Format") ``(`` (literal s :: exprs) ``)``
else
literal s

and buildId id : ExpressionSyntax =
match id with
| LocalVariable n -> n |> ident :> ExpressionSyntax
| GlobalCallable n -> getOpName context n |> ident :> ExpressionSyntax
| InvalidIdentifier ->
// TODO: Diagnostics
failwith "Received InvalidIdentifier"

and buildCopyAndUpdateExpression (lhsEx: TypedExpression, accEx: TypedExpression, rhsEx) =
match lhsEx.ResolvedType.Resolution |> QArrayType with
| Some arrayType ->
Expand Down Expand Up @@ -554,8 +560,10 @@ module SimulationCode =
| _ ->
failwith
"copy-and-update expressions are currently only supported for arrays and user defined types"

and buildTuple many : ExpressionSyntax =
many |> Seq.map captureExpression |> Seq.toList |> tuple // captured since we rely on the native C# tuples

and buildPartial (partialType: ResolvedType) typeParamResolutions opEx args =
let (pIn, pOut) = inAndOutputType partialType // The type of the operation constructed by partial application
let (oIn, _) = inAndOutputType opEx.ResolvedType // The type of the operation accepting the partial tuples.
Expand Down Expand Up @@ -607,10 +615,12 @@ module SimulationCode =
// If it does, we can't create the PartialMapper at compile time
// so we just build a partial-tuple and let it be resolved at runtime.
let op = buildExpression opEx
let values = if hasTypeParameters [ pIn; pOut ] then args |> captureExpression else buildPartialMapper ()
let values = if hasTypeParameters [ pIn; pOut ] then captureExpression args else buildPartialMapper ()
op <.> (ident "Partial", [ values ])

and buildNewUdt n args =
``new`` (``type`` [ justTheName context n ]) ``(`` [ args |> captureExpression ] ``)``

and buildApply returnType op args =
// Checks if the expression points to a non-generic user-defined callable.
// Because these have fully-resolved types in the runtime,
Expand Down Expand Up @@ -645,11 +655,13 @@ module SimulationCode =
(ident "Apply")

buildExpression op <.> (apply, [ args |> captureExpression ]) // we need to capture to guarantee that the result accurately reflects any indirect binding of arguments

and buildConditional c t f =
let cond = c |> buildExpression
let whenTrue = t |> captureExpression
let whenFalse = f |> captureExpression
``?`` cond (whenTrue, whenFalse)

and buildRange lhs rEnd =
let args =
lhs.Expression
Expand All @@ -659,17 +671,21 @@ module SimulationCode =
| _ -> [ (buildExpression lhs); (buildExpression rEnd) ]

``new`` (``type`` [ "QRange" ]) ``(`` args ``)``

and buildValueArray at elems =
match at.Resolution |> QArrayType with
| Some arrayType -> ``new`` arrayType ``(`` (elems |> Seq.map captureExpression |> Seq.toList) ``)``
// TODO: diagnostics.
| _ -> failwith ""

and buildSizedArray value size =
let supplier = ``() =>`` [] (captureExpression value) :> ExpressionSyntax
ident "QArray" <.> (ident "Filled", [ supplier; buildExpression size ])

and buildNewArray b count =
let arrayType = (ArrayType b |> QArrayType).Value
arrayType <.> (ident "Create", [ count |> buildExpression ])

and buildArrayItem a i =
match i.ResolvedType.Resolution with
| Range -> invoke ((buildExpression a) <|.|> (ident "Slice")) ``(`` [ (buildExpression i) ] ``)``
Expand Down Expand Up @@ -882,7 +898,7 @@ module SimulationCode =
override this.OnRepeatStatement rs =
let buildTest test fixup =
let condition = buildExpression test
let thens = [ ``break`` ]
let thens = [ break ]
let elses = buildBlock fixup
``if`` ``(`` condition ``)`` thens (Some(``else`` elses))

Expand Down Expand Up @@ -1091,8 +1107,8 @@ module SimulationCode =
// Use the right accessibility for the property depending on the accessibility of the callable.
// Note: In C#, "private protected" is the intersection of protected and internal.
match getCallableAccess qualifiedName |> Option.defaultValue Public with
| Public -> [ ``protected`` ]
| Internal -> [ ``private``; ``protected`` ]
| Public -> [ protected ]
| Internal -> [ ``private``; protected ]

let buildOne qualifiedName =
/// eg:
Expand Down Expand Up @@ -1462,7 +1478,7 @@ module SimulationCode =
``property-arrow_get`` "String" "ICallable.Name" [] get (``=>`` (literal name)) :> MemberDeclarationSyntax

let buildImpl name =
propg "IOperationFactory" "Impl" [ ``private``; ``protected`` ] :> MemberDeclarationSyntax
propg "IOperationFactory" "Impl" [ ``private``; protected ] :> MemberDeclarationSyntax

let buildFullName (name: QsQualifiedName) =
let fqn =
Expand Down Expand Up @@ -1752,8 +1768,7 @@ module SimulationCode =
op.Attributes
|> SymbolResolution.TryFindTestTargets
|> Seq.filter (String.IsNullOrWhiteSpace >> not)
|> Seq.map
(function
|> Seq.map (function
| x when x.Contains(".") ->
let indexOfDot = x.LastIndexOf('.')
{ Namespace = x.Substring(0, indexOfDot); Name = x.Substring(indexOfDot + 1) }
Expand Down Expand Up @@ -1970,9 +1985,8 @@ module SimulationCode =
// Returns only those namespaces and their elements that are defined for the given file.
let findLocalElements selector (fileName: string) syntaxTree =
syntaxTree
|> Seq.map
(fun ns ->
(ns.Name, (FilterBySourceFile.Apply(ns, fileName)).Elements |> Seq.choose selector |> Seq.toList))
|> Seq.map (fun ns ->
(ns.Name, (FilterBySourceFile.Apply(ns, fileName)).Elements |> Seq.choose selector |> Seq.toList))
|> Seq.sortBy fst
|> Seq.filter (fun (_, elements) -> not elements.IsEmpty)
|> Seq.toList
Expand Down
30 changes: 18 additions & 12 deletions src/QsCompiler/Core/DeclarationHeaders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ module DeclarationHeader =
new NullableRangeConverter() :> JsonConverter
|]

let private Serializer = [ qsNullableConverters; Json.Converters false ] |> Array.concat |> Json.CreateSerializer
let private Serializer =
[ qsNullableConverters; Json.Converters false ] |> Array.concat |> Json.CreateSerializer

let private PermissiveSerializer =
[ qsNullableConverters; Json.Converters true ] |> Array.concat |> Json.CreateSerializer
Expand Down Expand Up @@ -198,9 +199,12 @@ type TypeDeclarationHeader =
static member FromJson json =
let success, schema = DeclarationHeader.FromJson<TypeDeclarationHeaderSchema> json
let header = TypeDeclarationHeader.OfSchema schema
let attributesAreNullOrDefault = Object.ReferenceEquals(header.Attributes, null) || header.Attributes.IsDefault

let header = if attributesAreNullOrDefault then { header with Attributes = ImmutableArray.Empty } else header // no reason to raise an error
let attributesAreNullOrDefault =
Object.ReferenceEquals(header.Attributes, null) || header.Attributes.IsDefault

let header =
if attributesAreNullOrDefault then { header with Attributes = ImmutableArray.Empty } else header // no reason to raise an error

if not (Object.ReferenceEquals(header.TypeItems, null)) then
success, header
Expand Down Expand Up @@ -319,18 +323,17 @@ type CallableDeclarationHeader =
// due to changes of fields over time are initialized to a proper value
let success, schema = DeclarationHeader.FromJson<CallableDeclarationHeaderSchema> json
let header = CallableDeclarationHeader.OfSchema schema
let attributesAreNullOrDefault = Object.ReferenceEquals(header.Attributes, null) || header.Attributes.IsDefault

let header = if attributesAreNullOrDefault then { header with Attributes = ImmutableArray.Empty } else header // no reason to raise an error
let attributesAreNullOrDefault =
Object.ReferenceEquals(header.Attributes, null) || header.Attributes.IsDefault

let header =
if attributesAreNullOrDefault then { header with Attributes = ImmutableArray.Empty } else header // no reason to raise an error

let header = { header with ArgumentTuple = header.ArgumentTuple |> setInferredInfo }

if
Object.ReferenceEquals
(
header.Signature.Information,
null
)
Object.ReferenceEquals(header.Signature.Information, null)
|| Object.ReferenceEquals(header.Signature.Information.Characteristics, null)
then
false, { header with Signature = { header.Signature with Information = CallableInformation.Invalid } }
Expand Down Expand Up @@ -439,9 +442,12 @@ type SpecializationDeclarationHeader =
let header = SpecializationDeclarationHeader.OfSchema schema
let infoIsNull = Object.ReferenceEquals(header.Information, null)
let typeArgsAreNull = Object.ReferenceEquals(header.TypeArguments, null)
let attributesAreNullOrDefault = Object.ReferenceEquals(header.Attributes, null) || header.Attributes.IsDefault

let header = if attributesAreNullOrDefault then { header with Attributes = ImmutableArray.Empty } else header // no reason to raise an error
let attributesAreNullOrDefault =
Object.ReferenceEquals(header.Attributes, null) || header.Attributes.IsDefault

let header =
if attributesAreNullOrDefault then { header with Attributes = ImmutableArray.Empty } else header // no reason to raise an error

if not (infoIsNull || typeArgsAreNull) then
success, header
Expand Down
3 changes: 2 additions & 1 deletion src/QsCompiler/Core/Dependencies.fs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ type BuiltIn =
Kind = Function(TypeParameters = ImmutableArray.Empty)
}

static member Attribute = { FullName = { Name = "Attribute"; Namespace = BuiltIn.CoreNamespace }; Kind = Attribute }
static member Attribute =
{ FullName = { Name = "Attribute"; Namespace = BuiltIn.CoreNamespace }; Kind = Attribute }

static member EntryPoint =
{ FullName = { Name = "EntryPoint"; Namespace = BuiltIn.CoreNamespace }; Kind = Attribute }
Expand Down
11 changes: 5 additions & 6 deletions src/QsCompiler/Core/SymbolResolution.fs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ module internal ResolutionResult =
let internal TryAtMostOne<'T> (nsGetter: 'T -> string) (results: seq<ResolutionResult<'T>>) : ResolutionResult<'T> =
let found =
results
|> Seq.filter
(function
|> Seq.filter (function
| Found _ -> true
| _ -> false)

Expand Down Expand Up @@ -306,7 +305,8 @@ module SymbolResolution =
let getTarget (att: QsDeclarationAttribute) =
if att |> BuiltIn.MarksTestOperation then Some att.Argument else None

let validTargets = CommandLineArguments.BuiltInSimulators.ToImmutableDictionary(fun t -> t.ToLowerInvariant())
let validTargets =
CommandLineArguments.BuiltInSimulators.ToImmutableDictionary(fun t -> t.ToLowerInvariant())

let targetName (target: string) =
if target = null then
Expand Down Expand Up @@ -548,8 +548,7 @@ module SymbolResolution =
ImmutableArray.Create udtTuple |> QsTuple |> ResolveArgumentTuple(resolveItem, resolveType)

let underlyingType =
argTuple.ResolveWith
(function
argTuple.ResolveWith (function
| Anonymous t -> t.WithoutRangeInfo
| Named x -> x.Type.WithoutRangeInfo)

Expand Down Expand Up @@ -1148,7 +1147,7 @@ module SymbolResolution =
(properties: ImmutableDictionary<_, _>)
(kind, spec: Resolution<QsSpecializationGenerator, ResolvedGenerator>)
=
let bundle : SpecializationBundleProperties = properties.[SpecializationBundleProperties.BundleId spec]
let bundle = properties.[SpecializationBundleProperties.BundleId spec]

let impl, err =
match kind with
Expand Down
Loading

0 comments on commit c337d8d

Please sign in to comment.