diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 4a0659688..a23628769 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -3423,6 +3423,54 @@ class MLIRGenImpl const GenContext &genContext) { auto result = mlir::success(); + + // a marker names parameters by string, `@keeps("p")`: iterateDecorators drops an argument it + // cannot read as a literal, so `@keeps(p)` would name nothing + for (auto modifier : functionLikeDeclarationBaseAST->modifiers) + { + if (modifier != SyntaxKind::Decorator) + { + continue; + } + + auto expr = modifier.as()->expression; + if (expr != SyntaxKind::CallExpression) + { + continue; + } + + auto callExpression = expr.as(); + if (callExpression->expression != SyntaxKind::Identifier) + { + continue; + } + + auto name = MLIRHelper::getName(callExpression->expression.as(), stringAllocator); + if (name != KEEPS_DECORATOR && name != BORROWS_DECORATOR && name != LENDS_DECORATOR && + name != IMMORTALIZES_DECORATOR) + { + continue; + } + + for (auto argExpr : callExpression->arguments) + { + if (argExpr != SyntaxKind::StringLiteral) + { + emitError(loc(argExpr)) << "'@" << name << "' takes the names of parameters as strings: '@" << name << "(\"" + << (argExpr == SyntaxKind::Identifier + ? MLIRHelper::getName(argExpr.as(), stringAllocator) + : argExpr == SyntaxKind::ThisKeyword ? StringRef("this") : StringRef("p")) + << "\")'"; + result = mlir::failure(); + } + } + } + + if (mlir::failed(result)) + { + return result; + } + iterateDecorators(functionLikeDeclarationBaseAST, genContext, [&](StringRef name, SmallVector args) { // `@record` names nothing: it says what `@lends` lends into (OWN_DECLARED_RECORD_ATTR_NAME) if (name == RECORD_DECORATOR) @@ -15349,8 +15397,9 @@ class MLIRGenImpl continue; } + // an identifier that names nothing in scope, `@keeps(p)`, evaluates to no type auto resultType = evaluate(argExpr, genContext); - if (auto litType = dyn_cast(resultType)) + if (auto litType = dyn_cast_if_present(resultType)) { mlir::Attribute value = litType.getValue(); if (auto intAttr = dyn_cast(value)) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 29d7f6c7a..a7c91f24c 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -4741,6 +4741,7 @@ set(own_error_cases "own_err_borrows_not_generator|'@borrows' of 'copyOf' names 'w', but it neither makes a generator whose state borrows its arguments nor returns one" "own_err_borrows_mixed_return|'@borrows' of 'pick' names 'w', but it neither makes a generator whose state borrows its arguments nor returns one" "own_err_marker_unknown_param|'@keeps' names 'd', which is not a parameter of this function" + "own_err_marker_identifier_arg|:8:8: error: '@keeps' takes the names of parameters as strings: '@keeps[(]\"it\"[)]'" "own_err_marker_override|'Derived.set' overrides 'Base.set', and has to say the same '@keeps', '@borrows', '@lends' and '@record'" "own_err_lends_missing|'firstOf' could return a borrow or keep a parameter, but it can be called from another module. '@lends' or '@keeps' would say it in its signature" "own_err_lends_mismatch|'@lends' of 'pick' names 'a', but its body returns a borrow of 'b'" diff --git a/tslang/test/tester/own/own_err_marker_identifier_arg.ts b/tslang/test/tester/own/own_err_marker_identifier_arg.ts new file mode 100644 index 000000000..bd9e9da66 --- /dev/null +++ b/tslang/test/tester/own/own_err_marker_identifier_arg.ts @@ -0,0 +1,16 @@ +// A marker names a parameter by an identifier, not a string: an error at the argument, not a crash. +class Item { + v = 1; +} + +let kept: Item[] = []; + +@keeps(it) +function store(it: Item) { + kept.push(it); +} + +function main() { + store(new Item()); + print(kept.length); +}