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

Conversation

tbaederr
Copy link
Contributor

We can't make the assumption that types are always fine in std functions.

We can't make the assumption that types are always fine in std
functions.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

We can't make the assumption that types are always fine in std functions.


Full diff: https://github.com/llvm/llvm-project/pull/109828.diff

3 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Descriptor.cpp (+8-3)
  • (modified) clang/lib/AST/ByteCode/Interp.cpp (-4)
  • (modified) clang/test/AST/ByteCode/placement-new.cpp (+11-1)
diff --git a/clang/lib/AST/ByteCode/Descriptor.cpp b/clang/lib/AST/ByteCode/Descriptor.cpp
index 170203fe818775..05ece907af42f4 100644
--- a/clang/lib/AST/ByteCode/Descriptor.cpp
+++ b/clang/lib/AST/ByteCode/Descriptor.cpp
@@ -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");
 }
 
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 8b578ccbeb6792..b9c85626ffa990 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -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();
 
diff --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp
index 7a562adae02a6f..1ff6ff3ac19223 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -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'}}
   }
 }
 
@@ -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}}
+
 }

@tbaederr tbaederr changed the title [clang][bytecode] Fix diagnosting std::construct_at with wrong type [clang][bytecode] Fix diagnosing std::construct_at with wrong type Sep 24, 2024
@tbaederr tbaederr merged commit 4bd3a62 into llvm:main Sep 25, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants