Skip to content

[APINotes] Fix fatal error when using the Type key on fields#180672

Merged
egorzhdan merged 4 commits intollvm:mainfrom
literally-anything:apinotes-field-type
Feb 23, 2026
Merged

[APINotes] Fix fatal error when using the Type key on fields#180672
egorzhdan merged 4 commits intollvm:mainfrom
literally-anything:apinotes-field-type

Conversation

@literally-anything
Copy link
Contributor

This fixes a clang crash when importing a module with apinotes that sets the Type key on a Field:

# .---command stderr------------
# | API notes allowed a type on an unknown declaration
# | UNREACHABLE executed at <path>/llvm-project/clang/lib/Sema/SemaAPINotes.cpp:419!
# | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
# | Stack dump:
# | 0.  <path>/llvm-project/clang/test/APINotes/Inputs/Headers/Fields.h:4:12: current parser token ';'
# | 1.  <path>/llvm-project/clang/test/APINotes/Inputs/Headers/Fields.h:3:1: parsing struct/union/class body 'IntWrapper'
...
# |  #8 0x0000000109540868 clang::Sema::ApplyAPINotesType(clang::Decl*, llvm::StringRef)
# |  #9 0x000000010955c6ec applyAPINotesType(clang::Sema&, clang::Decl*, llvm::StringRef, (anonymous namespace)::VersionedInfoMetadata)
Tags:
- Name: IntWrapper
  Fields:
  - Name: value
    Type: ValueType  # Fails because of this

Previously, the case that the Decl passed to ApplyAPINotesType could be a FieldDecl was overlooked.

struct Field {
  …
  StringRef Type;
  ...
}
...
template <> struct MappingTraits<Field> {
  static void mapping(IO &IO, Field &F) {
    …
    IO.mapOptional("Type", F.Type, StringRef(""));  // ‘Type' is already a valid key in apinotes files.
    ….
  }
}

This adds the case to handle FieldDecls.

@egorzhdan @compnerd

literally-anything and others added 3 commits February 9, 2026 22:59
When using a Type key on fields of structs or clases, clang hits a fatal error: "llvm_unreachable("API notes allowed a type on an unknown declaration")".
This commit fixes that by adding the missing case to the if statement for FieldDecl.
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 10, 2026
@llvmbot
Copy link
Member

llvmbot commented Feb 10, 2026

@llvm/pr-subscribers-clang

Author: Hunter Baker (literally-anything)

Changes

This fixes a clang crash when importing a module with apinotes that sets the Type key on a Field:

# .---command stderr------------
# | API notes allowed a type on an unknown declaration
# | UNREACHABLE executed at &lt;path&gt;/llvm-project/clang/lib/Sema/SemaAPINotes.cpp:419!
# | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
# | Stack dump:
# | 0.  &lt;path&gt;/llvm-project/clang/test/APINotes/Inputs/Headers/Fields.h:4:12: current parser token ';'
# | 1.  &lt;path&gt;/llvm-project/clang/test/APINotes/Inputs/Headers/Fields.h:3:1: parsing struct/union/class body 'IntWrapper'
...
# |  #<!-- -->8 0x0000000109540868 clang::Sema::ApplyAPINotesType(clang::Decl*, llvm::StringRef)
# |  #<!-- -->9 0x000000010955c6ec applyAPINotesType(clang::Sema&amp;, clang::Decl*, llvm::StringRef, (anonymous namespace)::VersionedInfoMetadata)
Tags:
- Name: IntWrapper
  Fields:
  - Name: value
    Type: ValueType  # Fails because of this

Previously, the case that the Decl passed to ApplyAPINotesType could be a FieldDecl was overlooked.

struct Field {
  …
  StringRef Type;
  ...
}
...
template &lt;&gt; struct MappingTraits&lt;Field&gt; {
  static void mapping(IO &amp;IO, Field &amp;F) {
    …
    IO.mapOptional("Type", F.Type, StringRef(""));  // ‘Type' is already a valid key in apinotes files.
    ….
  }
}

This adds the case to handle FieldDecls.

@egorzhdan @compnerd


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

4 Files Affected:

  • (modified) clang/lib/Sema/SemaAPINotes.cpp (+6)
  • (modified) clang/test/APINotes/Inputs/Headers/Fields.apinotes (+1)
  • (modified) clang/test/APINotes/Inputs/Headers/Fields.h (+2)
  • (modified) clang/test/APINotes/fields.cpp (+1-1)
