This is an archive of the discontinued LLVM Phabricator instance.

[ObjC generics] Fix applying `__kindof` to the type parameter.
ClosedPublic

Authored by vsapsai on Jan 22 2019, 6:19 PM.

Details

Summary

Fixes the warning about incompatible pointer types on assigning to a
subclass of type argument an expression of type __kindof TypeParam.

We already have a mechanism in ASTContext::canAssignObjCInterfaces
that handles ObjCObjectType with __kindof. But it wasn't triggered
because during type substitution __kindof TypeParam was represented as
AttributedType with attribute ObjCKindOf and equivalent type
TypeArg. For assignment type checking we use canonical types so
attributed type was desugared and the attribute was ignored.

The fix is in checking transformed AttributedType and pushing
__kindof down into ObjCObjectType when necessary.

rdar://problem/38514910

Diff Detail

Repository
rC Clang

Event Timeline

vsapsai created this revision.Jan 22 2019, 6:19 PM

I think this is reasonable but Doug was the one who worked on this. I wonder if it also helps with the test cases in rdar://problem/24619481.

I think this is reasonable but Doug was the one who worked on this. I wonder if it also helps with the test cases in rdar://problem/24619481.

Yep, it helps with the test cases in rdar://problem/24619481. As soon as ObjCObjectType has IsKindOf, ASTContext::canAssignObjCInterfaces takes care of everything. It checks that LHS is a superclass of RHS or when RHS has __kindof that LHS is a subclass of RHS. My change doesn't deal with inheritance, so I didn't add those test cases.

vsapsai updated this revision to Diff 183215.Jan 23 2019, 5:12 PM
  • Add a test that __kindof works with type sugar such as typedef.
  • Use early return to reduce nesting. Also it's consistent with the previous early returns.
vsapsai updated this revision to Diff 183216.Jan 23 2019, 5:13 PM

Update diff to ignore parent patch.

Harbormaster completed remote builds in B27229: Diff 183216.
doug.gregor accepted this revision.Jan 23 2019, 9:55 PM

One minor request about regarding a dyn_cast, but this looks like the right approach. Thank you!

clang/lib/AST/Type.cpp
1293 ↗(On Diff #183216)

Either this should be a cast<AttributedType> or the next if should check for !newAttrType. I suspect the latter is safer.

This revision is now accepted and ready to land.Jan 23 2019, 9:55 PM
vsapsai updated this revision to Diff 183338.Jan 24 2019, 10:44 AM
  • Address review comment: don't crash when AttributedType is transformed to another type class.
vsapsai marked 2 inline comments as done.Jan 24 2019, 10:46 AM
vsapsai added inline comments.
clang/lib/AST/Type.cpp
1293 ↗(On Diff #183216)

You are right. Thanks for the catch. Currently it shouldn't happen, as far as I can tell, so no test for the case when AttributedType is transformed to something else.

This revision was automatically updated to reflect the committed changes.
vsapsai marked an inline comment as done.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2019, 5:00 PM
Herald added a subscriber: jdoerfert. · View Herald Transcript