[clang] [Sema] Fast-path simple plain auto deduction in DeduceAutoType (PR #188196)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 24 08:15:59 PDT 2026


================
@@ -5262,6 +5256,24 @@ Sema::DeduceAutoType(TypeLoc Type, Expr *Init, QualType &Result,
 
     DeducedType = getDecltypeForExpr(Init);
     assert(!DeducedType.isNull());
+  } else if (!InitList && !AT->isGNUAutoType() && !AT->isConstrained() &&
+             Context.hasSameType(Type.getType(), Context.AutoDeductTy) &&
+             !Init->getType()->isSpecificBuiltinType(BuiltinType::Overload) &&
+             Init->getType().isCanonical() &&
+             !Init->getType()->isObjCObjectPointerType()) {
+    // Fast-path a subset of plain unconstrained `auto` deduction for
+    // non-init-list cases with canonical initializer types. For these cases,
+    // the deduced type can be computed directly from the initializer type by
+    // removing references, applying array/function decay, and dropping
+    // top-level cv-qualifiers.
+    QualType Ty = Init->getType();
+    Ty = Ty.getNonReferenceType();
+
+    if (Ty->isArrayType() || Ty->isFunctionType())
+      Ty = Context.getDecayedType(Ty);
+
+    Ty = Ty.getLocalUnqualifiedType();
----------------
Sirraide wrote:

```suggestion
    Ty = Ty.getUnqualifiedType();
```
This is assuming we want to allow non-canonical types. Otherwise, I think this would give the wrong result for
```c++
using x = const int;
x y = 4;
auto q = y; // Should be 'int', not 'const int'.
```

https://github.com/llvm/llvm-project/pull/188196


More information about the cfe-commits mailing list