Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
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
4 changes: 2 additions & 2 deletions src/QsCompiler/CommandLineTool/LoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Microsoft.Quantum.QsCompiler
public class LoadContext : AssemblyLoadContext
{
public readonly string PathToParentAssembly;
private AssemblyDependencyResolver _Resolver;
private readonly AssemblyDependencyResolver _Resolver;
private readonly HashSet<string> _FallbackPaths;

/// <summary>
Expand Down Expand Up @@ -111,7 +111,7 @@ private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
return path == null ? null : LoadFromAssemblyPath(path);
}

private static ConcurrentBag<LoadContext> Loaded =
private static readonly ConcurrentBag<LoadContext> Loaded =
new ConcurrentBag<LoadContext>();

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/QsCompiler/CompilationManager/CompilationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ internal void UpdateCallables(IEnumerable<QsCallable> updates)
header.Location,
header.Signature,
header.ArgumentTuple,
ImmutableArray.Create<QsSpecialization>(defaultSpec),
ImmutableArray.Create(defaultSpec),
header.Documentation,
QsComments.Empty
);
Expand Down Expand Up @@ -551,8 +551,8 @@ public static ImmutableArray<QsNamespace> NewSyntaxTree(
if (types == null) throw new ArgumentNullException(nameof(types));
var emptyLookup = new NonNullable<string>[0].ToLookup(ns => ns, _ => ImmutableArray<string>.Empty);

string QualifiedName(QsQualifiedName fullName) => $"{fullName.Namespace.Value}.{fullName.Name.Value}";
string ElementName(QsNamespaceElement e) =>
static string QualifiedName(QsQualifiedName fullName) => $"{fullName.Namespace.Value}.{fullName.Name.Value}";
static string ElementName(QsNamespaceElement e) =>
e is QsNamespaceElement.QsCustomType t ? QualifiedName(t.Item.FullName) :
e is QsNamespaceElement.QsCallable c ? QualifiedName(c.Item.FullName) : null;
var namespaceElements = callables.Select(c => (c.FullName.Namespace, QsNamespaceElement.NewQsCallable(c)))
Expand Down Expand Up @@ -679,7 +679,7 @@ LocalDeclarations TryGetLocalDeclarations()
}

if (parentCallable == null) return LocalDeclarations.Empty;
declarations = declarations ?? TryGetLocalDeclarations();
declarations ??= TryGetLocalDeclarations();

