microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
744 stars 245 forks source link

AA0137 "Unused variable" warning given for used variable. #5341

Closed rdebath closed 4 years ago

rdebath commented 5 years ago

Describe the bug Warning [AA0137] is emitted for a variable that is being used.

To Reproduce

                ApplicationArea = Basic, Suite;
                CaptionClass = Format(StrSubstNo(Wobbly, Format(WorkDate)));
                Caption = 'Balance Due (LCY)';

Then define the variable as a text constant. In addition different errors are produced if the variable's name is Text000. A message that the variable is inaccessible due to it's protection level and a crash registered against app.json. This is in a page, not a page extension.

Analyzer 'Microsoft.Dynamics.Nav.CodeCop.Design.Rule131StringParamsMustMatchPlaceholders' threw an exception of type 'System.InvalidCastException' with message 'System.InvalidCastException: Unable to cast object of type 'Microsoft.Dynamics.Nav.CodeAnalysis.Symbols.SimpleNavTypeSymbol' to type 'Microsoft.Dynamics.Nav.CodeAnalysis.ITextConstTypeSymbol'.
   at Microsoft.Dynamics.Nav.CodeCop.Design.Rule131StringParamsMustMatchPlaceholders.GetExpressionParamValue(IOperation expression) in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeCop\Design\Rule131StringParamsMustMatchPlaceholders.cs:line 109
   at Microsoft.Dynamics.Nav.CodeCop.Design.Rule131StringParamsMustMatchPlaceholders.AnalyzeInvocation(OperationAnalysisContext ctx) in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeCop\Design\Rule131StringParamsMustMatchPlaceholders.cs:line 75
   at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__DisplayClass53_1.<ExecuteOperationAction>b__1() in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 763
   at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock(DiagnosticAnalyzer analyzer, Action analyze, Nullable`1 info) in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 1079'

Expected behavior Text constant is being used so warning isn't correct.

Screenshots image

image

image

5. Versions: Name: AL Language Id: ms-dynamics-smb.al Description: AL development tools for Dynamics 365 Business Central Version: 4.0.182565 Publisher: Microsoft VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-dynamics-smb.al

thpeder commented 5 years ago

Hi @rdebath,

With the following code I'm able to get the "Unused variables" warning, however I can't get the stack trace to show. Could you provide a some code that reproduces that issue.

page 50100 PageOne
{
    ApplicationArea = Basic, Suite;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(GroupName)
            {
                field(TextField; 'SomeField')
                {
                    ApplicationArea = Basic, Suite;
                    CaptionClass = Format(StrSubstNo(NormalText, Format(WorkDate)));
                }
                field(LabelField; 'SomeField')
                {
                    ApplicationArea = Basic, Suite;
                    CaptionClass = Format(StrSubstNo(LabelTxt, Format(WorkDate)));
                }
                field(TextConstField; 'SomeField')
                {
                    ApplicationArea = Basic, Suite;
                    CaptionClass = Format(StrSubstNo(TextConstTxt, Format(WorkDate)));
                }
                field(Text000Field; 'SomeField')
                {
                    ApplicationArea = Basic, Suite;
                    CaptionClass = Format(StrSubstNo(Text000, Format(WorkDate)));
                }
            }
        }
    }

    var
        NormalText: Text;
        LabelTxt: Label 'My # Label';
        TextConstTxt: TextConst JPN = 'My # Label';
        Text000: Label 'My # Label'; // From comment below: Comment this variable out to reproduce the crash.
        WorkDate: Date;
}
rdebath commented 5 years ago

After deleting almost everything from the project with the problem I finally realised where the weird "protection level" error was coming from. It appears that the MS/AL compiler thinks that globals in the SourceTable of a page are in some way accessible to the page sometimes. So if you add

SourceTable = Customer;

to the page you'll get the crash with Text000.

thpeder commented 5 years ago

I just tried adding the SourceTable = Customer; to the code and noticed that Text000 is a global variable of type Label defined on the customer table, so that is inaccessible by design, but it doesn't crash for me. Are you using the latest version from the marketplace?

rdebath commented 5 years ago

Yup, I'm still on the same version of the AL extension. The crash doesn't seem to depend on the version of the symbols (below is V13, previous are on V15 and runtime 4.0).

I've disabled all other extensions in this workspace; no change. The version of vscode was updated; no change.

When I comment out and save your altest.al the "protection level" error and the exception against app.json both appear at the same time.

Name: AL Language
Id: ms-dynamics-smb.al
Description: AL development tools for Dynamics 365 Business Central
Version: 4.0.182565
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-dynamics-smb.al
{
  "id": "1fdb0e4e-82b1-4605-b4ff-86be4fe05b9c",
  "name": "ZZZ Hello World",
  "publisher": "TVision (Temp)",
  "version": "2.0.0.0",
  "features": [
    "TranslationFile"
  ],

  "idRanges": [{ "from": 50000, "to": 99900}],

  "application": "13.0.0.0",
  "platform": "13.0.0.0",
  "runtime": "3.0"
}
{
    "al.enableCodeAnalysis": true,
    "al.codeAnalyzers": [
         "${CodeCop}"
    ]
}
{
    "version": "0.2.0",

    "configurations": [
        {
            "type": "al",
            "name": "BC on Navdev",
            "authentication": "Windows",
            "server": "http://navdev.tvt.local",
            "serverInstance": "BC130",
            "port": 7049,
            "request": "launch",
            "startupObjectType": "Page",
            "startupObjectId": 2500,
            "schemaUpdateMode": "ForceSync"
        }
    ]
}
thpeder commented 5 years ago

I got it now. I forgot to comment out the declaration in the current object. Thanks for reporting the bug.

atoader commented 4 years ago

We were not able to reproduce this issue on the latest insider build. Could you verify and reopen the issue if it still occurs?