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
42 changes: 40 additions & 2 deletions tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -1861,8 +1861,46 @@ class MLIRPropertyAccessCodeLogic
mlir::Value argument;
CompileOptions& compileOptions;
llvm::ScopedHashTable<mlir::Value, mlir::Value> *boundRefMaterializedCache = nullptr;
bool accessDenied = false;
std::string accessDeniedMessage;

// `o.v` of a `private v` read from outside its class: one error, as tsc gives it, and the access is
// marked denied so that the callers do not go on to say they can't resolve the member
template <typename T> void notAccessible(T type, mlir_ts::AccessLevel accessLevel)
{
auto isPrivate = accessLevel == mlir_ts::AccessLevel::Private;
llvm::raw_string_ostream os(accessDeniedMessage);
os << "Property '" << name << "' is " << (isPrivate ? "private" : "protected") << " and only accessible within ";
if constexpr (std::is_same_v<T, mlir_ts::ClassStorageType>)
{
os << "class '" << type.getName().getValue() << "'";
}
else
{
os << "its class";
}

if (!isPrivate)
{
os << " and its subclasses";
}

emitError(location) << accessDeniedMessage;
accessDenied = true;
}

public:
// the member exists but may not be read from here: the error is given (notAccessible)
bool isAccessDenied() const
{
return accessDenied;
}

const std::string &getAccessDeniedMessage() const
{
return accessDeniedMessage;
}

// optional: lets a bound-method property access (e.g. `g.next` on a storage-less
// `const` binding) reuse the ref it materialized on a previous access instead of
// minting a fresh one seeded from the pristine, never-mutated value each time. See
Expand Down Expand Up @@ -2175,7 +2213,7 @@ class MLIRPropertyAccessCodeLogic
}

if (accessingFromLevel < accessLevel) {
emitError(location, "Class member ") << fieldId << " is not accessable";
notAccessible(tupleType, accessLevel);
return mlir::Value();
}

Expand Down Expand Up @@ -2498,7 +2536,7 @@ class MLIRPropertyAccessCodeLogic
}

if (accessingFromLevel < accessLevel) {
emitError(location, "Class member ") << fieldId << " is not accessable";
notAccessible(classStorageType, accessLevel);
return mlir::Value();
}

Expand Down
54 changes: 46 additions & 8 deletions tslang/lib/TypeScript/MLIRGenAccessCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ namespace mlirgen
auto propType = evaluateProperty(location, objectValue, cl.getName().str(), genContext);
if (!propType)
{
// (the trial access ignores its errors: one that found the member private says so here)
if (accessDeniedAt(location))
{
emitError(location) << deniedAccessMessage;
return mlir::failure();
}

emitError(location, "Can't resolve property '") << cl.getName() << "' of type " << to_print(objectValue.getType());
return mlir::failure();
}
Expand Down Expand Up @@ -212,6 +219,14 @@ namespace mlirgen
return weakField(value);
}

// a private field read from outside: its error is given, and nothing else is looked for
if (cl.isAccessDenied())
{
deniedAccessAt = location;
deniedAccessMessage = cl.getAccessDeniedMessage();
return mlir::Value();
}

auto value = ClassMembersAccess(location, objectValue, classType.getName().getValue(), name,
false, argument, accessingFromLevel, genContext);
if (value && isa<mlir_ts::WeakRefType>(value.getType()))
Expand Down Expand Up @@ -305,6 +320,13 @@ namespace mlirgen
return value;
}

if (cl.isAccessDenied())
{
deniedAccessAt = location;
deniedAccessMessage = cl.getAccessDeniedMessage();
return mlir::Value();
}

return ValueClassMembersAccess(location, objectValue, classInfo, name, accessingFromLevel, genContext);
}

Expand Down Expand Up @@ -482,6 +504,13 @@ namespace mlirgen
return value;
}

if (cl.isAccessDenied())
{
deniedAccessAt = location;
deniedAccessMessage = cl.getAccessDeniedMessage();
return mlir::Value();
}

return ClassMembersAccess(location, objectValue,
classStorageType.getName().getValue(), name, true, argument, accessingFromLevel, genContext);
})
Expand Down Expand Up @@ -711,13 +740,22 @@ namespace mlirgen
// and one that does may fail in this very run, before any other run reaches the
// access - a function's return type is inferred this way. Without it the only error
// was what the caller made of no value, "the condition has no value" in an `if` (#442).
// (a member that may not be read from here has said so: notAccessible)
if (genContext.dummyRun || genContext.allowPartialResolve)
{
emitError(location, "Can't resolve property '") << name << "' of type " << to_print(objectValue.getType());
if (!cl.isAccessDenied() && !accessDeniedAt(location))
{
emitError(location, "Can't resolve property '") << name << "' of type " << to_print(objectValue.getType());
}

return mlir::success();
}

emitError(location, "Can't resolve property '") << name << "' of type " << to_print(objectValue.getType());
if (!cl.isAccessDenied() && !accessDeniedAt(location))
{
emitError(location, "Can't resolve property '") << name << "' of type " << to_print(objectValue.getType());
}

return mlir::failure();
}

Expand Down Expand Up @@ -909,7 +947,7 @@ namespace mlirgen

// static field access
auto value = ClassMembersAccess(location, thisValue, classInfo, name, baseClass, argument, accessingFromLevel, genContext);
if (!value)
if (!value && !accessDeniedAt(location))
{
emitError(location, "Class member '") << name << "' can't be found";
}
Expand All @@ -923,7 +961,7 @@ namespace mlirgen
{
auto genericMethodInfo = classInfo->staticGenericMethods[genericMethodIndex];
if (accessingFromLevel < genericMethodInfo.accessLevel) {
emitError(location, "Class member '") << genericMethodInfo.name << "' is not accessable";
notAccessible(location, genericMethodInfo.name, genericMethodInfo.accessLevel, classInfo);
return mlir::Value();
}

Expand Down Expand Up @@ -1485,7 +1523,7 @@ namespace mlirgen
}
}

if (isSuperClass || genContext.allowPartialResolve)
if (isSuperClass || genContext.allowPartialResolve || accessDeniedAt(location))
{
return mlir::Value();
}
Expand All @@ -1510,7 +1548,7 @@ namespace mlirgen
auto &methodInfo = classInfo->methods[methodIndex];
if (accessingFromLevel < methodInfo.accessLevel)
{
emitError(location, "Class member '") << methodInfo.name << "' is not accessable";
notAccessible(location, methodInfo.name, methodInfo.accessLevel, classInfo);
return mlir::Value();
}

