Skip to content

Commit 601ae1d

Browse files
authored
Merge branch 'trunk' into TestsForUsingEnum
2 parents fedb8a0 + f022e91 commit 601ae1d

File tree

622 files changed

+16837
-9579
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

622 files changed

+16837
-9579
lines changed

.github/workflows/clangd_tidy.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ jobs:
6666
if: steps.filter.outputs.has_cpp == 'true'
6767
run: ./scripts/create_compdb.py
6868

69+
- name: Build deps for clangd-tidy
70+
if: steps.filter.outputs.has_cpp == 'true'
71+
run: ./scripts/run_bazel.py build //scripts:deps_for_clangd_tidy
72+
6973
- name: Install clangd-tidy
7074
if: steps.filter.outputs.has_cpp == 'true'
7175
run: pip install clangd-tidy==1.1.0.post2

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ If you're already a C++ developer, Carbon should have a gentle learning curve.
169169
It is built out of a consistent set of language constructs that should feel
170170
familiar and be easy to read and understand.
171171

172+
The Carbon code here is hypothetical and meant to show the look and feel of the
173+
language.
174+
172175
C++ code like this:
173176

174177
<a href="docs/images/snippets.md#c">

common/array_stack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class ArrayStack {
8282
// Adds multiple values to the top array on the stack.
8383
auto AppendToTop(llvm::ArrayRef<ValueT> values) -> void {
8484
CARBON_CHECK(!array_offsets_.empty(),
85-
"Must call PushArray before PushValues.");
85+
"Must call PushArray before AppendToTop.");
8686
llvm::append_range(values_, values);
8787
}
8888

common/exe_path.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ namespace Carbon {
1313
// `argv0` is required to be null-terminated.
1414
//
1515
// A simplistic approach -- if the provided string isn't already a valid path,
16-
// we look it up in the PATH environment variable. Doesn't resolve any symlinks
17-
// and if it fails, returns the main executable path.
16+
// we look it up in the PATH environment variable. Doesn't resolve any symlinks.
17+
// If it doesn't find a value based on `argv[0]`, returns the main executable
18+
// path.
1819
auto FindExecutablePath(const char* argv0) -> std::string;
1920

2021
} // namespace Carbon

common/map_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ auto MakeKeyValues(ValueCB value_cb, RangeT&& range, RangeTs&&... ranges)
7777
}
7878
};
7979
add_range(std::forward<RangeT>(range));
80-
(add_range(std::forward<RangeT>(ranges)), ...);
80+
(add_range(std::forward<RangeTs>(ranges)), ...);
8181

8282
return elements;
8383
}

common/raw_hashtable_metadata_group.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ inline auto MetadataGroup::SimdLoad(const uint8_t* metadata, ssize_t index)
10011001
return g;
10021002
}
10031003

1004+
// NOLINTNEXTLINE(readability-non-const-parameter): Mutation is in #if.
10041005
inline auto MetadataGroup::SimdStore(uint8_t* metadata, ssize_t index) const
10051006
-> void {
10061007
#if CARBON_NEON_SIMD_SUPPORT

common/set_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ auto MakeElements(RangeT&& range, RangeTs&&... ranges) {
5151
}
5252
};
5353
add_range(std::forward<RangeT>(range));
54-
(add_range(std::forward<RangeT>(ranges)), ...);
54+
(add_range(std::forward<RangeTs>(ranges)), ...);
5555

5656
return elements;
5757
}

core/io.carbon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import library "prelude";
1212
// TODO: Support printing other types.
1313
// TODO: Consider rewriting using native support once library support exists.
1414
fn Print(x: i32) = "print.int";
15-
fn PrintChar(x: i32) -> i32 = "print.char";
15+
fn PrintChar(x: char) -> i32 = "print.char";
16+
// TODO: Return an `Optional(char)` instead of `i32`.
1617
fn ReadChar() -> i32 = "read.char";
1718

1819
// TODO: Change this to a global constant once they are fully supported.

core/prelude/types/optional.carbon

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,90 @@ package Core library "prelude/types/optional";
66

77
import library "prelude/copy";
88
import library "prelude/destroy";
9+
import library "prelude/operators/as";
910
import library "prelude/types/bool";
11+
import library "prelude/types/maybe_unformed";
12+
13+
// TODO: Decide how to expose this in the public API.
14+
private interface OptionalStorage {
15+
let Type:! type;
16+
fn None() -> Type;
17+
fn Some[self: Self]() -> Type;
18+
fn Has(value: Type) -> bool;
19+
fn Get(value: Type) -> Self;
20+
}
1021