Tuple<int, int> AbsolutePosition(QsNullable<Tuple<int, int>> relOffset) =>
relOffset.IsNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Linq;
using Microsoft.Quantum.QsCompiler.CompilationBuilder.DataStructures;
using Microsoft.Quantum.QsCompiler.DataTypes;
using Microsoft.Quantum.QsCompiler.SymbolManagement;
using Microsoft.Quantum.QsCompiler.SyntaxProcessing;
using Microsoft.Quantum.QsCompiler.SyntaxTokens;
using Microsoft.Quantum.QsCompiler.SyntaxTree;
Expand Down Expand Up @@ -143,10 +142,10 @@ internal static bool TryGetReferences(this CompilationUnit compilation, QsQualif
referenceLocations = namespaces.SelectMany(ns =>
{
var locs = IdentifierReferences.Find(fullName, ns, defaultOffset, out var dLoc, limitToSourceFiles);
declLoc = declLoc ?? dLoc;
declLoc ??= dLoc;
return locs;
})
.Distinct().Select(AsLocation).ToArray(); // ToArray is needed here to force the execution before checking declLoc
.Select(AsLocation).ToArray(); // ToArray is needed here to force the execution before checking declLoc
declarationLocation = declLoc == null ? null : AsLocation(declLoc.Item1, declLoc.Item2.Offset, declLoc.Item2.Range);
return true;
}
Expand Down Expand Up @@ -218,16 +217,16 @@ internal static bool TryGetReferences(
.SelectMany(spec =>
spec.Implementation is SpecializationImplementation.Provided impl && spec.Location.IsValue
? IdentifierReferences.Find(definition.Item.Item1, impl.Item2, file.FileName, spec.Location.Item.Offset)
: ImmutableArray<IdentifierReferences.Location>.Empty)
.Distinct().Select(AsLocation);
: ImmutableHashSet<IdentifierReferences.Location>.Empty)
.Select(AsLocation);
}
else // the given position corresponds to a variable declared as part of a specialization declaration or implementation
{
var defStart = DiagnosticTools.GetAbsolutePosition(defOffset, defRange.Item1);
var statements = implementation.StatementsAfterDeclaration(defStart.Subtract(specPos));
var scope = new QsScope(statements.ToImmutableArray(), locals);
var rootOffset = DiagnosticTools.AsTuple(specPos);
referenceLocations = IdentifierReferences.Find(definition.Item.Item1, scope, file.FileName, rootOffset).Distinct().Select(AsLocation);
referenceLocations = IdentifierReferences.Find(definition.Item.Item1, scope, file.FileName, rootOffset).Select(AsLocation);
}
declarationLocation = AsLocation(file.FileName, definition.Item.Item2, defRange);
return true;
Expand Down
30 changes: 9 additions & 21 deletions src/QsCompiler/Core/SymbolTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -899,12 +899,9 @@ and NamespaceManager
/// Compares the accessibility of the parent declaration with the accessibility of the UDT being referenced. If the
/// accessibility of a referenced type is less than the accessibility of the parent, returns a diagnostic using the
/// given error code. Otherwise, returns an empty array.
let checkUdtAccessibility code
(parent : NonNullable<string>, parentAccess)
(udt : UserDefinedType, udtAccess) =
let checkUdtAccessibility code (parent : NonNullable<string>, parentAccess) (udt : UserDefinedType, udtAccess) =
if parentAccess = DefaultAccess && udtAccess = Internal
then [| QsCompilerDiagnostic.Error (code, [udt.Name.Value; parent.Value])
(udt.Range.ValueOr QsCompilerDiagnostic.DefaultRange) |]
then [| QsCompilerDiagnostic.Error (code, [udt.Name.Value; parent.Value]) (udt.Range.ValueOr QsCompilerDiagnostic.DefaultRange) |]
else [||]


Expand Down Expand Up @@ -1032,9 +1029,7 @@ and NamespaceManager
///
/// May throw an exception if the given parent and/or source file is inconsistent with the defined declarations.
/// Throws a NotSupportedException if the QsType to resolve contains a MissingType.
member this.ResolveType (parent : QsQualifiedName, tpNames : ImmutableArray<_>, source : NonNullable<string>)
(qsType : QsType)
: ResolvedType * QsCompilerDiagnostic[] =
member this.ResolveType (parent : QsQualifiedName, tpNames : ImmutableArray<_>, source : NonNullable<string>) (qsType : QsType) =
resolveType (parent, tpNames, source) qsType (fun _ -> [||])

/// Resolves the underlying type as well as all named and unnamed items for the given type declaration in the
Expand All @@ -1048,13 +1043,10 @@ and NamespaceManager
///
/// May throw an exception if the given parent and/or source file is inconsistent with the defined types. Throws an
/// ArgumentException if the given type tuple is an empty QsTuple.
member private this.ResolveTypeDeclaration (fullName, source, modifiers) typeTuple =
member private this.ResolveTypeDeclaration (fullName : QsQualifiedName, source, modifiers) typeTuple =
// Currently, type parameters for UDTs are not supported.
let resolveType qsType =
resolveType
(fullName, ImmutableArray<_>.Empty, source)
qsType
(checkUdtAccessibility ErrorCode.TypeLessAccessibleThanParentType (fullName.Name, modifiers.Access))
let checkAccessibility = checkUdtAccessibility ErrorCode.TypeLessAccessibleThanParentType (fullName.Name, modifiers.Access)
let resolveType qsType = resolveType (fullName, ImmutableArray<_>.Empty, source) qsType checkAccessibility
SymbolResolution.ResolveTypeDeclaration resolveType typeTuple

