List of [Codeunit X].Set(Index, NewValue, var OldValue) generates C# that does not compile (CS1503: NavCodeunit to NavCodeunitHandle)

Đang mở
#8,373 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Lĩnh vực
backend-api-design

Hướng nghiên cứu

The payload names no repository source files or tests. Start by locating the compiler code that emits the three-argument List.Set call and compare it with the working Dictionary.Set and List.Get output shown in the issue. Confirm the generated call uses the expected NavCodeunitHandle and by-reference wrapper, then add a regression test covering both statement and used-value forms.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Please include the following with each issue:

1. Describe the bug
For a list of codeunits, the three-argument overload Set(Index, NewValue, var OldValue) is emitted with .Target (a NavCodeunit) for both the new value and the var old value, while the platform's ALSet expects a NavCodeunitHandle and a ByRef<NavCodeunitHandle>. The AL compiler reports no diagnostic; the server fails to compile the generated C# and refuses the whole object, so every call into the codeunit fails, not only the Set call. Both forms are affected: the statement (DataError.ThrowError) and the used value (ok := Items.Set(...), DataError.TrapError).

This looks like the same family as #6652 (NavCodeunit instead of NavCodeunitHandle, fixed in 2021 for codeunit return values), but for a different construct.

2. To Reproduce
Steps to reproduce the behavior:

  1. Create an app ("runtime": "15.0") with the two codeunits below.
  2. Publish it to a Business Central 29 sandbox.
  3. Run codeunit 50101 (e.g. add &codeunit=50101 to the web client URL).
  4. Optionally, build with alc /generatecode+ and open bin/COD50101.cs in the .app to see the generated C#.
codeunit 50100 "Repro Item"
{
}

codeunit 50101 "List Set Repro"
{
    trigger OnRun()
    var
        Items: List of [Codeunit "Repro Item"];
        NewItem: Codeunit "Repro Item";
        OldItem: Codeunit "Repro Item";
    begin
        Items.Add(OldItem);
        Items.Set(1, NewItem, OldItem);
    end;
}

3. Expected behavior
The code compiles on the server and Set replaces the element, returning the previous one in OldItem. The compiler already emits the right shape for the neighbouring calls, which compile and work:

// Items.Set(1, NewItem);  (two-argument overload)
this.items.ALSet(DataError.ThrowError, 1, this.newItem);
// ByKey.Set(1, NewItem, OldItem);  (Dictionary of [Integer, Codeunit "Repro Item"])
this.byKey.ALSet(1, this.newItem, new ByRef<NavCodeunitHandle>(() => this.oldItem, setValue => this.oldItem.ALAssign(setValue)));
// Items.Get(1, OldItem);
this.items.ALGet(DataError.ThrowError, 1, new ByRef<NavCodeunitHandle>(() => this.oldItem, setValue => this.oldItem.ALAssign(setValue)));

So List.Set with an old value should be this.items.ALSet(DataError.ThrowError, 1, this.newItem, new ByRef<NavCodeunitHandle>(...)).

4. Actual behavior
Generated C# (from alc /generatecode+):

this.items.ALSet(DataError.ThrowError, 1, this.newItem.Target, this.oldItem.Target);

Error on the server when any method of the codeunit is first run (observed in our app, whose generated code has exactly this line; the repro above generates the same line):

System.InvalidOperationException: C# compilation has failed for the application object CodeUnit_50101.
... error CS1503: Argument 3: cannot convert from 'Microsoft.Dynamics.Nav.Runtime.NavCodeunit' to 'Microsoft.Dynamics.Nav.Runtime.NavCodeunitHandle'
... error CS1503: Argument 4: cannot convert from 'Microsoft.Dynamics.Nav.Runtime.NavCodeunit' to 'Microsoft.Dynamics.Nav.Runtime.ByRef<Microsoft.Dynamics.Nav.Runtime.NavCodeunitHandle>'

Workaround: read the old element with Get(Index) and call the two-argument Set(Index, NewValue).

5. Versions:

  • AL Language: 18.1.2842859 (compiler alc.exe 18.1.43.24897)
  • Visual Studio Code: 1.142.0
  • Business Central: online sandbox, version 29 (platform 29.0.57071.0)
  • List of Visual Studio Code extensions that you have installed: not involved. The repro was built with the command-line compiler (alc.exe from the AL Language extension's bin folder), outside Visual Studio Code.
  • Operating System:
    • Windows
    • Linux
    • MacOS
Final Checklist

Please remember to do the following:

  • Search the issue repository to ensure you are reporting a new issue

  • Reproduce the issue after disabling all extensions except the AL Language extension

  • Simplify your code around the issue to better isolate the problem

Ngôn ngữ chính
PowerShell
Star
891
Fork
287
Merge trung bình
4 giờ 7 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/AL

Tất cả issue của microsoft/AL

Issue tương tự

Thêm issue về Backend & API Design

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.