compiler: Keep the entry package index fixed

2ccf2a0715f8d60207f904e29144bfef962fe4db08b3aab8d88c2b7923bf9efd
The package build loop overwrote `entryPkgIdx` with the package it was
compiling, so the field meant the program entry before the loop and the
current package inside it. `compile` and `finishPackage` read it as the
current package, and the resolver log named whichever package the field
held.

Set the entry index once, at its position in the selected package
order, and pass the current package index to `compile` and
`finishPackage`. `runResolver` takes the package it logs. Test runner
selection compares indices, and the startup file comes from the entry's
selected input directly.

Assisted-by: Claude:opus-5.5
Change-Id: I994a2180cd426e1935b5c6995ec2a13c3a2dab93
Alexis Sellier committed ago 1 parent 4d9cca2e
compiler/radiance.rad +12 -5
1011 1011
        return false;
1012 1012
    };
1013 1013
    return true;
1014 1014
}
1015 1015
1016 -
/// Run the resolver on the parsed modules.
1016 +
/// Run the resolver on the parsed modules, logging progress for `pkg`.
1017 1017
/// Its error buffer is permanent storage in `driverArena`. The resolver takes
1018 1018
/// ownership of `mainArena`.
1019 1019
export fn runResolver(
1020 1020
    ctx: &CompileContext,
1021 +
    pkg: &package::Package,
1021 1022
    modules: module::ModuleTable,
1022 1023
    nodeCount: u32,
1023 1024
    mainArena: *heap::Heap,
1024 1025
    driverArena: &heap::Heap
1025 1026
) -> resolver::Resolver throws (Error) {
1026 -
    let entryPkg = try getEntryPackage(ctx);
1027 -
1028 -
    pkgLog(&entryPkg, &["resolving", ".."]);
1027 +
    pkgLog(pkg, &["resolving", ".."]);
1029 1028
1030 1029
    let nodeData = try nodeDataStorage(mainArena, nodeCount);
1031 1030
    // The sentinel is never read: the resolver writes an error before it reads one.
1032 1031
    let mut errors: *mut [resolver::Error] = &mut [];
1033 1032
    try! errors.resize(
1436 1435
    }
1437 1436
    // The module graph is read-only from here on.
1438 1437
    let modules = module::freeze(graph);
1439 1438
1440 1439
    // Run resolution phase. The resolver takes ownership of the main arena.
1440 +
    let entryPkg = try getEntryPackage(&ctx) catch {
1441 +
        return 1;
1442 +
    };
1441 1443
    let res = try runResolver(
1442 -
        &ctx, modules, ctx.parser.arena.nextId, mainArena, driverArena
1444 +
        &ctx,
1445 +
        &entryPkg,
1446 +
        modules,
1447 +
        ctx.parser.arena.nextId,
1448 +
        mainArena,
1449 +
        driverArena
1443 1450
    ) catch {
1444 1451
        return 1;
1445 1452
    };
1446 1453
    let fnArena = heap::over(&mut storage.fnIl[..]);
1447 1454
    let mut backend = BackendStorage {
compiler/radiance/build.rad +31 -15
607 607
    let selected = try inputs(ctx, work.main, sourceArena, work.artifacts) catch {
608 608
        throw super::error(&["cannot read package inputs or dependency manifests"]);
609 609
    };
610 610
    let entryIndex = ctx.entryPkgIdx else panic "run: missing entry package";
611 611
    let entryName = ctx.inputs[entryIndex].name;
612 +
    // Package slots follow the selected order from here on.
613 +
    let mut found: ?u32 = nil;
614 +
    for input, index in selected {
615 +
        if mem::eq(input.options.name, entryName) {
616 +
            set found = index;
617 +
        }
618 +
    }
619 +
    let entry = found else panic "run: entry package not selected";
620 +
    set ctx.entryPkgIdx = entry;
612 621
    let built = try buildPackages(
613 622
        ctx, graph, work, selected, entryName, driverArena, compiler
614 623
    );
615 624
    let case BuiltPackages { packages, native, data, sourcePaths, asmData } = built
616 625
        else panic "run: expected package results";
636 645
    selected: *[Input],
637 646
    entryName: *[u8],
638 647
    driverArena: &heap::Heap,
639 648
    compiler: binary::Id
640 649
) -> BuiltPackages throws (super::Error) {
641 -
    let entry = find(selected, entryName) else panic "buildPackages: missing entry package";
650 +
    let entry = ctx.entryPkgIdx else panic "buildPackages: missing entry package";
642 651
    let initial = try nativeOutput(ctx, selected[entry], work);
643 652
    let case NativeSetup { native: initialNative, asmData } = initial
644 653
        else panic "buildPackages: expected native setup";
645 654
    let mut native = initialNative;
646 655
    let mut allData: *mut [il::Data] = &mut [];
647 656
    let mut sourcePaths: *mut [*[u8]] = &mut [];
648 657
    let mut compiled: *mut [artifact::Package] = &mut [];
649 658
    let mut published: *mut [link::Provider] = &mut [];
650 659
    for input, index in selected {
651 660
        set ctx.inputs[index] = input.options;
661 +
        // Only the current package and its predecessors are registered.
652 662
        set ctx.packageCount = index + 1;
653 -
        set ctx.entryPkgIdx = index;
654 663
        let providerIds = try providers(&input, &selected[.. index], driverArena) catch {
655 664
            throw super::error(&["cannot select package providers"]);
656 665
        };
657 666
        let mut built: artifact::Package = undefined;
658 667
        let mut fresh = false;
664 673
                    ctx,
665 674
                    input.options.name,
666 675
                    &source,
667 676
                    &compiled[..],
668 677
                    providerIds,
669 -
                    ctx.config.buildTest and mem::eq(input.options.name, entryName),
678 +
                    ctx.config.buildTest and index == entry,
670 679
                    compiler
671 680
                )
672 681
                    if ctx.cacheDirectory <> nil or ctx.rilDirectory <> nil
673 682
                    else nil;
674 683
                let mut cached: ?artifact::Package = nil;
695 704
                    try loadInterfaces(ctx, graph, selected, &compiled[..], driverArena);
696 705
                    let embeds = try embedded(source.files, driverArena);
697 706
                    try load(
698 707
                        ctx, graph, index, source.modules, module::Role::Source, providerIds, embeds
699 708
                    );
700 -
                    if ctx.config.buildTest and mem::eq(input.options.name, entryName) {
709 +
                    if ctx.config.buildTest and index == entry {
701 710
                        try super::generateTestRunner(ctx, graph, driverArena);
702 711
                    }
703 712
                    let modules = try table(graph, ctx.parser.arena.heap);
704 713
                    let res = try super::runResolver(
705 -
                        ctx, modules, ctx.parser.arena.nextId, work.main, driverArena
714 +
                        ctx,
715 +
                        &ctx.packages[index],
716 +
                        modules,
717 +
                        ctx.parser.arena.nextId,
718 +
                        work.main,
719 +
                        driverArena
706 720
                    );
707 721
                    if ctx.debug and ctx.outputPath <> nil {
708 722
                        let root = ctx.packages[index].rootModuleId
709 723
                            else panic "buildPackages: missing source root";
710 724
                        assert sourcePaths.len == root as u32, "buildPackages: source module order";
716 730
                            }
717 731
                        }
718 732
                    }
719 733
                    let result = try compile(
720 734
                        ctx,
735 +
                        index,
721 736
                        modules,
722 737
                        res,
723 738
                        &compiled[..],
724 739
                        work,
725 740
                        Request {
726 741
                            declared: providerIds,
727 742
                            providers: &published[..],
728 743
                            key: request,
729 744
                            files: source.files,
730 745
                        },
731 -
                        native,
732 -
                        entryName
746 +
                        native
733 747
                    );
734 748
                    let case Compilation { package, exports, native: generated, data } = result
735 749
                        else panic "buildPackages: expected compilation";
736 750
                    set built = package;
737 751
                    set native = generated;
1010 1024
        }
