mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:20:11 +01:00
Prevent copies when initializing value expression from reference expression. This is based on https://github.com/carbon-language/carbon-lang/pull/2006, which introduces expression categories, and how it is possible to convert to/from those different categories. Continuation of https://github.com/carbon-language/carbon-lang/pull/2907 ## Functional changes * Initializing a value expression from a reference expression takes its value without a copy * Reading from the value expression causes an error if the value changed from the time it was initialized * In this situation, prevents a copy both for variable definitions, and call parameter bindings ## Main implementation changes * Add new `ExpressionCategoryAction`, which evaluates an expression and returns an `ExpressionValue` containing its category and address (if any), in addition to the resulting `Value*` * `ExpressionAction`s now invokes `ExpressionCategoryAction` and unwraps the returned `ExpressionValue` * `RuntimeScope::BindAndPin` method, and corresponding when attempting to read a `value_node`. ## Next work * Avoid unnecessary copies from value expression to value expression, after ensuring that even value expression temporaries are registered for destruction (https://github.com/Pixep/carbon-lang/pull/9)
33 lines
758 B
Plaintext
33 lines
758 B
Plaintext
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
// AUTOUPDATE
|
|
// CHECK:STDOUT: 0: Heap{}, 1: C{}
|
|
// CHECK:STDOUT: Initialize c from reference expression
|
|
// CHECK:STDOUT: 0: Heap{}, 1: C{}, 2: C{}
|
|
// CHECK:STDOUT: c destroyed
|
|
// CHECK:STDOUT: c destroyed
|
|
// CHECK:STDOUT: result: 0
|
|
|
|
package ExplorerTest api;
|
|
|
|
class C {
|
|
destructor[self: Self] {
|
|
Print("c destroyed");
|
|
}
|
|
}
|
|
|
|
fn FromReferenceExpression() {
|
|
var c_var: C = {};
|
|
heap.PrintAllocs();
|
|
Print("Initialize c from reference expression");
|
|
var c: C = c_var;
|
|
heap.PrintAllocs();
|
|
}
|
|
|
|
fn Main() -> i32 {
|
|
FromReferenceExpression();
|
|
return 0;
|
|
}
|