Skip to content

[GNU ObjC] Clang 14 regression: class methods always return nil object with ARC and optimization #56952

Closed
@triplef

Description

@triplef

Using using Automatic Reference Counting and -O1 or better optimization using Clang 14.0.5 or 15.0 rc1 causes class methods returning an ObjC object type to always return a null pointer.

This works correctly with Clang 13, or when not using ARC, or when using -O0 optimization. Methods returning C primitive types are not affected.

The following code reproduces the issue:

#include <stdio.h>
#import "objc/runtime.h"

__attribute__((objc_root_class))
@interface TestObj { id isa; }
+ (Class)class;
+ (id)testObj;
+ (const char *)testStr;
+ (int)testNum;
@end

@implementation TestObj
+ (Class)class {
	return self;
}
+ (id)testObj {
	id testObj = @"Test";
	printf("returning testObj: 0x%p\n", testObj);
	return testObj;
}
+ (const char *)testStr {
	return "forty two";
}
+ (int)testNum {
	return 42;
}
@end

int main() {
	id testClass = [TestObj class];
	printf("testClass: %s (%p)\n", class_getName(testClass), testClass);
	TestObj *testObj = [TestObj testObj];
	printf("testObj: 0x%p\n", testObj);
	const char *testStr = [TestObj testStr];
	printf("testStr: %s\n", testStr);
	int testNum = [TestObj testNum];
	printf("testNum: %d\n", testNum);

	return 0;
}

Build command:

"C:\Program Files\LLVM-14\bin\clang" -IC:\libobjc2\include -LC:\libobjc2\lib -fobjc-runtime=gnustep-2.0 -fobjc-arc -fuse-ld=lld -lobjc -O2 -o test.exe test.m

Output:

testClass: TestObj (00007FF7CD7F1158)
returning testObj: 0xA9979F4000000024
testObj: 0x0000000000000000   // <<<< should be 0xA9979F4000000024
testStr: forty two
testNum: 42

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions