diff --git a/toolchain/check/convert.cpp b/toolchain/check/convert.cpp index e36e927e6002..7fa08d3cf330 100644 --- a/toolchain/check/convert.cpp +++ b/toolchain/check/convert.cpp @@ -722,14 +722,16 @@ static auto ConvertDerivedPointerToBasePointer( } // Returns whether `category` is a valid expression category to produce as a -// result of a conversion with kind `target_kind`, or at most needs a temporary -// to be materialized. +// result of a conversion with kind `target_kind`. static auto IsValidExprCategoryForConversionTarget( SemIR::ExprCategory category, ConversionTarget::Kind target_kind) -> bool { switch (target_kind) { case ConversionTarget::Value: return category == SemIR::ExprCategory::Value; case ConversionTarget::ValueOrRef: + return category == SemIR::ExprCategory::Value || + category == SemIR::ExprCategory::DurableRef || + category == SemIR::ExprCategory::EphemeralRef; case ConversionTarget::Discarded: return category == SemIR::ExprCategory::Value || category == SemIR::ExprCategory::DurableRef || @@ -910,6 +912,12 @@ static auto PerformBuiltinConversion( context, loc_id, {.type_id = value_type_id, .init_id = value_id}); } + // Materialization is handled as part of the enclosing conversion. + if (value_cat == SemIR::ExprCategory::Initializing && + target.kind == ConversionTarget::ValueOrRef) { + return value_id; + } + // PerformBuiltinConversion converts each part of a tuple or struct, even // when the types are the same. This is not done for classes since they have // to define their conversions as part of their api. diff --git a/toolchain/check/handle_operator.cpp b/toolchain/check/handle_operator.cpp index d3c0b248160c..1a23d71945c7 100644 --- a/toolchain/check/handle_operator.cpp +++ b/toolchain/check/handle_operator.cpp @@ -252,13 +252,6 @@ auto HandleParseNode(Context& context, Parse::PrefixOperatorAmpId node_id) case SemIR::ExprCategory::DurableRef: case SemIR::ExprCategory::Error: break; - case SemIR::ExprCategory::EphemeralRef: - CARBON_DIAGNOSTIC(AddrOfEphemeralRef, Error, - "cannot take the address of a temporary object"); - context.emitter().Emit(LocIdForDiagnostics::TokenOnly(node_id), - AddrOfEphemeralRef); - value_id = SemIR::ErrorInst::InstId; - break; default: CARBON_DIAGNOSTIC(AddrOfNonRef, Error, "cannot take the address of non-reference expression"); diff --git a/toolchain/check/testdata/pointer/fail_address_of_value.carbon b/toolchain/check/testdata/pointer/fail_address_of_value.carbon index 0bd1173a0f78..ec5cc4be59d8 100644 --- a/toolchain/check/testdata/pointer/fail_address_of_value.carbon +++ b/toolchain/check/testdata/pointer/fail_address_of_value.carbon @@ -53,7 +53,7 @@ fn AddressOfOperator() { // CHECK:STDERR: ^ // CHECK:STDERR: &(true and false); - // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of a temporary object [AddrOfEphemeralRef] + // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef] // CHECK:STDERR: &H().a; // CHECK:STDERR: ^ // CHECK:STDERR: diff --git a/toolchain/check/testdata/tuple/element_access.carbon b/toolchain/check/testdata/tuple/element_access.carbon index 584e749699ef..0c660f9d4880 100644 --- a/toolchain/check/testdata/tuple/element_access.carbon +++ b/toolchain/check/testdata/tuple/element_access.carbon @@ -438,7 +438,6 @@ var b: i32 = a.({.index = 2}.index); // CHECK:STDOUT: // CHECK:STDOUT: constants { // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete] -// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] // CHECK:STDOUT: %N: Core.IntLiteral = symbolic_binding N, 0 [symbolic] // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete] // CHECK:STDOUT: %tuple.type.a1c: type = tuple_type (%i32) [concrete] @@ -455,11 +454,6 @@ var b: i32 = a.({.index = 2}.index); // CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value %i32, (%Copy.impl_witness.fb7) [concrete] // CHECK:STDOUT: %.65f: type = fn_type_with_self_type %Copy.Op.type, %Copy.facet [concrete] // CHECK:STDOUT: %Int.as.Copy.impl.Op.specific_fn: = specific_function %Int.as.Copy.impl.Op.dfd, @Int.as.Copy.impl.Op(%int_32) [concrete] -// CHECK:STDOUT: %type_where: type = facet_type > [concrete] -// CHECK:STDOUT: %facet_value: %type_where = facet_value %tuple.type.a1c, () [concrete] -// CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.49b: type = fn_type @DestroyT.binding.as_type.as.Destroy.impl.Op, @DestroyT.binding.as_type.as.Destroy.impl(%facet_value) [concrete] -// CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.c36: %DestroyT.binding.as_type.as.Destroy.impl.Op.type.49b = struct_value () [concrete] -// CHECK:STDOUT: %ptr.0b7: type = ptr_type %tuple.type.a1c [concrete] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -472,20 +466,14 @@ var b: i32 = a.({.index = 2}.index); // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F] // CHECK:STDOUT: %F.call: init %tuple.type.a1c = call %F.ref() // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6] -// CHECK:STDOUT: %.loc7_12.1: ref %tuple.type.a1c = temporary_storage -// CHECK:STDOUT: %.loc7_12.2: ref %tuple.type.a1c = temporary %.loc7_12.1, %F.call -// CHECK:STDOUT: %tuple.elem0: ref %i32 = tuple_access %.loc7_12.2, element0 -// CHECK:STDOUT: %.loc7_13: %i32 = acquire_value %tuple.elem0 +// CHECK:STDOUT: %.loc7_12.1: %tuple.type.a1c = value_of_initializer %F.call +// CHECK:STDOUT: %.loc7_12.2: %tuple.type.a1c = converted %F.call, %.loc7_12.1 +// CHECK:STDOUT: %tuple.elem0: %i32 = tuple_access %.loc7_12.2, element0 // CHECK:STDOUT: %impl.elem0: %.65f = impl_witness_access constants.%Copy.impl_witness.fb7, element0 [concrete = constants.%Int.as.Copy.impl.Op.dfd] -// CHECK:STDOUT: %bound_method.loc7_13.1: = bound_method %.loc7_13, %impl.elem0 +// CHECK:STDOUT: %bound_method.loc7_13.1: = bound_method %tuple.elem0, %impl.elem0 // CHECK:STDOUT: %specific_fn: = specific_function %impl.elem0, @Int.as.Copy.impl.Op(constants.%int_32) [concrete = constants.%Int.as.Copy.impl.Op.specific_fn] -// CHECK:STDOUT: %bound_method.loc7_13.2: = bound_method %.loc7_13, %specific_fn -// CHECK:STDOUT: %Int.as.Copy.impl.Op.call: init %i32 = call %bound_method.loc7_13.2(%.loc7_13) -// CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.bound: = bound_method %.loc7_12.2, constants.%DestroyT.binding.as_type.as.Destroy.impl.Op.c36 -// CHECK:STDOUT: -// CHECK:STDOUT: %bound_method.loc7_12: = bound_method %.loc7_12.2, %DestroyT.binding.as_type.as.Destroy.impl.Op.specific_fn -// CHECK:STDOUT: %addr: %ptr.0b7 = addr_of %.loc7_12.2 -// CHECK:STDOUT: %DestroyT.binding.as_type.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc7_12(%addr) +// CHECK:STDOUT: %bound_method.loc7_13.2: = bound_method %tuple.elem0, %specific_fn +// CHECK:STDOUT: %Int.as.Copy.impl.Op.call: init %i32 = call %bound_method.loc7_13.2(%tuple.elem0) // CHECK:STDOUT: return %Int.as.Copy.impl.Op.call to %return // CHECK:STDOUT: } // CHECK:STDOUT: diff --git a/toolchain/diagnostics/diagnostic_kind.def b/toolchain/diagnostics/diagnostic_kind.def index 20d78a84b14b..bcac19f85eea 100644 --- a/toolchain/diagnostics/diagnostic_kind.def +++ b/toolchain/diagnostics/diagnostic_kind.def @@ -392,7 +392,6 @@ CARBON_DIAGNOSTIC_KIND(AbstractTypeInFieldDecl) CARBON_DIAGNOSTIC_KIND(AbstractTypeInFunctionReturnType) CARBON_DIAGNOSTIC_KIND(AbstractTypeInInit) CARBON_DIAGNOSTIC_KIND(AbstractTypeInVarPattern) -CARBON_DIAGNOSTIC_KIND(AddrOfEphemeralRef) CARBON_DIAGNOSTIC_KIND(AddrOfNonRef) CARBON_DIAGNOSTIC_KIND(AddrOnNonSelfParam) CARBON_DIAGNOSTIC_KIND(AddrOnNonPointerType)