/// Given the namespace and the name of the callable that the given signature belongs to, as well as its kind and
Expand All @@ -1074,14 +1066,10 @@ and NamespaceManager
///
/// May throw an exception if the given parent and/or source file is inconsistent with the defined callables. Throws
/// an ArgumentException if the given list of characteristics is empty.
member private this.ResolveCallableSignature (parentKind, parentName, source, access)
(signature, specBundleCharacteristics) =
member private this.ResolveCallableSignature (parentKind, parentName : QsQualifiedName, source, access) (signature, specBundleCharacteristics) =
let checkAccessibility = checkUdtAccessibility ErrorCode.TypeLessAccessibleThanParentCallable (parentName.Name, access)
let resolveType tpNames qsType =
let res, errs =
resolveType
(parentName, tpNames, source)
qsType
(checkUdtAccessibility ErrorCode.TypeLessAccessibleThanParentCallable (parentName.Name, access))
let res, errs = resolveType (parentName, tpNames, source) qsType checkAccessibility
if parentKind <> TypeConstructor then res, errs
else res.WithoutRangeInfo, errs // strip positional info for auto-generated type constructors
SymbolResolution.ResolveCallableSignature (resolveType, specBundleCharacteristics) signature
Expand Down
3 changes: 2 additions & 1 deletion src/QsCompiler/Core/TypeTransformation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ type TypeTransformationBase(options : TransformationOptions) =
| ExpressionType.Result -> this.OnResult ()
| ExpressionType.Pauli -> this.OnPauli ()
| ExpressionType.Range -> this.OnRange ()
ResolvedType.New |> Node.BuildOr t transformed
let ResolvedType t = ResolvedType.New (true, t)
ResolvedType |> Node.BuildOr t transformed
28 changes: 10 additions & 18 deletions src/QsCompiler/Tests.Compiler/LinkingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open System
open System.Collections.Generic
open System.Collections.Immutable
open System.IO
open System.Linq
open Microsoft.Quantum.QsCompiler
open Microsoft.Quantum.QsCompiler.CompilationBuilder
open Microsoft.Quantum.QsCompiler.DataTypes
Expand All @@ -27,21 +28,16 @@ type LinkingTests (output:ITestOutputHelper) =

let compilationManager = new CompilationUnitManager(new Action<Exception> (fun ex -> failwith ex.Message))

let getTempFile () =
// The file name needs to end in ".qs" so that it isn't ignored by the References.Headers class during the
// internal renaming tests.
Path.GetRandomFileName () + ".qs" |> Path.GetFullPath |> Uri

// The file name needs to end in ".qs" so that it isn't ignored by the References.Headers class during the internal renaming tests.
let getTempFile () = Path.GetRandomFileName () + ".qs" |> Path.GetFullPath |> Uri
let getManager uri content = CompilationUnitManager.InitializeFileManager(uri, content, compilationManager.PublishDiagnostics, compilationManager.LogException)

let defaultOffset =
{
Offset = DiagnosticTools.AsTuple (Position (0, 0))
Range = QsCompilerDiagnostic.DefaultRange
}

let qualifiedName ns name =
{
let defaultOffset = {
Offset = DiagnosticTools.AsTuple (Position (0, 0))
Range = QsCompilerDiagnostic.DefaultRange
}

let qualifiedName ns name = {
Namespace = NonNullable<_>.New ns
Name = NonNullable<_>.New name
}
Expand All @@ -59,11 +55,7 @@ type LinkingTests (output:ITestOutputHelper) =
let declaration = if obj.ReferenceEquals (references.SharedState.DeclarationLocation, null) then 0 else 1
references.SharedState.Locations.Count + declaration

do
let addOrUpdateSourceFile filePath =
getManager (new Uri(filePath)) (File.ReadAllText filePath)
|> compilationManager.AddOrUpdateSourceFileAsync
|> ignore
do let addOrUpdateSourceFile filePath = getManager (new Uri(filePath)) (File.ReadAllText filePath) |> compilationManager.AddOrUpdateSourceFileAsync |> ignore
Path.Combine ("TestCases", "LinkingTests", "Core.qs") |> Path.GetFullPath |> addOrUpdateSourceFile

static member private ReadAndChunkSourceFile fileName =
Expand Down
Loading