This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Ignore nullability qualifiers when deducing types for `auto` and `__auto_type`
Needs ReviewPublic

Authored by ahatanak on Jul 31 2023, 12:30 PM.

Details

Summary

Prior to https://reviews.llvm.org/D110216, the deduced types didn't inherit the nullability qualifiers of the initializer expressions. This patch restores the previous behavior.

See https://developer.apple.com/forums/thread/726000#726000021 for background.

Diff Detail

Event Timeline

ahatanak created this revision.Jul 31 2023, 12:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 31 2023, 12:30 PM
ahatanak requested review of this revision.Jul 31 2023, 12:30 PM
ahatanak added inline comments.Jul 31 2023, 12:36 PM
clang/lib/Sema/SemaTemplateDeduction.cpp
3885

This isn't needed to fix the test cases I added, but I think the nullability qualifiers should be stripped here regardless.

clang/test/SemaCXX/nullability.cpp
150

Note that the template parameter for foo is correctly deduced (i.e., _Nullable is ignored) without this patch.

Also, _Atomic has the same problem, but it's not clear whether we want to strip the qualifiers. See https://github.com/llvm/llvm-project/issues/63659

I understand that this is an expedient fix that silences the warning. However, the fundamental problem is the simplistic implementation of the warning (that it is not flow-sensitive), not the inference of nullability. That is, when we are removing nullability qualifiers, we don't know whether there was a check or not. So while this change removes some false positives, it also removes some true positives.

clang/lib/Sema/SemaTemplateDeduction.cpp
3933

This comment merely duplicates the code. Please add an explanation why it is done.

I think we still need to do something like what this patch is doing (i.e., drop the nullability qualifiers) when deducing types for auto or __auto_type, assuming that's the rule we want and we want to restore the previous behavior.

I agree with you that we should make the warning flow sensitive, but I was thinking we could do that later after fixing the type deduction bug.

clang/lib/Sema/SemaTemplateDeduction.cpp
3933

I guess we were dropping the nullability qualifiers for the same reason we drop cv qualifiers. The new variable declared with auto is a separate variable, so it doesn't inherit the qualifiers the argument type.

Of course, I'm assuming that's the rule we want, but I'm not sure as nullability qualifiers aren't part of the C/C++ standards.

gribozavr2 added inline comments.Aug 4 2023, 5:01 AM
clang/lib/Sema/SemaTemplateDeduction.cpp
3933

I guess we were dropping the nullability qualifiers for the same reason we drop cv qualifiers.

I don't think that argument applies. If the null dereference warning was flow sensitive, we would want to do exactly the opposite - that is, preserve nullability qualifiers during deduction.