Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Decorator>()->expression;
if (expr != SyntaxKind::CallExpression)
{
continue;
}

auto callExpression = expr.as<CallExpression>();
if (callExpression->expression != SyntaxKind::Identifier)
{
continue;
}

auto name = MLIRHelper::getName(callExpression->expression.as<Node>(), 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<Node>(), stringAllocator)
: argExpr == SyntaxKind::ThisKeyword ? StringRef("this") : StringRef("p"))
<< "\")'";
result = mlir::failure();
}
}
}

if (mlir::failed(result))
{
return result;
}

iterateDecorators(functionLikeDeclarationBaseAST, genContext, [&](StringRef name, SmallVector<StringRef> args) {
// `@record` names nothing: it says what `@lends` lends into (OWN_DECLARED_RECORD_ATTR_NAME)
if (name == RECORD_DECORATOR)
Expand Down Expand Up @@ -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<mlir_ts::LiteralType>(resultType))
if (auto litType = dyn_cast_if_present<mlir_ts::LiteralType>(resultType))
{
mlir::Attribute value = litType.getValue();
if (auto intAttr = dyn_cast<mlir::IntegerAttr>(value))
Expand Down
1 change: 1 addition & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand Down
16 changes: 16 additions & 0 deletions tslang/test/tester/own/own_err_marker_identifier_arg.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Loading