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
+35 -35
View File
@@ -219,74 +219,74 @@ fn F() {
// CHECK:STDOUT: !33 = !DILocation(line: 27, column: 7, scope: !21)
// CHECK:STDOUT: !34 = !DILocation(line: 29, column: 14, scope: !21)
// CHECK:STDOUT: !35 = !DILocation(line: 29, column: 7, scope: !21)
// CHECK:STDOUT: !36 = distinct !DISubprogram(name: "HasValue", linkageName: "_CHasValue.Optional.Core.b5e6acce74484d77", scope: null, file: !37, line: 32, type: !38, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !40)
// CHECK:STDOUT: !36 = distinct !DISubprogram(name: "HasValue", linkageName: "_CHasValue.Optional.Core.b5e6acce74484d77", scope: null, file: !37, line: 33, type: !38, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !40)
// CHECK:STDOUT: !37 = !DIFile(filename: "{{.*}}/prelude/types/optional.carbon", directory: "")
// CHECK:STDOUT: !38 = !DISubroutineType(types: !39)
// CHECK:STDOUT: !39 = !{!17, !17}
// CHECK:STDOUT: !40 = !{!41}
// CHECK:STDOUT: !41 = !DILocalVariable(arg: 1, scope: !36, type: !17)
// CHECK:STDOUT: !42 = !DILocation(line: 33, column: 12, scope: !36)
// CHECK:STDOUT: !43 = !DILocation(line: 33, column: 5, scope: !36)
// CHECK:STDOUT: !44 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.Optional.Core.b5e6acce74484d77", scope: null, file: !37, line: 35, type: !14, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !45)
// CHECK:STDOUT: !42 = !DILocation(line: 34, column: 12, scope: !36)
// CHECK:STDOUT: !43 = !DILocation(line: 34, column: 5, scope: !36)
// CHECK:STDOUT: !44 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.Optional.Core.b5e6acce74484d77", scope: null, file: !37, line: 36, type: !14, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !45)
// CHECK:STDOUT: !45 = !{!46}
// CHECK:STDOUT: !46 = !DILocalVariable(arg: 1, scope: !44, type: !17)
// CHECK:STDOUT: !47 = !DILocation(line: 36, column: 12, scope: !44)
// CHECK:STDOUT: !48 = !DILocation(line: 36, column: 5, scope: !44)
// CHECK:STDOUT: !49 = distinct !DISubprogram(name: "Op", linkageName: "_COp.Int.9452f4c51951679b.Core:Inc.Core.be1e879c1ad406d8", scope: null, file: !50, line: 360, type: !51, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !53)
// CHECK:STDOUT: !47 = !DILocation(line: 37, column: 12, scope: !44)
// CHECK:STDOUT: !48 = !DILocation(line: 37, column: 5, scope: !44)
// CHECK:STDOUT: !49 = distinct !DISubprogram(name: "Op", linkageName: "_COp.Int.9452f4c51951679b.Core:Inc.Core.be1e879c1ad406d8", scope: null, file: !50, line: 363, type: !51, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !53)
// CHECK:STDOUT: !50 = !DIFile(filename: "{{.*}}/prelude/types/int.carbon", directory: "")
// CHECK:STDOUT: !51 = !DISubroutineType(types: !52)
// CHECK:STDOUT: !52 = !{null, !16}
// CHECK:STDOUT: !53 = !{!54}
// CHECK:STDOUT: !54 = !DILocalVariable(arg: 1, scope: !49, type: !16)
// CHECK:STDOUT: !55 = !DILocation(line: 362, column: 5, scope: !49)
// CHECK:STDOUT: !56 = !DILocation(line: 360, column: 3, scope: !49)
// CHECK:STDOUT: !57 = distinct !DISubprogram(name: "Some", linkageName: "_CSome.Optional.Core.b5e6acce74484d77", scope: null, file: !37, line: 29, type: !58, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !60)
// CHECK:STDOUT: !55 = !DILocation(line: 365, column: 5, scope: !49)
// CHECK:STDOUT: !56 = !DILocation(line: 363, column: 3, scope: !49)
// CHECK:STDOUT: !57 = distinct !DISubprogram(name: "Some", linkageName: "_CSome.Optional.Core.b5e6acce74484d77", scope: null, file: !37, line: 30, type: !58, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !60)
// CHECK:STDOUT: !58 = !DISubroutineType(types: !59)
// CHECK:STDOUT: !59 = !{!17, !16}
// CHECK:STDOUT: !60 = !{!61}
// CHECK:STDOUT: !61 = !DILocalVariable(arg: 1, scope: !57, type: !16)
// CHECK:STDOUT: !62 = !DILocation(line: 30, column: 12, scope: !57)
// CHECK:STDOUT: !63 = !DILocation(line: 30, column: 5, scope: !57)
// CHECK:STDOUT: !64 = distinct !DISubprogram(name: "None", linkageName: "_CNone.Optional.Core.b5e6acce74484d77", scope: null, file: !37, line: 26, type: !65, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !62 = !DILocation(line: 31, column: 12, scope: !57)
// CHECK:STDOUT: !63 = !DILocation(line: 31, column: 5, scope: !57)
// CHECK:STDOUT: !64 = distinct !DISubprogram(name: "None", linkageName: "_CNone.Optional.Core.b5e6acce74484d77", scope: null, file: !37, line: 27, type: !65, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !65 = !DISubroutineType(types: !66)
// CHECK:STDOUT: !66 = !{!17}
// CHECK:STDOUT: !67 = !DILocation(line: 27, column: 12, scope: !64)
// CHECK:STDOUT: !68 = !DILocation(line: 27, column: 5, scope: !64)
// CHECK:STDOUT: !69 = distinct !DISubprogram(name: "Has", linkageName: "_CHas.3e8267224c5dc9c2:OptionalStorage.Core.bfb441135f07cbe1", scope: null, file: !37, line: 123, type: !38, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !70)
// CHECK:STDOUT: !67 = !DILocation(line: 28, column: 12, scope: !64)
// CHECK:STDOUT: !68 = !DILocation(line: 28, column: 5, scope: !64)
// CHECK:STDOUT: !69 = distinct !DISubprogram(name: "Has", linkageName: "_CHas.3e8267224c5dc9c2:OptionalStorage.Core.bfb441135f07cbe1", scope: null, file: !37, line: 132, type: !38, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !70)
// CHECK:STDOUT: !70 = !{!71}
// CHECK:STDOUT: !71 = !DILocalVariable(arg: 1, scope: !69, type: !17)
// CHECK:STDOUT: !72 = !DILocation(line: 124, column: 12, scope: !69)
// CHECK:STDOUT: !73 = !DILocation(line: 124, column: 5, scope: !69)
// CHECK:STDOUT: !74 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.3e8267224c5dc9c2:OptionalStorage.Core.bfb441135f07cbe1", scope: null, file: !37, line: 126, type: !14, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !75)
// CHECK:STDOUT: !72 = !DILocation(line: 133, column: 12, scope: !69)
// CHECK:STDOUT: !73 = !DILocation(line: 133, column: 5, scope: !69)
// CHECK:STDOUT: !74 = distinct !DISubprogram(name: "Get", linkageName: "_CGet.3e8267224c5dc9c2:OptionalStorage.Core.bfb441135f07cbe1", scope: null, file: !37, line: 135, type: !14, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !75)
// CHECK:STDOUT: !75 = !{!76}
// CHECK:STDOUT: !76 = !DILocalVariable(arg: 1, scope: !74, type: !17)
// CHECK:STDOUT: !77 = !DILocation(line: 127, column: 12, scope: !74)
// CHECK:STDOUT: !78 = !DILocation(line: 127, column: 5, scope: !74)
// CHECK:STDOUT: !79 = distinct !DISubprogram(name: "Op", linkageName: "_COp:thunk.Int.9452f4c51951679b.Core:AddAssignWith.e2da7149b7fcf782.Core.23ea0ff7335d4fdc", scope: null, file: !50, line: 296, type: !80, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !82)
// CHECK:STDOUT: !77 = !DILocation(line: 136, column: 12, scope: !74)
// CHECK:STDOUT: !78 = !DILocation(line: 136, column: 5, scope: !74)
// CHECK:STDOUT: !79 = distinct !DISubprogram(name: "Op", linkageName: "_COp:thunk.Int.9452f4c51951679b.Core:AddAssignWith.e2da7149b7fcf782.Core.23ea0ff7335d4fdc", scope: null, file: !50, line: 299, type: !80, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !82)
// CHECK:STDOUT: !80 = !DISubroutineType(types: !81)
// CHECK:STDOUT: !81 = !{null, !16, !16}
// CHECK:STDOUT: !82 = !{!83, !84}
// CHECK:STDOUT: !83 = !DILocalVariable(arg: 1, scope: !79, type: !16)
// CHECK:STDOUT: !84 = !DILocalVariable(arg: 2, scope: !79, type: !16)
// CHECK:STDOUT: !85 = !DILocation(line: 4294967295, scope: !79)
// CHECK:STDOUT: !86 = !DILocation(line: 296, column: 3, scope: !79)
// CHECK:STDOUT: !87 = distinct !DISubprogram(name: "Some", linkageName: "_CSome.3e8267224c5dc9c2:OptionalStorage.Core.bfb441135f07cbe1", scope: null, file: !37, line: 115, type: !58, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !88)
// CHECK:STDOUT: !86 = !DILocation(line: 299, column: 3, scope: !79)
// CHECK:STDOUT: !87 = distinct !DISubprogram(name: "Some", linkageName: "_CSome.3e8267224c5dc9c2:OptionalStorage.Core.bfb441135f07cbe1", scope: null, file: !37, line: 122, type: !58, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !88)
// CHECK:STDOUT: !88 = !{!89}
// CHECK:STDOUT: !89 = !DILocalVariable(arg: 1, scope: !87, type: !16)
// CHECK:STDOUT: !90 = !DILocation(line: 119, column: 5, scope: !87)
// CHECK:STDOUT: !91 = !DILocation(line: 120, column: 5, scope: !87)
// CHECK:STDOUT: !92 = !DILocation(line: 121, column: 5, scope: !87)
// CHECK:STDOUT: !93 = distinct !DISubprogram(name: "None", linkageName: "_CNone.3e8267224c5dc9c2:OptionalStorage.Core.bfb441135f07cbe1", scope: null, file: !37, line: 110, type: !65, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !94 = !DILocation(line: 112, column: 5, scope: !93)
// CHECK:STDOUT: !95 = !DILocation(line: 113, column: 5, scope: !93)
// CHECK:STDOUT: !96 = distinct !DISubprogram(name: "Convert", linkageName: "_CConvert.93210ed0b06d6c25:ImplicitAs.11da3bb3c3dce533.Core.5da21217b3d19eab", scope: null, file: !50, line: 59, type: !97, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !99)
// CHECK:STDOUT: !90 = !DILocation(line: 128, column: 5, scope: !87)
// CHECK:STDOUT: !91 = !DILocation(line: 129, column: 5, scope: !87)
// CHECK:STDOUT: !92 = !DILocation(line: 130, column: 5, scope: !87)
// CHECK:STDOUT: !93 = distinct !DISubprogram(name: "None", linkageName: "_CNone.3e8267224c5dc9c2:OptionalStorage.Core.bfb441135f07cbe1", scope: null, file: !37, line: 116, type: !65, spFlags: DISPFlagDefinition, unit: !2)
// CHECK:STDOUT: !94 = !DILocation(line: 119, column: 5, scope: !93)
// CHECK:STDOUT: !95 = !DILocation(line: 120, column: 5, scope: !93)
// CHECK:STDOUT: !96 = distinct !DISubprogram(name: "Convert", linkageName: "_CConvert.93210ed0b06d6c25:ImplicitAs.11da3bb3c3dce533.Core.5da21217b3d19eab", scope: null, file: !50, line: 62, type: !97, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !99)
// CHECK:STDOUT: !97 = !DISubroutineType(types: !98)
// CHECK:STDOUT: !98 = !{!16, !16}
// CHECK:STDOUT: !99 = !{!100}
// CHECK:STDOUT: !100 = !DILocalVariable(arg: 1, scope: !96, type: !16)
// CHECK:STDOUT: !101 = !DILocation(line: 61, column: 17, scope: !96)
// CHECK:STDOUT: !102 = !DILocation(line: 61, column: 5, scope: !96)
// CHECK:STDOUT: !103 = distinct !DISubprogram(name: "AsInt", linkageName: "_CAsInt.Int.9452f4c51951679b.Core:AnyInt.Core.be1e879c1ad406d8", scope: null, file: !50, line: 54, type: !97, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !104)
// CHECK:STDOUT: !101 = !DILocation(line: 64, column: 17, scope: !96)
// CHECK:STDOUT: !102 = !DILocation(line: 64, column: 5, scope: !96)
// CHECK:STDOUT: !103 = distinct !DISubprogram(name: "AsInt", linkageName: "_CAsInt.Int.9452f4c51951679b.Core:AnyInt.Core.be1e879c1ad406d8", scope: null, file: !50, line: 57, type: !97, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !104)
// CHECK:STDOUT: !104 = !{!105}
// CHECK:STDOUT: !105 = !DILocalVariable(arg: 1, scope: !103, type: !16)
// CHECK:STDOUT: !106 = !DILocation(line: 54, column: 36, scope: !103)
// CHECK:STDOUT: !106 = !DILocation(line: 57, column: 36, scope: !103)