qsharp-compiler icon indicating copy to clipboard operation
qsharp-compiler copied to clipboard

Using Controlled variant of a function throws "The compiler threw an exception"

Open tcNickolas opened this issue 3 years ago • 1 comments

Describe the bug

Using Controlled variant of a function crashes language server and throws compilation errors "QS7022 The compiler threw an exception." and "QS7106 Unable to serialize the built compilation."

To Reproduce

namespace Quantum.QSharpApplication1 {
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Diagnostics;
    open Microsoft.Quantum.Intrinsic;

    @EntryPoint()
    operation HelloQ () : Unit {
        Controlled DumpMachine(new Qubit[0], ());
    }
}

Expected behavior

I expect the language server not to crash, and the compiler to offer a more specific compilation error, something along the lines "Controlled functor can only be applied to operations".

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • SDK and VS extension version 0.17.2105144881
  • Windows 10, .NET Core SDK 5.0.301 and 3.1.410

tcNickolas avatar Jun 11 '21 00:06 tcNickolas

More details about this bug.

The compiler throws an exception while running global type checking. More specifically, the GetProperties member throws an exception with message "properties cannot be determined".

Here's the call stack:

Microsoft.Quantum.QsCompiler.QsCompilerException: error while running global type checking
QsCompilerException: error while running type checking in background
InvalidOperationException: properties cannot be determined
Stack trace:
   at Microsoft.Quantum.QsCompiler.SyntaxTree.ResolvedCharacteristics.GetProperties() in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\DataStructures\SyntaxTree.fs:line 363
   at Microsoft.Quantum.QsCompiler.SyntaxProcessing.TypeInference.Inference.isSubset(CallableInformation info1, CallableInformation info2) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\SyntaxProcessor\TypeInference\InferenceContext.fs:line 132
   at Microsoft.Quantum.QsCompiler.SyntaxProcessing.TypeInference.InferenceContext.UnifyByOrdering(Ordering ordering, TypeContext types) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\SyntaxProcessor\TypeInference\InferenceContext.fs:line 426
   at Microsoft.Quantum.QsCompiler.SyntaxProcessing.TypeInference.InferenceContext.Unify(ResolvedType expected, ResolvedType actual) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\SyntaxProcessor\TypeInference\InferenceContext.fs:line 347
   at Microsoft.Quantum.QsCompiler.SyntaxProcessing.TypeInference.InferenceContext.ApplyConstraint(Constraint typeConstraint, ResolvedType resolvedType) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\SyntaxProcessor\TypeInference\InferenceContext.fs:line 475
   at Microsoft.Quantum.QsCompiler.SyntaxProcessing.TypeInference.InferenceContext.Constrain(ResolvedType resolvedType, Constraint typeConstraint) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\SyntaxProcessor\TypeInference\InferenceContext.fs:line 369
   at Microsoft.Quantum.QsCompiler.SyntaxProcessing.Expressions.buildCall@751.Invoke(QsExpression callable, QsExpression arg) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\SyntaxProcessor\ExpressionVerification.fs:line 766
   at Microsoft.Quantum.QsCompiler.SyntaxProcessing.Expressions.QsExpression.Resolve(QsExpression this, ScopeContext context, FSharpFunc`2 diagnose) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\SyntaxProcessor\ExpressionVerification.fs:line 809
   at Microsoft.Quantum.QsCompiler.SyntaxProcessing.Statements.resolveExpr(ScopeContext context, QsExpression expr) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\SyntaxProcessor\StatementVerification.fs:line 25
   at Microsoft.Quantum.QsCompiler.SyntaxProcessing.Statements.NewExpressionStatement(QsComments comments, QsLocation location, ScopeContext context, QsExpression expr) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\SyntaxProcessor\StatementVerification.fs:line 58
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.<>c__DisplayClass30_0.<TryBuildExpressionStatement>b__0(QsLocation relPos, ScopeContext ctx) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1353
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.BuildStatement[T](TreeNode node, Func`3 build, ScopeContext context, List`1 diagnostics) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 667
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.TryBuildExpressionStatement(IEnumerator`1 nodes, ScopeContext context, List`1 diagnostics, Boolean& proceed, QsStatement& statement) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1351
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.BuildStatements(IEnumerator`1 nodes, ScopeContext context, List`1 diagnostics) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1392
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.BuildScope(IEnumerator`1 nodes, ScopeContext context, List`1 diagnostics, ImmutableHashSet`1 requiredFunctors) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 622
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.BuildImplicitScope(IEnumerator`1 nodes, ScopeContext context, List`1 diagnostics, ImmutableHashSet`1 requiredFunctors) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 643
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.TryBuildUsingStatement(IEnumerator`1 nodes, ScopeContext context, List`1 diagnostics, Boolean& proceed, QsStatement& statement) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 713
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.BuildStatements(IEnumerator`1 nodes, ScopeContext context, List`1 diagnostics) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1440
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.BuildScope(IEnumerator`1 nodes, ScopeContext context, List`1 diagnostics, ImmutableHashSet`1 requiredFunctors) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 622
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.BuildUserDefinedImplementation(TreeNode root, String sourceFile, QsTuple`1 argTuple, ImmutableHashSet`1 requiredFunctorSupport, ScopeContext context, List`1 diagnostics) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1500
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.<>c__DisplayClass34_0.<BuildSpecializations>g__BuildSpecialization|4(QsSpecializationKind kind, ResolvedSignature signature, QsSpecializationGeneratorKind`1 gen, TreeNode root, Func`2 buildArg, QsComments comments) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1675
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.<>c__DisplayClass34_0.<BuildSpecializations>g__BuildSpec|5(TreeNode root) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1713
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.BuildSpecializations(FragmentTree specsRoot, ResolvedSignature parentSignature, QsTuple`1 argTuple, CompilationUnit compilation, List`1 diagnostics, CancellationToken cancellationToken) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1771
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.<>c__DisplayClass35_1.<RunTypeChecking>g__GetSpecializations|6(KeyValuePair`2 specsRoot) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1859
   at System.Linq.Utilities.<>c__DisplayClass2_0`3.<CombineSelectors>b__0(TSource x)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items)
   at System.Collections.Immutable.ImmutableArray.ToImmutableArray[TSource](IEnumerable`1 items)
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.TypeChecking.RunTypeChecking(CompilationUnit compilation, ImmutableDictionary`2 roots, CancellationToken cancellationToken) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\TypeChecking.cs:line 1902
   at Microsoft.Quantum.QsCompiler.CompilationBuilder.CompilationUnitManager.<>c__DisplayClass43_0.<RunGlobalTypeChecking>b__0() in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\CompilationManager\CompilationUnitManager.cs:line 679
   at Microsoft.Quantum.QsCompiler.QsCompilerError.RaiseOnFailure[T](Func`1 func, String header) in C:\Microsoft\ReposA\qsharp-compiler\src\QsCompiler\DataStructures\ErrorHandling.fs:line 74

cesarzc avatar Feb 08 '22 04:02 cesarzc