Obj-C false positive warning with deprecation and instancetype?

Hi all,

Using clang r147866 on Mac OS X 10.7, the code below causes this warning:

$ clang -fsyntax-only test.m
test.m:18:11: warning: 'initWithContentsOfURL:' is deprecated [-Wdeprecated-declarations]
        return [[[self alloc] initWithContentsOfURL:inURL] autorelease];
                 ^

NSString has a deprecated method initWithContentsOfURL:, but in my own class it's not deprecated. I would have thought that between my own use of instancetype and clang's special casing of alloc that this should not warn. Is it a false positive bug?

Hi all,

Using clang r147866 on Mac OS X 10.7, the code below causes this warning:

$ clang -fsyntax-only test.m
test.m:18:11: warning: 'initWithContentsOfURL:' is deprecated [-Wdeprecated-declarations]
       return [[[self alloc] initWithContentsOfURL:inURL] autorelease];
                ^

NSString has a deprecated method initWithContentsOfURL:, but in my own class it's not deprecated. I would have thought that between my own use of instancetype and clang's special casing of alloc that this should not warn. Is it a false positive bug?

It is not false positive. [self alloc] is of type 'id'. Not knowing which initWithContentsOfURL gets called, clang goes out of its way to issue the deprecated warning so user can take action.
You can make your intention known to clang by type-casting [self alloc].

- Fariborz

If alloc is using instancetype then you shouldn't see this warning. Please file a bug report with the pre-processed file.

- Fariborz

According to clang doc, "+alloc implicitly has a related result type". So clang should be able to determine what the return type is.

-- Jean-Daniel

Hi all,

Using clang r147866 on Mac OS X 10.7, the code below causes this warning:

$ clang -fsyntax-only test.m
test.m:18:11: warning: 'initWithContentsOfURL:' is deprecated [-Wdeprecated-declarations]
    return [[[self alloc] initWithContentsOfURL:inURL] autorelease];
             ^

NSString has a deprecated method initWithContentsOfURL:, but in my own class it's not deprecated. I would have thought that between my own use of instancetype and clang's special casing of alloc that this should not warn. Is it a false positive bug?

It is not false positive. [self alloc] is of type 'id'. Not knowing which initWithContentsOfURL gets called, clang goes out of its way to issue the deprecated warning so user can take action.
You can make your intention known to clang by type-casting [self alloc].

- Fariborz

If alloc is using instancetype then you shouldn't see this warning. Please file a bug report with the pre-processed file.

According to clang doc, "+alloc implicitly has a related result type". So clang should be able to determine what the return type is.

Can you point to the document? This can't be part of the 'clang' doc. 'alloc' need to use instancetype for type deduction purposes. Legacy
frameworks use 'id' for 'alloc' type.

- Fariborz

It's here:

<http://clang.llvm.org/docs/LanguageExtensions.html#objc_instancetype>

"The related result type can also be inferred for some methods. To determine whether a method has an inferred related result type, the first word in the camel-case selector (e.g., "init" in "initWithObjects") is considered....."

This doc is exactly what's lead me to believe this is a false positive.

clang’s instancetype features overrides what is declared in the framework headers. So even if +alloc was declared to return id, it is implicitly changed to return the instancetype instead.

Hi Sean,

As Fariborz suggested, please file a bug so we can dig deeper. For us the preferred place is bugreporter.apple.com, but an LLVM Bugzilla report is fine too. We'll likely need preprocessed source or a reduced test case.

Thanks,
Ted

Here you go:
<http://llvm.org/bugs/show_bug.cgi?id=11746>

(Is Radar really appropriate? The clang in Apple's Xcode doesn't even support instancetype at all, how can I file a bug against something that Apple's product doesn't support? Still, if you want, I'll copy-paste it to Radar too...)

Thanks,

Thanks Sean. A Bugzilla PR is fine. I can clone this to Radar fairly easily.

Can you point to the document? This can’t be part of the ‘clang’ doc. ‘alloc’ need to use instancetype for type deduction purposes. Legacy
frameworks use ‘id’ for ‘alloc’ type.

clang’s instancetype features overrides what is declared in the framework headers. So even if +alloc was declared to return id, it is implicitly changed to return the instancetype instead.

Correct. Forgot about naming convention for +alloc and others implying instancetype type return.

  • Fariborz

IIRC, we've seen this bug before, and the issue is that the related result type inference for message sends to 'self' within class methods is not working properly. Definitely looks like a Clang bug.

  - Doug