Skip to content

Commit

Permalink
Tweak merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jeskew committed Jul 18, 2023
1 parent e9a83e8 commit 6012104
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "export",
"kind": "function",
"documentation": {
"kind": "markdown",
"value": "```bicep\nexport(): any\n\n```\n\n"
},
"deprecated": false,
"preselect": false,
"sortText": "3_export",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "export()$0"
}
},
{
"label": "maxLength",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "export",
"kind": "function",
"documentation": {
"kind": "markdown",
"value": "```bicep\nexport(): any\n\n```\n\n"
},
"deprecated": false,
"preselect": false,
"sortText": "3_export",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "export()$0"
}
},
{
"label": "maxLength",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ public void GetBuiltInConfiguration_NoParameter_ReturnsBuiltInConfigurationWithA
"sourceMapping": false,
"userDefinedTypes": false,
"userDefinedFunctions": false,
"prettyPrinting": false,
"prettyPrinting": false,
"testFramework": false,
"assertions": false
"assertions": false,
"compileTimeImports": false
},
"formatting": {
"indentKind": "Space",
Expand Down Expand Up @@ -172,9 +173,10 @@ public void GetBuiltInConfiguration_DisableAllAnalyzers_ReturnsBuiltInConfigurat
"sourceMapping": false,
"userDefinedTypes": false,
"userDefinedFunctions": false,
"prettyPrinting": false,
"prettyPrinting": false,
"testFramework": false,
"assertions": false
"assertions": false,
"compileTimeImports": false
},
"formatting": {
"indentKind": "Space",
Expand Down Expand Up @@ -267,9 +269,10 @@ public void GetBuiltInConfiguration_DisableAnalyzers_ReturnsBuiltInConfiguration
"sourceMapping": false,
"userDefinedTypes": false,
"userDefinedFunctions": false,
"prettyPrinting": false,
"prettyPrinting": false,
"testFramework": false,
"assertions": false
"assertions": false,
"compileTimeImports": false
},
"formatting": {
"indentKind": "Space",
Expand Down Expand Up @@ -593,9 +596,10 @@ public void GetConfiguration_ValidCustomConfiguration_OverridesBuiltInConfigurat
"sourceMapping": false,
"userDefinedTypes": false,
"userDefinedFunctions": false,
"prettyPrinting": false,
"prettyPrinting": false,
"testFramework": false,
"assertions": false
"assertions": false,
"compileTimeImports": false
},
"formatting": {
"indentKind": "Space",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ private static (TypeSymbol result, IReadOnlyList<IDiagnostic> diagnostics) Narro

parsingErrorLookup ??= EmptyDiagnosticLookup.Instance;

var typeManager = new TypeManager(BicepTestConstants.Features, binderMock.Object, fileResolverMock.Object, parsingErrorLookup, Core.Workspaces.BicepSourceFileKind.BicepFile);
var typeManager = new TypeManager(BicepTestConstants.Features, binderMock.Object, fileResolverMock.Object, parsingErrorLookup, StrictMock.Of<ISourceFileLookup>().Object, StrictMock.Of<ISemanticModelLookup>().Object, Core.Workspaces.BicepSourceFileKind.BicepFile);

var diagnosticWriter = ToListDiagnosticWriter.Create();
var result = TypeValidator.NarrowTypeAndCollectDiagnostics(typeManager, binderMock.Object, parsingErrorLookup, diagnosticWriter, expression, targetType);
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core/Semantics/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Bicep.Core.Semantics
{
public class Compilation
public class Compilation : ISemanticModelLookup
{
// Stores semantic model for each source file (map exists for all source files, but semantic model created only when indexed)
private readonly ImmutableDictionary<ISourceFile, Lazy<ISemanticModel>> lazySemanticModelLookup;
Expand Down
11 changes: 11 additions & 0 deletions src/Bicep.Core/Semantics/ISemanticModelLookup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.Core.Workspaces;

namespace Bicep.Core.Semantics;

public interface ISemanticModelLookup
{
public ISemanticModel GetSemanticModel(ISourceFile sourceFile);
}
3 changes: 2 additions & 1 deletion src/Bicep.Core/Semantics/ModuleSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public ModuleSymbol(ISymbolContext context, string name, ModuleDeclarationSyntax
public override SymbolKind Kind => SymbolKind.Module;

public bool TryGetSemanticModel([NotNullWhen(true)] out ISemanticModel? semanticModel, [NotNullWhen(false)] out ErrorDiagnostic? failureDiagnostic)
=> SemanticModelHelper.TryGetSemanticModelForForeignTemplateReference(Context.Compilation,
=> SemanticModelHelper.TryGetSemanticModelForForeignTemplateReference(Context.Compilation.SourceFileGrouping,
DeclaringModule,
b => b.ModuleDeclarationMustReferenceBicepModule(),
Context.Compilation,
out semanticModel,
out failureDiagnostic);

Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core/Semantics/SemanticModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public SemanticModel(Compilation compilation, BicepSourceFile sourceFile, IFileR
SymbolContext = symbolContext;

Binder = new Binder(compilation.NamespaceProvider, features, sourceFile, symbolContext);
TypeManager = new TypeManager(features, Binder, fileResolver, this.ParsingErrorLookup, this.SourceFile.FileKind, Compilation);
TypeManager = new TypeManager(features, Binder, fileResolver, this.ParsingErrorLookup, Compilation.SourceFileGrouping, Compilation, this.SourceFile.FileKind);

// name binding is done
// allow type queries now
Expand Down
9 changes: 5 additions & 4 deletions src/Bicep.Core/Semantics/SemanticModelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ function.DeclaringObject is NamespaceType namespaceType &&
});
}

public static bool TryGetSemanticModelForForeignTemplateReference(Compilation compilation,
public static bool TryGetSemanticModelForForeignTemplateReference(ISourceFileLookup sourceFileLookup,
IForeignTemplateReference reference,
DiagnosticBuilder.ErrorBuilderDelegate onInvalidSourceFileType,
ISemanticModelLookup semanticModelLookup,
[NotNullWhen(true)] out ISemanticModel? semanticModel,
[NotNullWhen(false)] out ErrorDiagnostic? failureDiagnostic)
{
if (compilation.SourceFileGrouping.TryGetErrorDiagnostic(reference) is {} errorBuilder)
if (sourceFileLookup.TryGetErrorDiagnostic(reference) is {} errorBuilder)
{
semanticModel = null;
failureDiagnostic = errorBuilder(DiagnosticBuilder.ForPosition(reference.ReferenceSourceSyntax));
Expand All @@ -59,7 +60,7 @@ public static bool TryGetSemanticModelForForeignTemplateReference(Compilation co

// SourceFileGroupingBuilder should have already visited every module declaration and either recorded a failure or mapped it to a syntax tree.
// So it is safe to assume that this lookup will succeed without throwing an exception.
var sourceFile = compilation.SourceFileGrouping.TryGetSourceFile(reference) ?? throw new InvalidOperationException($"Failed to find source file for module");
var sourceFile = sourceFileLookup.TryGetSourceFile(reference) ?? throw new InvalidOperationException($"Failed to find source file for module");

// when we inevitably add a third language ID,
// the inclusion list style below will prevent the new language ID from being
Expand All @@ -72,7 +73,7 @@ public static bool TryGetSemanticModelForForeignTemplateReference(Compilation co
}

failureDiagnostic = null;
semanticModel = compilation.GetSemanticModel(sourceFile);
semanticModel = semanticModelLookup.GetSemanticModel(sourceFile);
return true;
}
}
Expand Down
35 changes: 6 additions & 29 deletions src/Bicep.Core/Semantics/TestSymbol.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Bicep.Core.Diagnostics;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem;
using Bicep.Core.Workspaces;

namespace Bicep.Core.Semantics
{
Expand All @@ -24,32 +21,12 @@ public TestSymbol(ISymbolContext context, string name, TestDeclarationSyntax dec
public override SymbolKind Kind => SymbolKind.Test;

public bool TryGetSemanticModel([NotNullWhen(true)] out ISemanticModel? semanticModel, [NotNullWhen(false)] out ErrorDiagnostic? failureDiagnostic)
{
if (Context.Compilation.SourceFileGrouping.TryGetErrorDiagnostic(this.DeclaringTest) is {} errorBuilder)
{
semanticModel = null;
failureDiagnostic = errorBuilder(DiagnosticBuilder.ForPosition(DeclaringTest.Path));
return false;
}

// SourceFileGroupingBuilder should have already visited every test declaration and either recorded a failure or mapped it to a syntax tree.
// So it is safe to assume that this lookup will succeed without throwing an exception.
var sourceFile = Context.Compilation.SourceFileGrouping.TryGetSourceFile(this.DeclaringTest) ?? throw new InvalidOperationException($"Failed to find source file for Test");

// when we inevitably add a third language ID,
// the inclusion list style below will prevent the new language ID from being
// automatically allowed to be referenced via module declarations
if (sourceFile is not BicepFile)
{
semanticModel = null;
failureDiagnostic = DiagnosticBuilder.ForPosition(DeclaringTest.Path).TestDeclarationMustReferenceBicepTest();
return false;
}

failureDiagnostic = null;
semanticModel = Context.Compilation.GetSemanticModel(sourceFile);
return true;
}
=> SemanticModelHelper.TryGetSemanticModelForForeignTemplateReference(Context.Compilation.SourceFileGrouping,
DeclaringTest,
b => b.ModuleDeclarationMustReferenceBicepModule(),
Context.Compilation,
out semanticModel,
out failureDiagnostic);

public override IEnumerable<Symbol> Descendants
{
Expand Down
17 changes: 9 additions & 8 deletions src/Bicep.Core/TypeSystem/TypeAssignmentVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ public sealed class TypeAssignmentVisitor : AstVisitor
private readonly IBinder binder;
private readonly IFileResolver fileResolver;
private readonly IDiagnosticLookup parsingErrorLookup;
private readonly ISourceFileLookup sourceFileLookup;
private readonly ISemanticModelLookup semanticModelLookup;
private readonly BicepSourceFileKind fileKind;
private readonly ConcurrentDictionary<SyntaxBase, TypeAssignment> assignedTypes;
private readonly ConcurrentDictionary<FunctionCallSyntaxBase, FunctionOverload> matchedFunctionOverloads;
private readonly ConcurrentDictionary<FunctionCallSyntaxBase, Expression> matchedFunctionResultValues;
private readonly ConcurrentDictionary<CompileTimeImportDeclarationSyntax, ISemanticModel?> importedModels;
private readonly BicepSourceFileKind fileKind;
private readonly Compilation compilation;

public TypeAssignmentVisitor(ITypeManager typeManager, IFeatureProvider features, IBinder binder, IFileResolver fileResolver, IDiagnosticLookup parsingErrorLookup, Workspaces.BicepSourceFileKind fileKind, Compilation compilation)
public TypeAssignmentVisitor(ITypeManager typeManager, IFeatureProvider features, IBinder binder, IFileResolver fileResolver, IDiagnosticLookup parsingErrorLookup, ISourceFileLookup sourceFileLookup, ISemanticModelLookup semanticModelLookup, Workspaces.BicepSourceFileKind fileKind)
{
this.typeManager = typeManager;
this.features = features;
this.binder = binder;
this.fileResolver = fileResolver;
this.parsingErrorLookup = parsingErrorLookup;
this.sourceFileLookup = sourceFileLookup;
this.semanticModelLookup = semanticModelLookup;
this.fileKind = fileKind;
assignedTypes = new();
matchedFunctionOverloads = new();
matchedFunctionResultValues = new();
importedModels = new();
this.fileKind = fileKind;
this.compilation = compilation;
}

private TypeAssignment GetTypeAssignment(SyntaxBase syntax)
Expand Down Expand Up @@ -878,9 +878,10 @@ public override void VisitCompileTimeImportDeclarationSyntax(CompileTimeImportDe
return ErrorType.Empty();
}
if (!SemanticModelHelper.TryGetSemanticModelForForeignTemplateReference(compilation,
if (!SemanticModelHelper.TryGetSemanticModelForForeignTemplateReference(sourceFileLookup,
syntax,
b => b.CompileTimeImportDeclarationMustReferenceTemplate(),
semanticModelLookup,
out var semanticModel,
out var failureDiagnostic))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core/TypeSystem/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public class TypeManager : ITypeManager
private readonly TypeAssignmentVisitor typeAssignmentVisitor;
private readonly DeclaredTypeManager declaredTypeManager;

public TypeManager(IFeatureProvider features, IBinder binder, IFileResolver fileResolver, IDiagnosticLookup parsingErrorLookup, BicepSourceFileKind kind, Compilation compilation)
public TypeManager(IFeatureProvider features, IBinder binder, IFileResolver fileResolver, IDiagnosticLookup parsingErrorLookup, ISourceFileLookup sourceFileLookup, ISemanticModelLookup semanticModelLookup, BicepSourceFileKind kind)
{
// bindings will be modified by name binding after this object is created
// so we can't make an immutable copy here
// (using the IReadOnlyDictionary to prevent accidental mutation)
this.typeAssignmentVisitor = new TypeAssignmentVisitor(this, features, binder, fileResolver, parsingErrorLookup, kind, compilation);
this.typeAssignmentVisitor = new TypeAssignmentVisitor(this, features, binder, fileResolver, parsingErrorLookup, sourceFileLookup, semanticModelLookup, kind);
this.declaredTypeManager = new DeclaredTypeManager(this, binder, features);
}

Expand Down
14 changes: 14 additions & 0 deletions src/Bicep.Core/Workspaces/ISourceFileLookup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.Core.Diagnostics;
using Bicep.Core.Navigation;

namespace Bicep.Core.Workspaces;

public interface ISourceFileLookup
{
public DiagnosticBuilder.ErrorBuilderDelegate? TryGetErrorDiagnostic(IForeignTemplateReference foreignTemplateReference);

public ISourceFile? TryGetSourceFile(IForeignTemplateReference foreignTemplateReference);
}
2 changes: 1 addition & 1 deletion src/Bicep.Core/Workspaces/SourceFileGrouping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public record SourceFileGrouping(
Uri EntryFileUri,
ImmutableDictionary<Uri, FileResolutionResult> FileResultByUri,
ImmutableDictionary<ISourceFile, ImmutableDictionary<IForeignTemplateReference, UriResolutionResult>> UriResultByModule,
ImmutableDictionary<ISourceFile, ImmutableHashSet<ISourceFile>> SourceFileParentLookup)
ImmutableDictionary<ISourceFile, ImmutableHashSet<ISourceFile>> SourceFileParentLookup) : ISourceFileLookup
{
public IEnumerable<ModuleSourceResolutionInfo> GetModulesToRestore()
=> UriResultByModule.SelectMany(
Expand Down
58 changes: 0 additions & 58 deletions src/Bicep.Core/Workspaces/SourceFileGroupingBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,6 @@ private FileResolutionResult PopulateRecursive(Uri fileUri, ModuleReference? mod

fileResultByUri[uriResult.FileUri] = childResult;
}
foreach (var childModule in bicepFile.ProgramSyntax.Declarations.OfType<TestDeclarationSyntax>())
{

var (childModuleReference, uriResult) = GetModuleRestoreResult(fileUri, childModule);

uriResultByModule.GetOrAdd(bicepFile, f => new())[childModule] = uriResult;
if (uriResult.FileUri is null)
{
continue;
}

if (!fileResultByUri.TryGetValue(uriResult.FileUri, out var childResult) ||
(childResult.File is not null && sourceFileToRebuild is not null && sourceFileToRebuild.Contains(childResult.File)))
{
// only recurse if we've not seen this file before - to avoid infinite loops
childResult = PopulateRecursive(uriResult.FileUri, childModuleReference, sourceFileToRebuild);
}

fileResultByUri[uriResult.FileUri] = childResult;
}
break;
}
case BicepParamFile paramsFile:
Expand Down Expand Up @@ -246,44 +226,6 @@ private FileResolutionResult PopulateRecursive(Uri fileUri, ModuleReference? mod
return (moduleReference, new(moduleFileUri, false, null));
}


private (ModuleReference? reference, UriResolutionResult result) GetModuleRestoreResult(Uri parentFileUri, TestDeclarationSyntax module)
{
if (!moduleDispatcher.TryGetModuleReference(module, parentFileUri, out var moduleReference, out var referenceResolutionError))
{
// module reference is not valid
return (null, new(module, null, false, referenceResolutionError));
}

if (!moduleDispatcher.TryGetLocalModuleEntryPointUri(moduleReference, out var moduleFileUri, out var moduleGetPathFailureBuilder))
{
return (moduleReference, new(module, null, false, moduleGetPathFailureBuilder));
}

if (forceModulesRestore)
{
//override the status to force restore
return (moduleReference, new(module, moduleFileUri, true, x => x.ModuleRequiresRestore(moduleReference.FullyQualifiedReference)));
}

var restoreStatus = moduleDispatcher.GetModuleRestoreStatus(moduleReference, out var restoreErrorBuilder);
switch (restoreStatus)
{
case ModuleRestoreStatus.Unknown:
// we have not yet attempted to restore the module, so let's do it
return (moduleReference, new(module, moduleFileUri, true, x => x.ModuleRequiresRestore(moduleReference.FullyQualifiedReference)));
case ModuleRestoreStatus.Failed:
// the module has not yet been restored or restore failed
// in either case, set the error
return (moduleReference, new(module, null, false, restoreErrorBuilder));
default:
break;
}

return (moduleReference, new(module, moduleFileUri, false, null));
}


private ILookup<ISourceFile, ISourceFile> ReportFailuresForCycles()
{
var sourceFileGraph = this.fileResultByUri.Values
Expand Down

0 comments on commit 6012104

Please sign in to comment.