diff --git a/.github/workflows/cmake-test-release-linux.yml b/.github/workflows/cmake-test-release-linux.yml index 19f5cce52..7e8bb5c3b 100644 --- a/.github/workflows/cmake-test-release-linux.yml +++ b/.github/workflows/cmake-test-release-linux.yml @@ -92,10 +92,15 @@ jobs: continue-on-error: false shell: sh # Public resolvers: the runner's own DNS sometimes fails on the Ubuntu mirrors ("Temporary - # failure resolving 'azure.archive.ubuntu.com'") while every other host resolves. + # failure resolving 'azure.archive.ubuntu.com'") while every other host resolves. Even then a + # lookup can drop for a few seconds, longer than apt's own retries, so the whole step retries. run: | printf 'nameserver 8.8.8.8\nnameserver 1.1.1.1\n' | sudo tee /etc/resolv.conf > /dev/null - sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y ninja-build zlib1g-dev libzstd-dev libxml2-dev + for i in 1 2 3 4 5; do + sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y ninja-build zlib1g-dev libzstd-dev libxml2-dev && exit 0 + echo "apt-get failed (attempt $i), retrying in 30 s"; sleep 30 + done + exit 1 # - name: Install Clang 12 # continue-on-error: false diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index eec1e2cc6..584743abb 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -362,10 +362,15 @@ jobs: shell: sh # zlib, zstd and libxml2: the official LLVM package's CMake targets link against them. # Public resolvers: the runner's own DNS sometimes fails on the Ubuntu mirrors ("Temporary - # failure resolving 'azure.archive.ubuntu.com'") while every other host resolves. + # failure resolving 'azure.archive.ubuntu.com'") while every other host resolves. Even then a + # lookup can drop for a few seconds, longer than apt's own retries, so the whole step retries. run: | printf 'nameserver 8.8.8.8\nnameserver 1.1.1.1\n' | sudo tee /etc/resolv.conf > /dev/null - sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y ninja-build libcurl4-openssl-dev zlib1g-dev libzstd-dev libxml2-dev + for i in 1 2 3 4 5; do + sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y ninja-build libcurl4-openssl-dev zlib1g-dev libzstd-dev libxml2-dev && exit 0 + echo "apt-get failed (attempt $i), retrying in 30 s"; sleep 30 + done + exit 1 # - name: Install Clang 12 # continue-on-error: false @@ -561,7 +566,11 @@ jobs: # Public resolvers: see "Install Ninja" above. run: | printf 'nameserver 8.8.8.8\nnameserver 1.1.1.1\n' | sudo tee /etc/resolv.conf > /dev/null - sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y libcurl4-openssl-dev + for i in 1 2 3 4 5; do + sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y libcurl4-openssl-dev && exit 0 + echo "apt-get failed (attempt $i), retrying in 30 s"; sleep 30 + done + exit 1 - name: Clone Default Library shell: bash diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h b/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h index fd4c98e4c..6390d1b56 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h @@ -250,10 +250,48 @@ class MLIRTypeHelper static mlir::Type getMissingValueType(mlir_ts::ValueType valueType) { mlir::SmallVector members{valueType, mlir_ts::NullType::get(valueType.getContext())}; - std::sort(members.begin(), members.end(), [](auto i, auto j) { return i.getAsOpaquePointer() < j.getAsOpaquePointer(); }); + sortUnionMembers(members); return mlir_ts::OptionalType::get(mlir_ts::UnionType::get(valueType.getContext(), members)); } + // The one order of a union's members, so `A | B` and `B | A` are one type. By the members' + // printed text, which is the same in every compiler run: a declaration written out by one run + // (the default library's) has to match the type another run makes of its source. The address + // of a type, used before, differs between runs. Members that print alike are ordered by + // address, as they cannot be told apart in text anyway. + static void sortUnionMembers(mlir::SmallVectorImpl &members) + { + if (members.size() < 2) + { + return; + } + + mlir::SmallVector> keyed; + keyed.reserve(members.size()); + for (auto member : members) + { + std::string text; + llvm::raw_string_ostream os(text); + member.print(os); + os.flush(); + keyed.emplace_back(std::move(text), member); + } + + std::sort(keyed.begin(), keyed.end(), [](const auto &a, const auto &b) { + if (a.first != b.first) + { + return a.first < b.first; + } + + return a.second.getAsOpaquePointer() < b.second.getAsOpaquePointer(); + }); + + for (size_t i = 0; i < members.size(); i++) + { + members[i] = keyed[i].second; + } + } + // Where null of a @value type T is in T itself: the index path, into T's storage, of a pointer no // value of T holds as null - the `this` of a field of an interface type (`{vtable, this}`, null // exactly when the interface is), or a field of a class type - so `T | null` needs no tag: null is @@ -3874,7 +3912,7 @@ class MLIRTypeHelper newTypes.push_back(type); } - std::sort (newTypes.begin(), newTypes.end(), [](auto i, auto j) { return (i.getAsOpaquePointer() < j.getAsOpaquePointer()); }); + sortUnionMembers(newTypes); return isUndefined ? mlir::Type(mlir_ts::OptionalType::get(mlir_ts::UnionType::get(context, newTypes))) @@ -4145,6 +4183,37 @@ class MLIRTypeHelper return false; } + // `valueType` when `literalType` is an object literal (a tuple) with its fields, by name and in + // order, each converting without loss (`{ at: 6 }`'s s32 to Num's number). Kept apart in a union, + // the two have one tag ("object") but not one layout, and a field read from the union takes + // the first member's: right or wrong by the members' order. + mlir_ts::ValueType literalConvertsToValueType(mlir::Type literalType, mlir::Type valueTypeIn) + { + auto valueType = dyn_cast_or_null(valueTypeIn); + if (!valueType) + { + return mlir_ts::ValueType(); + } + + auto storage = dyn_cast(stripValueType(valueType)); + auto literal = dyn_cast_or_null(convertConstTupleTypeToTupleType(literalType)); + if (!storage || !literal || storage.getFields().size() != literal.getFields().size()) + { + return mlir_ts::ValueType(); + } + + for (auto [from, to] : llvm::zip(literal.getFields(), storage.getFields())) + { + auto fromType = stripLiteralType(from.type); + if (from.id != to.id || !(fromType == to.type || canWideTypeWithoutDataLoss(fromType, to.type))) + { + return mlir_ts::ValueType(); + } + } + + return valueType; + } + mlir::Type mergeType(mlir::Location location, mlir::Type existType, mlir::Type currentType, bool& merged) { merged = false; @@ -4178,6 +4247,19 @@ class MLIRTypeHelper return currentType; } + // an object literal of a @value type's fields is that type: `n ?? { at: 6 }` is `Num` + if (auto valueType = literalConvertsToValueType(currentType, existType)) + { + merged = true; + return valueType; + } + + if (auto valueType = literalConvertsToValueType(existType, currentType)) + { + merged = true; + return valueType; + } + // in case of merging integer types with sign/no sign auto mergedInts = false; auto resNewIntType = mergeIntTypes(existType, currentType, mergedInts); diff --git a/tslang/test/tester/value/value_missing_ops.ts b/tslang/test/tester/value/value_missing_ops.ts index 23cb0bce1..556f5a0ac 100644 --- a/tslang/test/tester/value/value_missing_ops.ts +++ b/tslang/test/tester/value/value_missing_ops.ts @@ -56,6 +56,11 @@ function main() { const fallback: Mark = { at: 7, owner: new Owner(7) }; assert((mark(-1) ?? fallback).at == 7); assert((num(-1) ?? num(4)).at == 4 && (num(5) ?? { at: 6 }).at == 5); + // a literal of Num's fields beside a Num is a Num: kept as a union of the two, a field was read + // as the first member's layout, and got 0 when the literal (s32 fields) was ordered first + const five: Num = { at: 5 }; + const pick = five.at > 0; + assert((pick ? five : { at: 6 }).at == 5 && (five || { at: 6 }).at == 5, "a literal beside a Num"); const nothing: Mark | undefined = undefined; assert((nothing ?? fallback).at == 7);