@@ -2259,7 +2259,68 @@ The common type is required to be a type that both types have an
2259
2259
2260
2260
### Safety
2261
2261
2262
- > ** TODO:**
2262
+ Carbon's premise is that C++ users can't give up performance to get safety. Even
2263
+ if some isolated users can make that tradeoff, they share code with
2264
+ performance-sensitive users. Any path to safety must preserve performance of C++
2265
+ today. This rules out garbage collection, and many other options. The only well
2266
+ understood mechanism of achieving safety without giving up performance is
2267
+ compile-time safety. The leading example of how to achieve this is Rust.
2268
+
2269
+ The difference between Rust's approach and Carbon's is that Rust starts with
2270
+ safety and Carbons starts with migration. Rust supports interop with C, and
2271
+ there is ongoing work to improve the C++-interop story and develop migration
2272
+ tools. However, there is a large gap in programming models between the two
2273
+ languages, generally requiring a revision to the architecture. So, thus far the
2274
+ common pattern in the Rust community is to "rewrite it in Rust"
2275
+ ([ 1] ( https://deprogrammaticaipsum.com/the-great-rewriting-in-rust/ ) ,
2276
+ [ 2] ( https://unhandledexpression.com/rust/2017/07/10/why-you-should-actually-rewrite-it-in-rust.html ) ,
2277
+ [ 3] ( https://transitiontech.ca/random/RIIR ) ). Carbon's approach is to focus on
2278
+ migration from C++, including seamless interop, and then incrementally improve
2279
+ safety.
2280
+
2281
+ The first impact on Carbon's design to support its safety strategy are the
2282
+ necessary building blocks for this level of compile-time safety. We look at
2283
+ existing languages like Rust and Swift to understand what fundamental
2284
+ capabilities they ended up needing. The two components that stand out are:
2285
+
2286
+ - Expanded type system that includes more semantic information.
2287
+ - More pervasive use of type system abstractions (typically generics).
2288
+
2289
+ For migrating C++ code, we also need the ability to add features and migrate
2290
+ code to use those new features incrementally and over time. This requires
2291
+ designing the language with evolution baked in on day one. This impacts a wide
2292
+ range of features:
2293
+
2294
+ - At the lowest level, a simple and extensible syntax and grammar.
2295
+ - Tools and support for adding and removing APIs.
2296
+ - Scalable migration strategies, including tooling support.
2297
+
2298
+ Rust shows the value of expanded semantic information in the type system such as
2299
+ precise lifetimes. This is hard to do in C++ since it has too many kinds of
2300
+ references and pointers, which increases the complexity in the type system
2301
+ multiplicatively. Carbon is attempting to compress C++'s type variations into
2302
+ just values and [ pointers] ( #pointer-types ) .
2303
+
2304
+ Rust also shows the value of functions parameterized by lifetimes. Since
2305
+ lifetimes are only used to establish safety properties of the code, there is no
2306
+ reason to pay the cost of monomorphization for those parameters. So we need a
2307
+ [ generics system] ( #generics ) that can reason about code before it is
2308
+ instantiated, unlike C++ templates.
2309
+
2310
+ In conclusion, there are two patterns in how Carbon diverges from C++:
2311
+
2312
+ - Simplify and removing things to create space for new safety features. This
2313
+ trivially requires breaking backwards compatibility.
2314
+ - Re-engineer foundations to model and enforce safety. This has complex and
2315
+ difficulty in C++ without first simplifying the language.
2316
+
2317
+ This leads to Carbon's incremental path to safety:
2318
+
2319
+ - Keep your performance, your existing codebase, and your developers.
2320
+ - Adopt Carbon through a scalable, tool-assisted migration from C++.
2321
+ - Address initial, easy safety improvements starting day one.
2322
+ - Shift the Carbon code onto an incremental path towards memory safety over
2323
+ the next decade.
2263
2324
2264
2325
> References: [ Safety strategy] ( /docs/project/principles/safety_strategy.md )
2265
2326
0 commit comments