11-
// For now, an `Optional(T)` is stored as a pair of a `bool` and a `T`, with
12-
// the `T` left uninitialized if the `bool` is `false`. This isn't a viable
13-
// approach in the longer term, but is the best we can do for now.
14-
//
15-
// TODO: Revisit this once we have choice types implemented in the toolchain.
16-
//
1722
// TODO: We don't have an approved design for an `Optional` type yet, but it's
1823
// used by the design for `Iterate`. The API here is a placeholder.
19-
class Optional(T:! Copy) {
24+
class Optional(T:! OptionalStorage) {
2025
fn None() -> Self {
21-
returned var me: Self;
22-
me.has_value = false;
23-
return var;
26+
return T.None() as Self;
2427
}
25-
2628
fn Some(value: T) -> Self {
27-
return {.has_value = true, .value = value};
29+
return value.Some() as Self;
30+
}
31+
fn HasValue[self: Self]() -> bool {
32+
return T.Has(self as T.Type);
33+
}
34+
fn Get[self: Self]() -> T {
35+
return T.Get(self as T.Type);
2836
}
2937

30-
fn HasValue[self: Self]() -> bool { return self.has_value; }
31-
fn Get[self: Self]() -> T { return self.value; }
38+
impl T as ImplicitAs(Optional(T)) {
39+
fn Convert[self: T]() -> Optional(T) {
40+
return Some(self);
41+
}
42+
}
3243

33-
private var has_value: bool;
34-
private var value: T;
44+
// TODO: Use `private adapt` or `unsafe adapt` once available.
45+
adapt T.Type;
46+
}
47+
48+
// By default, an `Optional(T)` is stored as a pair of a `bool` and a
49+
// `MaybeUnformed(T)`, with the `MaybeUnformed(T)` left uninitialized if the
50+
// `bool` is `false`.
51+
//
52+
// TODO: Revisit this once we have choice types implemented in the toolchain.
53+
private class DefaultOptionalStorage(T:! Copy) {
54+
var value: MaybeUnformed(T);
55+
var has_value: bool;
56+
}
57+
58+
impl forall [T:! Copy] T as OptionalStorage
59+
where .Type = DefaultOptionalStorage(T) {
60+
fn None() -> DefaultOptionalStorage(T) {
61+
returned var me: DefaultOptionalStorage(T);
62+
me.has_value = false;
63+
return var;
64+
}
65+
fn Some[self: Self]() -> DefaultOptionalStorage(T) {
66+
returned var me: DefaultOptionalStorage(T);
67+
// TODO: Should be:
68+
// me.value = self as MaybeUnformed(T);
69+
me.value unsafe as T = self;
70+
me.has_value = true;
71+
return var;
72+
}
73+
fn Has(value: DefaultOptionalStorage(T)) -> bool {
74+
return value.has_value;
75+
}
76+
fn Get(value: DefaultOptionalStorage(T)) -> T {
77+
return value.value unsafe as T;
78+
}
79+
}
80+
81+
// For pointers, we use a null pointer value as the "None" value. This allows
82+
// `Optional(T*)` to be ABI-compatible with a C++ nullable pointer.
83+
final impl forall [T:! type] T* as OptionalStorage
84+
where .Type = MaybeUnformed(T*) {
85+
fn None() -> MaybeUnformed(T*) = "pointer.make_null";
86+
fn Some[self: Self]() -> MaybeUnformed(T*) {
87+
returned var result: MaybeUnformed(T*);
88+
result unsafe as T* = self;
89+
return var;
90+
}
91+
fn Has(value: MaybeUnformed(T*)) -> bool = "pointer.is_null";
92+
fn Get(value: MaybeUnformed(T*)) -> T* {
93+
return value unsafe as T*;
94+
}
3595
}

docs/design/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77
-->
88

99
> **STATUS:** Up-to-date on 09-Aug-2022, including proposals up through
10-
> [#1327](https://github.com/carbon-language/carbon-lang/pull/1327).
10+
> [#1382](https://github.com/carbon-language/carbon-lang/pull/1382).
1111
1212
<!-- toc -->
1313

@@ -702,7 +702,7 @@ similar to [C++](https://en.cppreference.com/w/cpp/language/value_category):
702702
Expressions in one category can be converted to any other category when needed.
703703
The primitive conversion steps used are:
704704

705-
- _Value binding_ converts a reference expression into a value expression.
705+
- _Value acquisition_ converts a reference expression into a value expression.
706706
- _Direct initialization_ converts a value expression into an initializing
707707
expression.
708708
- _Copy initialization_ converts a reference expression into an initializing

0 commit comments

Comments
 (0)