Skip to content

[Clang][Sema] Fix bug where operator-> typo corrects in the current instantiation #91972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions clang/lib/Sema/SemaExprMember.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,8 +995,6 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
// arrow operator was used with a dependent non-pointer object expression,
// build a CXXDependentScopeMemberExpr.
if (R.wasNotFoundInCurrentInstantiation() ||
(IsArrow && !BaseExprType->isPointerType() &&
BaseExprType->isDependentType()) ||
(R.getLookupName().getCXXOverloadedOperator() == OO_Equal &&
(SS.isSet() ? SS.getScopeRep()->isDependent()
: BaseExprType->isDependentType())))
Expand Down Expand Up @@ -1322,28 +1320,28 @@ static ExprResult LookupMemberExpr(Sema &S, LookupResult &R,
else if (const ObjCObjectPointerType *Ptr =
BaseType->getAs<ObjCObjectPointerType>())
BaseType = Ptr->getPointeeType();
else if (!BaseType->isDependentType()) {
if (BaseType->isRecordType()) {
// Recover from arrow accesses to records, e.g.:
// struct MyRecord foo;
// foo->bar
// This is actually well-formed in C++ if MyRecord has an
// overloaded operator->, but that should have been dealt with
// by now--or a diagnostic message already issued if a problem
// was encountered while looking for the overloaded operator->.
if (!S.getLangOpts().CPlusPlus) {
S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
<< BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
<< FixItHint::CreateReplacement(OpLoc, ".");
}
IsArrow = false;
} else if (BaseType->isFunctionType()) {
goto fail;
} else {
S.Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
<< BaseType << BaseExpr.get()->getSourceRange();
return ExprError();
else if (BaseType->isFunctionType())
goto fail;
else if (BaseType->isDependentType())
BaseType = S.Context.DependentTy;
else if (BaseType->isRecordType()) {
// Recover from arrow accesses to records, e.g.:
// struct MyRecord foo;
// foo->bar
// This is actually well-formed in C++ if MyRecord has an
// overloaded operator->, but that should have been dealt with
// by now--or a diagnostic message already issued if a problem
// was encountered while looking for the overloaded operator->.
if (!S.getLangOpts().CPlusPlus) {
S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
<< BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
<< FixItHint::CreateReplacement(OpLoc, ".");
}
IsArrow = false;
} else {
S.Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
<< BaseType << BaseExpr.get()->getSourceRange();
return ExprError();
}
}

Expand All @@ -1363,7 +1361,7 @@ static ExprResult LookupMemberExpr(Sema &S, LookupResult &R,
}

// Handle field access to simple records.
if (BaseType->getAsRecordDecl() || BaseType->isDependentType()) {
if (BaseType->getAsRecordDecl()) {
TypoExpr *TE = nullptr;
if (LookupMemberExprInRecord(S, R, BaseExpr.get(), BaseType, OpLoc, IsArrow,
SS, HasTemplateArgs, TemplateKWLoc, TE))
Expand All @@ -1374,6 +1372,9 @@ static ExprResult LookupMemberExpr(Sema &S, LookupResult &R,
// failed, the lookup result will have been cleared--that combined with the
// valid-but-null ExprResult will trigger the appropriate diagnostics.
return ExprResult(TE);
} else if (BaseType->isDependentType()) {
R.setNotFoundInCurrentInstantiation();
return ExprEmpty();
}

// Handle ivar access to Objective-C objects.
Expand Down
27 changes: 27 additions & 0 deletions clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,16 +539,43 @@ namespace N4 {
a->y;
a->f();
a->g();

a->T::x;
a->T::y;
a->T::f();
a->T::g();

// FIXME: 'U' should be a dependent name, and its lookup context should be 'a.operator->()'!
a->U::x; // expected-error {{use of undeclared identifier 'U'}}
a->U::y; // expected-error {{use of undeclared identifier 'U'}}
a->U::f(); // expected-error {{use of undeclared identifier 'U'}}
a->U::g(); // expected-error {{use of undeclared identifier 'U'}}
}

void instantiated(D a) {
a->x;
a->y; // expected-error {{no member named 'y' in 'N4::B'}}
a->f();
a->g(); // expected-error {{no member named 'g' in 'N4::B'}}

a->T::x;
a->T::y; // expected-error {{no member named 'y' in 'N4::B'}}
a->T::f();
a->T::g(); // expected-error {{no member named 'g' in 'N4::B'}}
}
};

template void D<B>::instantiated(D); // expected-note {{in instantiation of}}

template<typename T>
struct Typo {
T *operator->();

void not_instantiated(Typo a) {
a->Not_instantiated;
a->typo;
a->T::Not_instantiated;
a->T::typo;
}
};
} // namespace N4
Loading