Skip to content

Commit 4bd3a62

Browse files
authored
[clang][bytecode] Fix diagnosing std::construct_at with wrong type (#109828)
We can't make the assumption that types are always fine in std functions.
1 parent d8f555d commit 4bd3a62

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

clang/lib/AST/ByteCode/Descriptor.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,17 @@ Descriptor::Descriptor(const DeclTy &D)
389389
}
390390

391391
QualType Descriptor::getType() const {
392-
if (const auto *E = asExpr())
393-
return E->getType();
394392
if (const auto *D = asValueDecl())
395393
return D->getType();
396-
if (const auto *T = dyn_cast<TypeDecl>(asDecl()))
394+
if (const auto *T = dyn_cast_if_present<TypeDecl>(asDecl()))
397395
return QualType(T->getTypeForDecl(), 0);
396+
397+
// The Source sometimes has a different type than the once
398+
// we really save. Try to consult the Record first.
399+
if (isRecord())
400+
return QualType(ElemRecord->getDecl()->getTypeForDecl(), 0);
401+
if (const auto *E = asExpr())
402+
return E->getType();
398403
llvm_unreachable("Invalid descriptor type");
399404
}
400405

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,10 +1296,6 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
12961296
if (!InvalidNewDeleteExpr(S, OpPC, E))
12971297
return false;
12981298

1299-
// Assume proper types in std functions.
1300-
if (S.Current->isStdFunction())
1301-
return true;
1302-
13031299
const auto *NewExpr = cast<CXXNewExpr>(E);
13041300
QualType StorageType = Ptr.getType();
13051301

clang/test/AST/ByteCode/placement-new.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ namespace std {
1313
};
1414
template<typename T, typename ...Args>
1515
constexpr void construct_at(void *p, Args &&...args) {
16-
new (p) T((Args&&)args...); // both-note {{in call to}}
16+
new (p) T((Args&&)args...); // both-note {{in call to}} \
17+
// both-note {{placement new would change type of storage from 'int' to 'float'}}
1718
}
1819
}
1920

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

264+
265+
constexpr bool bad_construct_at_type() {
266+
int a;
267+
std::construct_at<float>(&a, 1.0f); // both-note {{in call to}}
268+
return true;
269+
}
270+
static_assert(bad_construct_at_type()); // both-error {{not an integral constant expression}} \
271+
// both-note {{in call}}
272+
263273
}

0 commit comments

Comments
 (0)