[clang] [Clang] Fix getReturnTypeSourceRange() for trailing return types (PR #219101)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 26 21:15:29 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Younan Zhang (zyn0217)
<details>
<summary>Changes</summary>
I'm not quite sure what that 'self-referential' meant and that logic doesn't seem to make much sense and it doesn't work for trailing return types.
Fixes #<!-- -->162649
---
Full diff: https://github.com/llvm/llvm-project/pull/219101.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+5-2)
- (modified) clang/lib/AST/Decl.cpp (+1-4)
- (modified) clang/test/Sema/warn-main-return-type.c (+3-2)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 8cc8eb5f80066..815e858b85360 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -486,9 +486,9 @@ features cannot lower the translation-unit ABI level;
producing a spurious "no matching function" error with no candidate notes.
(#GH210822)
-- Fixed a crash when module directive export module foo not following a
+- Fixed a crash when module directive export module foo not following a
semicolon and there are no rest pp-tokens in current module file. (#GH187771)
-
+
- Fixed a crash when a lambda parameter pack was given a default argument that
is a pack expansion referencing an enclosing function's parameter pack (e.g.
`[](Types... = args...) {}`). Clang now diagnoses the illegal default
@@ -544,6 +544,9 @@ features cannot lower the translation-unit ABI level;
serialized PCH/AST files and `-Wunused-local-typedef` diagnostics
non-reproducible across runs. (#GH209639)
+- `FunctionDecl::getReturnTypeSourceRange()` now returns correct source
+ location of a trailing return type. (#GH162649)
+
#### Miscellaneous Bug Fixes
#### Miscellaneous Clang Crashes Fixed
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 0894097333d73..c9524dc82588a 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -4070,12 +4070,9 @@ SourceRange FunctionDecl::getReturnTypeSourceRange() const {
if (!FTL)
return SourceRange();
- // Skip self-referential return types.
- const SourceManager &SM = getASTContext().getSourceManager();
SourceRange RTRange = FTL.getReturnLoc().getSourceRange();
SourceLocation Boundary = getNameInfo().getBeginLoc();
- if (RTRange.isInvalid() || Boundary.isInvalid() ||
- !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary))
+ if (RTRange.isInvalid() || Boundary.isInvalid())
return SourceRange();
return RTRange;
diff --git a/clang/test/Sema/warn-main-return-type.c b/clang/test/Sema/warn-main-return-type.c
index 468a5de478e39..307395f70b4c0 100644
--- a/clang/test/Sema/warn-main-return-type.c
+++ b/clang/test/Sema/warn-main-return-type.c
@@ -43,8 +43,9 @@ fptr main(void) {
return (fptr) 0;
}
-// expected-error at +2 {{conflicting types for 'main}}
-// expected-warning at +1 {{return type of 'main' is not 'int'}}
+// expected-error at +3 {{conflicting types for 'main}}
+// expected-warning at +2 {{return type of 'main' is not 'int'}}
+// expected-note at +1 {{change return type to 'int'}}
void *(*main(void))(int a) {
return (fptr) 0;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/219101
More information about the cfe-commits
mailing list