diff --git a/clang/lib/Sema/SemaAPINotes.cpp b/clang/lib/Sema/SemaAPINotes.cpp
index 059eb0dd767b7..12929895b3789 100644
--- a/clang/lib/Sema/SemaAPINotes.cpp
+++ b/clang/lib/Sema/SemaAPINotes.cpp
@@ -409,6 +409,12 @@ void Sema::ApplyAPINotesType(Decl *D, StringRef TypeString) {
                                           property->getType(), Type)) {
           property->setType(Type, TypeInfo);
         }
+      } else if (auto field = dyn_cast<FieldDecl>(D)) {
+        if (!checkAPINotesReplacementType(*this, field->getLocation(),
+                                          field->getType(), Type)) {
+          field->setType(Type);
+          field->setTypeSourceInfo(TypeInfo);
+        }
       } else {
         llvm_unreachable("API notes allowed a type on an unknown declaration");
       }
diff --git a/clang/test/APINotes/Inputs/Headers/Fields.apinotes b/clang/test/APINotes/Inputs/Headers/Fields.apinotes
index 931da52ba29d1..44545ab8c7d27 100644
--- a/clang/test/APINotes/Inputs/Headers/Fields.apinotes
+++ b/clang/test/APINotes/Inputs/Headers/Fields.apinotes
@@ -4,6 +4,7 @@ Tags:
 - Name: IntWrapper
   Fields:
   - Name: value
+    Type: ValueType
     Availability: none
     AvailabilityMsg: "oh no"
 - Name: Outer
diff --git a/clang/test/APINotes/Inputs/Headers/Fields.h b/clang/test/APINotes/Inputs/Headers/Fields.h
index dbd342da47789..cc2bc5814c00a 100644
--- a/clang/test/APINotes/Inputs/Headers/Fields.h
+++ b/clang/test/APINotes/Inputs/Headers/Fields.h
@@ -1,3 +1,5 @@
+enum class ValueType {};
+
 struct IntWrapper {
   int value;
 
diff --git a/clang/test/APINotes/fields.cpp b/clang/test/APINotes/fields.cpp
index 8dea2229b4e0a..1522f098634cd 100644
--- a/clang/test/APINotes/fields.cpp
+++ b/clang/test/APINotes/fields.cpp
@@ -6,7 +6,7 @@
 #include "Fields.h"
 
 // CHECK-FIELD: Dumping IntWrapper::value:
-// CHECK-FIELD-NEXT: FieldDecl {{.+}} value
+// CHECK-FIELD-NEXT: FieldDecl {{.+}} value 'ValueType'
 // CHECK-FIELD: UnavailableAttr {{.+}} <<invalid sloc>> "oh no"
 
 // CHECK-DEEP-FIELD: Dumping Outer::Inner::value:

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@egorzhdan egorzhdan merged commit 20bdcbd into llvm:main Feb 23, 2026
10 checks passed
@github-actions
Copy link

@literally-anything Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

HendrikHuebner pushed a commit to HendrikHuebner/llvm-project that referenced this pull request Mar 10, 2026
…0672)

This fixes a clang crash when importing a module with apinotes that sets
the Type key on a Field:
```
# .---command stderr------------
# | API notes allowed a type on an unknown declaration
# | UNREACHABLE executed at <path>/llvm-project/clang/lib/Sema/SemaAPINotes.cpp:419!
# | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
# | Stack dump:
# | 0.  <path>/llvm-project/clang/test/APINotes/Inputs/Headers/Fields.h:4:12: current parser token ';'
# | 1.  <path>/llvm-project/clang/test/APINotes/Inputs/Headers/Fields.h:3:1: parsing struct/union/class body 'IntWrapper'
...
# |  llvm#8 0x0000000109540868 clang::Sema::ApplyAPINotesType(clang::Decl*, llvm::StringRef)
# |  llvm#9 0x000000010955c6ec applyAPINotesType(clang::Sema&, clang::Decl*, llvm::StringRef, (anonymous namespace)::VersionedInfoMetadata)
```
```yaml
Tags:
- Name: IntWrapper
  Fields:
  - Name: value
    Type: ValueType  # Fails because of this
```

Previously, the case that the Decl passed to `ApplyAPINotesType` could
be a `FieldDecl` was overlooked.
```cpp
struct Field {
  …
  StringRef Type;
  ...
}
...
template <> struct MappingTraits<Field> {
  static void mapping(IO &IO, Field &F) {
    …
    IO.mapOptional("Type", F.Type, StringRef(""));  // ‘Type' is already a valid key in apinotes files.
    ….
  }
}
```
This adds the case to handle `FieldDecl`s.

@egorzhdan @compnerd
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.

4 participants