From d840fec4f05a8ba2a79c4fe5e07afcd2202bc25f Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 5 Dec 2026 15:04:04 +0000 Subject: [PATCH] A private or protected member used from outside its class is one error, in tsc's words `t.v` of `constructor(private v: number)` gave four errors: "Class member "v" is not accessable", then "can't resolve property/field/base", "Class member 'v' can't be found" and "Can't resolve property 'v' of type T". Now it is "Property 'v' is private and only accessible within class 'T'" (protected: "... and its subclasses") for fields, methods, static fields, stores and `t?.v`, and the lookups after it say nothing more of that access. "Class constructor is not accessible" is spelled right. Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/MLIRLogic/MLIRCodeLogic.h | 42 ++++++++++++++- tslang/lib/TypeScript/MLIRGenAccessCall.cpp | 54 ++++++++++++++++--- tslang/lib/TypeScript/MLIRGenImpl.h | 32 ++++++++++- tslang/test/tester/CMakeLists.txt | 25 ++++++++- .../private_field_optional_chain.ts | 2 + .../access-errors/private_field_store.ts | 2 + .../tester/access-errors/private_method.ts | 2 + .../private_parameter_property.ts | 2 + .../access-errors/private_static_field.ts | 2 + .../tester/access-errors/protected_field.ts | 2 + 10 files changed, 152 insertions(+), 13 deletions(-) create mode 100644 tslang/test/tester/access-errors/private_field_optional_chain.ts create mode 100644 tslang/test/tester/access-errors/private_field_store.ts create mode 100644 tslang/test/tester/access-errors/private_method.ts create mode 100644 tslang/test/tester/access-errors/private_parameter_property.ts create mode 100644 tslang/test/tester/access-errors/private_static_field.ts create mode 100644 tslang/test/tester/access-errors/protected_field.ts diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h index 5004ae84d..2b29fb627 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h @@ -1861,8 +1861,46 @@ class MLIRPropertyAccessCodeLogic mlir::Value argument; CompileOptions& compileOptions; llvm::ScopedHashTable *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 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) + { + 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 @@ -2175,7 +2213,7 @@ class MLIRPropertyAccessCodeLogic } if (accessingFromLevel < accessLevel) { - emitError(location, "Class member ") << fieldId << " is not accessable"; + notAccessible(tupleType, accessLevel); return mlir::Value(); } @@ -2498,7 +2536,7 @@ class MLIRPropertyAccessCodeLogic } if (accessingFromLevel < accessLevel) { - emitError(location, "Class member ") << fieldId << " is not accessable"; + notAccessible(classStorageType, accessLevel); return mlir::Value(); } diff --git a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp index a34c8e484..2527ccf70 100644 --- a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp +++ b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp @@ -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(); } @@ -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(value.getType())) @@ -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); } @@ -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); }) @@ -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(); } @@ -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"; } @@ -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(); } @@ -1485,7 +1523,7 @@ namespace mlirgen } } - if (isSuperClass || genContext.allowPartialResolve) + if (isSuperClass || genContext.allowPartialResolve || accessDeniedAt(location)) { return mlir::Value(); } @@ -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(); } @@ -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(); } @@ -7161,7 +7199,7 @@ function __gpu_sort_T(__a: T[]) { { auto accessingFromLevel = detectAccessLevel(mlir::cast(effectiveThisValue.getType()), genContext); if (accessingFromLevel < classInfo->constructorAccessLevel) { - emitError(location, "Class constructor is not accessable"); + emitError(location, "Class constructor is not accessible"); return mlir::failure(); } diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 520798ce9..f311caab5 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -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(fieldInfo.id); + notAccessible(location, fieldName ? fieldName.getValue() : mlir::StringRef("?"), fieldInfo.accessLevel, classInfo); return mlir::Value(); } @@ -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(); } @@ -25303,6 +25326,11 @@ class MLIRGenImpl // of restoring them when the nested closure's generation finishes. llvm::ScopedHashTable 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 deniedAccessAt; + std::string deniedAccessMessage; + NamespaceInfo::TypePtr rootNamespace; NamespaceInfo::TypePtr currentNamespace; diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index c55b8e0dc..46553b93a 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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 $ --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 diff --git a/tslang/test/tester/access-errors/private_field_optional_chain.ts b/tslang/test/tester/access-errors/private_field_optional_chain.ts new file mode 100644 index 000000000..f9beaed66 --- /dev/null +++ b/tslang/test/tester/access-errors/private_field_optional_chain.ts @@ -0,0 +1,2 @@ +class T { private v = 1; } +function main() { const t: T | null = new T(); print(t?.v); } diff --git a/tslang/test/tester/access-errors/private_field_store.ts b/tslang/test/tester/access-errors/private_field_store.ts new file mode 100644 index 000000000..0e5dd05a0 --- /dev/null +++ b/tslang/test/tester/access-errors/private_field_store.ts @@ -0,0 +1,2 @@ +class T { private v = 1; } +function main() { const t = new T(); t.v = 2; } diff --git a/tslang/test/tester/access-errors/private_method.ts b/tslang/test/tester/access-errors/private_method.ts new file mode 100644 index 000000000..883ae2d24 --- /dev/null +++ b/tslang/test/tester/access-errors/private_method.ts @@ -0,0 +1,2 @@ +class T { private m() { return 1; } } +function main() { const t = new T(); print(t.m()); } diff --git a/tslang/test/tester/access-errors/private_parameter_property.ts b/tslang/test/tester/access-errors/private_parameter_property.ts new file mode 100644 index 000000000..fbe5aacb5 --- /dev/null +++ b/tslang/test/tester/access-errors/private_parameter_property.ts @@ -0,0 +1,2 @@ +class T { constructor(private v: number) {} } +function main() { const t = new T(1); print(t.v); } diff --git a/tslang/test/tester/access-errors/private_static_field.ts b/tslang/test/tester/access-errors/private_static_field.ts new file mode 100644 index 000000000..f2dcfaf0b --- /dev/null +++ b/tslang/test/tester/access-errors/private_static_field.ts @@ -0,0 +1,2 @@ +class T { private static s = 1; } +function main() { print(T.s); } diff --git a/tslang/test/tester/access-errors/protected_field.ts b/tslang/test/tester/access-errors/protected_field.ts new file mode 100644 index 000000000..3628b2f62 --- /dev/null +++ b/tslang/test/tester/access-errors/protected_field.ts @@ -0,0 +1,2 @@ +class T { protected v = 1; } +function main() { const t = new T(); print(t.v); }