1011 1025
    }
1012 1026
    return try pretty::interfaceModule(root, &statements[..], arena);
1013 1027
}
1014 1028
1015 -
/// Produce an immutable package image and compiler declaration set.
1029 +
/// Produce an immutable package image and compiler declaration set
1030 +
/// for package slot `index`.
1016 1031
fn compile(
1017 1032
    ctx: &mut super::CompileContext,
1033 +
    index: u32,
1018 1034
    modules: module::ModuleTable,
1019 1035
    res: resolver::Resolver,
1020 1036
    providers: &[artifact::Package],
1021 1037
    work: &mut Workspace,
1022 1038
    selection: Request['providers],
1023 -
    native: locations::Native,
1024 -
    entryName: *[u8]
1039 +
    native: locations::Native
1025 1040
) -> Compilation throws (super::Error) {
1026 -
    let index = ctx.entryPkgIdx else panic "compile: missing package";
1027 1041
    let pkg = ctx.packages[index];
1028 1042
    let mut declarations: *mut [artifact::Module] = &mut [];
1029 1043
    {
1030 1044
        let mut text = try buffer(work.functions, INTERFACE_BYTES);
1031 1045
        for entry in modules.entries {
1094 1108
            try writer::bytes(&mut locationWriter, modules.entries[i].filePath) catch {
1095 1109
                throw super::Error::Other;
1096 1110
            };
1097 1111
        }
1098 1112
    }
1113 +
    let entryIndex = ctx.entryPkgIdx else panic "compile: missing entry package";
1099 1114
    let mut sink = locations::Sink {
1100 1115
        ril: initial,
1101 1116
        locations: locationWriter,
1102 1117
        firstModule: root,
1103 1118
        native,
1104 -
        entry: mem::eq(pkg.name, entryName),
1119 +
        entry: index == entryIndex,
1105 1120
    };
1106 1121
    try super::lowerPackage(ctx, modules, &mut low, &pkg, true, &mut sink);
1107 1122
    return try finishPackage(
1108 -
        ctx, low, sink, &declarations[..], providers, work, selection
1123 +
        ctx, index, low, sink, &declarations[..], providers, work, selection
1109 1124
    );
1110 1125
}
1111 1126
1112 -
/// Encode the tables, metadata pins, and artifact descriptors after a package's functions are consumed.
1127 +
/// Encode the tables, metadata pins, and artifact descriptors after the
1128 +
/// functions of package slot `index` are consumed.
1113 1129
fn finishPackage(
1114 1130
    ctx: &super::CompileContext,
1131 +
    index: u32,
1115 1132
    low: lower::Lowerer,
1116 1133
    sink: locations::Sink,
1117 1134
    declarations: *[artifact::Module],
1118 1135
    providers: &[artifact::Package],
1119 1136
    work: &mut Workspace,
1120 1137
    selection: Request['providers]
1121 1138
) -> Compilation throws (super::Error) {
1122 1139
    let mut lowering = low;
1123 -
    let index = ctx.entryPkgIdx else panic "finishPackage: missing package";
1124 1140
    let pkg = ctx.packages[index];
1125 1141
    let case Request { declared, key: request, files, .. } = selection
1126 1142
        else panic "finishPackage: expected source request";
1127 1143
    let mut packageFiles: *mut [artifact::File] = &mut [];
1128 1144
    try packageFiles.extend(files, work.artifacts) catch {