Skip to content

[clang][bytecode] Fix diagnosing std::construct_at with wrong type #109828

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 1 commit into from
Sep 25, 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
11 changes: 8 additions & 3 deletions clang/lib/AST/ByteCode/Descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,17 @@ Descriptor::Descriptor(const DeclTy &D)
}

QualType Descriptor::getType() const {
if (const auto *E = asExpr())
return E->getType();
if (const auto *D = asValueDecl())
return D->getType();
if (const auto *T = dyn_cast<TypeDecl>(asDecl()))
if (const auto *T = dyn_cast_if_present<TypeDecl>(asDecl()))
return QualType(T->getTypeForDecl(), 0);

// The Source sometimes has a different type than the once
// we really save. Try to consult the Record first.
if (isRecord())
return QualType(ElemRecord->getDecl()->getTypeForDecl(), 0);
if (const auto *E = asExpr())
return E->getType();
llvm_unreachable("Invalid descriptor type");
}

Expand Down
4 changes: 0 additions & 4 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,10 +1296,6 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
if (!InvalidNewDeleteExpr(S, OpPC, E))
return false;

// Assume proper types in std functions.
if (S.Current->isStdFunction())
return true;

const auto *NewExpr = cast<CXXNewExpr>(E);
QualType StorageType = Ptr.getType();

Expand Down
12 changes: 11 additions & 1 deletion clang/test/AST/ByteCode/placement-new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace std {
};
template<typename T, typename ...Args>
constexpr void construct_at(void *p, Args &&...args) {
new (p) T((Args&&)args...); // both-note {{in call to}}
new (p) T((Args&&)args...); // both-note {{in call to}} \
// both-note {{placement new would change type of storage from 'int' to 'float'}}
}
}

Expand Down Expand Up @@ -260,4 +261,13 @@ namespace ConstructAt {
static_assert(ctorFail()); // both-error {{not an integral constant expression}} \
// both-note {{in call to 'ctorFail()'}}


constexpr bool bad_construct_at_type() {
int a;
std::construct_at<float>(&a, 1.0f); // both-note {{in call to}}
return true;
}
static_assert(bad_construct_at_type()); // both-error {{not an integral constant expression}} \
// both-note {{in call}}

}
Loading