Expand Down Expand Up @@ -6706,7 +6744,7 @@ function __gpu_sort_T(__a: T[]) {
auto &constructorInfo = classInfo->methods[constructorIndex];
if (detectAccessLevel(classInfo->classType, genContext) < classInfo->constructorAccessLevel)
{
emitError(location, "Class constructor is not accessable");
emitError(location, "Class constructor is not accessible");
return mlir::failure();
}

Expand Down Expand Up @@ -7161,7 +7199,7 @@ function __gpu_sort_T(__a: T[]) {
{
auto accessingFromLevel = detectAccessLevel(mlir::cast<mlir_ts::ClassType>(effectiveThisValue.getType()), genContext);
if (accessingFromLevel < classInfo->constructorAccessLevel) {
emitError(location, "Class constructor is not accessable");
emitError(location, "Class constructor is not accessible");
return mlir::failure();
}

Expand Down
32 changes: 30 additions & 2 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13770,12 +13770,35 @@ class MLIRGenImpl
return effectiveThisValue;
}

// tsc's words: "Property 'v' is private and only accessible within class 'T'"
void notAccessible(mlir::Location location, mlir::StringRef name, mlir_ts::AccessLevel accessLevel, ClassInfo::TypePtr classInfo)
{
auto isPrivate = accessLevel == mlir_ts::AccessLevel::Private;
deniedAccessMessage.clear();
llvm::raw_string_ostream os(deniedAccessMessage);
os << "Property '" << name << "' is " << (isPrivate ? "private" : "protected") << " and only accessible within class '"
<< classInfo->fullName << "'";
if (!isPrivate)
{
os << " and its subclasses";
}

emitError(location) << deniedAccessMessage;
deniedAccessAt = location;
}

bool accessDeniedAt(mlir::Location location)
{
return deniedAccessAt && *deniedAccessAt == location;
}

mlir::Value ClassStaticFieldAccess(ClassInfo::TypePtr classInfo,
mlir::Location location, mlir::Value thisValue, int staticFieldIndex, mlir_ts::AccessLevel accessingFromLevel, const GenContext &genContext) {

auto fieldInfo = classInfo->staticFields[staticFieldIndex];
if (accessingFromLevel < fieldInfo.accessLevel) {
emitError(location, "Class member ") << fieldInfo.id << " is not accessable";
auto fieldName = mlir::dyn_cast<mlir::StringAttr>(fieldInfo.id);
notAccessible(location, fieldName ? fieldName.getValue() : mlir::StringRef("?"), fieldInfo.accessLevel, classInfo);
return mlir::Value();
}

Expand Down Expand Up @@ -13865,7 +13888,7 @@ class MLIRGenImpl
// a @final method is the one every object that has it runs, whatever its class (TSLANG 48)
finalReceiver = finalReceiver || methodInfo.isFinal;
if (accessingFromLevel < methodInfo.accessLevel) {
emitError(location, "Class member '") << methodInfo.name << "' is not accessable";
notAccessible(location, methodInfo.name, methodInfo.accessLevel, classInfo);
return mlir::Value();
}

Expand Down Expand Up @@ -25303,6 +25326,11 @@ class MLIRGenImpl
// of restoring them when the nested closure's generation finishes.
llvm::ScopedHashTable<mlir::Value, mlir::Value> boundRefMaterializedCache;

// where a member was found but may not be used from there (`t.m()` of a `private m()`): its one error is
// given (notAccessible), and the lookups that find nothing after it say nothing more of that access
std::optional<mlir::Location> deniedAccessAt;
std::string deniedAccessMessage;

NamespaceInfo::TypePtr rootNamespace;

NamespaceInfo::TypePtr currentNamespace;
Expand Down
25 changes: 24 additions & 1 deletion tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6251,9 +6251,32 @@ add_test(NAME test-compile-diagnostic-location-implicit-constructor
"${PROJECT_SOURCE_DIR}/test/tester/diagnostic-location/implicit_constructor.ts"
-o "${CMAKE_CURRENT_BINARY_DIR}/diagnostic-location-implicit-constructor.obj")
set_tests_properties(test-compile-diagnostic-location-implicit-constructor
PROPERTIES PASS_REGULAR_EXPRESSION "implicit_constructor\\.ts:9:1: error: Class constructor is not accessable"
PROPERTIES PASS_REGULAR_EXPRESSION "implicit_constructor\\.ts:9:1: error: Class constructor is not accessible"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed|implicit_constructor\\.ts:1:")

# A private or protected member used from outside its class: one error, in tsc's words. It was "Class member "v" is
# not accessable" and then three errors more of the same access ("can't be found", "Can't resolve property")
set(access_error_cases
"private_parameter_property|private_parameter_property\\.ts:2:45: error: Property 'v' is private and only accessible within class 'T'"
"protected_field|protected_field\\.ts:2:44: error: Property 'v' is protected and only accessible within class 'T' and its subclasses"
"private_method|private_method\\.ts:2:44: error: Property 'm' is private and only accessible within class 'T'"
"private_static_field|private_static_field\\.ts:2:25: error: Property 's' is private and only accessible within class 'T'"
"private_field_store|private_field_store\\.ts:2:38: error: Property 'v' is private and only accessible within class 'T'"
"private_field_optional_chain|private_field_optional_chain\\.ts:2:54: error: Property 'v' is private and only accessible within class 'T'")
foreach(access_error_case ${access_error_cases})
string(FIND "${access_error_case}" "|" access_error_bar)
string(SUBSTRING "${access_error_case}" 0 ${access_error_bar} access_error_name)
math(EXPR access_error_bar "${access_error_bar} + 1")
string(SUBSTRING "${access_error_case}" ${access_error_bar} -1 access_error_message)
add_test(NAME test-compile-access-errors-${access_error_name}
COMMAND $<TARGET_FILE:tslang> --emit=obj --no-default-lib -mm=none
"${PROJECT_SOURCE_DIR}/test/tester/access-errors/${access_error_name}.ts"
-o "${CMAKE_CURRENT_BINARY_DIR}/access-errors-${access_error_name}.obj")
set_tests_properties(test-compile-access-errors-${access_error_name}
PROPERTIES PASS_REGULAR_EXPRESSION "${access_error_message}"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed|can't be found|an't resolve|accessable")
endforeach()

# An enum member after a string member needs an initializer: it took the count of the members
# before it.
add_test(NAME test-compile-diagnostic-location-enum-member-initializer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class T { private v = 1; }
function main() { const t: T | null = new T(); print(t?.v); }
2 changes: 2 additions & 0 deletions tslang/test/tester/access-errors/private_field_store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class T { private v = 1; }
function main() { const t = new T(); t.v = 2; }
2 changes: 2 additions & 0 deletions tslang/test/tester/access-errors/private_method.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class T { private m() { return 1; } }
function main() { const t = new T(); print(t.m()); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class T { constructor(private v: number) {} }
function main() { const t = new T(1); print(t.v); }
2 changes: 2 additions & 0 deletions tslang/test/tester/access-errors/private_static_field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class T { private static s = 1; }
function main() { print(T.s); }
2 changes: 2 additions & 0 deletions tslang/test/tester/access-errors/protected_field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class T { protected v = 1; }
function main() { const t = new T(); print(t.v); }
Loading