Add an interface for initialization of vars without an explicit initializer (#6934)

When a `var` is not explicitly given an initializer, initialize it in
one of two ways:

* If its type implements the new interface `Core.Default`, call
`Core.Default.Op` to initialize it.
* Otherwise, if its type implements `UnformedInit`, leave it in an
unformed state. For now, this is always an uninitialized state, but that
will change in the future.
* If neither of those apply, the `var` declaration is ill-formed.

This is a step towards implementing leads decision #6739 and proposals
#257 and #5913.

Assisted-by: Gemini 3.1 Pro via Antigravity

---------

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Richard Smith
2026-03-19 23:46:06 +00:00
committed by GitHub
co-authored by Geoff Romer
parent fd2d210c63
commit ce50f181f1
146 changed files with 5226 additions and 3535 deletions
+5 -5
View File
@@ -224,14 +224,14 @@ fn ConvertNullptrConstant() -> Core.Optional(i32*) {
// CHECK:STDOUT: !42 = !DILocation(line: 32, column: 3, scope: !40)
// CHECK:STDOUT: !43 = distinct !DISubprogram(name: "ConvertNullptrConstant", linkageName: "_CConvertNullptrConstant.Main", scope: null, file: !6, line: 35, type: !25, spFlags: DISPFlagDefinition, unit: !5)
// CHECK:STDOUT: !44 = !DILocation(line: 36, column: 3, scope: !43)
// CHECK:STDOUT: !45 = distinct !DISubprogram(name: "Convert", linkageName: "_CConvert.NullptrT.CppCompat.Core:ImplicitAs.83dbfcd16e56a769.Core.b88d1103f417c6d4", scope: null, file: !46, line: 46, type: !47, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !49)
// CHECK:STDOUT: !45 = distinct !DISubprogram(name: "Convert", linkageName: "_CConvert.NullptrT.CppCompat.Core:ImplicitAs.83dbfcd16e56a769.Core.b88d1103f417c6d4", scope: null, file: !46, line: 51, type: !47, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !49)
// CHECK:STDOUT: !46 = !DIFile(filename: "{{.*}}/prelude/types/cpp/nullptr.carbon", directory: "")
// CHECK:STDOUT: !47 = !DISubroutineType(types: !48)
// CHECK:STDOUT: !48 = !{!27, !27}
// CHECK:STDOUT: !49 = !{!50}
// CHECK:STDOUT: !50 = !DILocalVariable(arg: 1, scope: !45, type: !27)
// CHECK:STDOUT: !51 = !DILocation(line: 47, column: 14, scope: !45)
// CHECK:STDOUT: !52 = !DILocation(line: 47, column: 7, scope: !45)
// CHECK:STDOUT: !53 = distinct !DISubprogram(name: "None", linkageName: "_CNone.Optional.Core.95809cda2a4fffc7", scope: null, file: !54, line: 26, type: !25, spFlags: DISPFlagDefinition, unit: !5)
// CHECK:STDOUT: !51 = !DILocation(line: 52, column: 14, scope: !45)
// CHECK:STDOUT: !52 = !DILocation(line: 52, column: 7, scope: !45)
// CHECK:STDOUT: !53 = distinct !DISubprogram(name: "None", linkageName: "_CNone.Optional.Core.95809cda2a4fffc7", scope: null, file: !54, line: 27, type: !25, spFlags: DISPFlagDefinition, unit: !5)
// CHECK:STDOUT: !54 = !DIFile(filename: "{{.*}}/prelude/types/optional.carbon", directory: "")
// CHECK:STDOUT: !55 = !DILocation(line: 27, column: 5, scope: !53)
// CHECK:STDOUT: !55 = !DILocation(line: 28, column: 5, scope: !53)