Skip to content

Commit c7646b7

Browse files
authored
Reduce use of function return type deduction (#6227)
Trying to reduce use where we could have explicit return types because it should make it a bit quicker to understand what's returned. We've generally agreed to it for [local variables](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#naming-variable-types-and-the-use-of-auto), but even the [google style guide](https://google.github.io/styleguide/cppguide.html#Type_deduction) discourages it and says "Do not use decltype(auto) if a simpler option will work; because it's a fairly obscure feature, it has a high cost in code clarity." (which to one point of discussion, feels like maybe we don't need a specific rule about this -- the remaining couple uses I see feel like they're harder to avoid)
1 parent 851da43 commit c7646b7

File tree

2 files changed

+70
-42
lines changed

2 files changed

+70
-42
lines changed

toolchain/check/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ cc_library(
139139
"//toolchain/base:index_base",
140140
"//toolchain/base:int",
141141
"//toolchain/base:kind_switch",
142+
"//toolchain/base:shared_value_stores",
142143
"//toolchain/base:value_ids",
143144
"//toolchain/base:value_store",
144145
"//toolchain/check:generic_region_stack",

toolchain/check/import_ref.cpp

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "common/check.h"
1313
#include "common/growing_range.h"
1414
#include "toolchain/base/kind_switch.h"
15+
#include "toolchain/base/shared_value_stores.h"
1516
#include "toolchain/check/context.h"
1617
#include "toolchain/check/eval.h"
1718
#include "toolchain/check/generic.h"
@@ -24,10 +25,13 @@
2425
#include "toolchain/sem_ir/constant.h"
2526
#include "toolchain/sem_ir/file.h"
2627
#include "toolchain/sem_ir/ids.h"
28+
#include "toolchain/sem_ir/impl.h"
2729
#include "toolchain/sem_ir/import_ir.h"
2830
#include "toolchain/sem_ir/inst.h"
2931
#include "toolchain/sem_ir/inst_categories.h"
3032
#include "toolchain/sem_ir/inst_kind.h"
33+
#include "toolchain/sem_ir/name_scope.h"
34+
#include "toolchain/sem_ir/specific_interface.h"
3135
#include "toolchain/sem_ir/type_info.h"
3236
#include "toolchain/sem_ir/typed_insts.h"
3337

@@ -223,56 +227,69 @@ class ImportContext {
223227
auto import_ir() -> const SemIR::File& { return import_ir_; }
224228

225229
// Accessors into value stores of the file we are importing from.
226-
auto import_associated_constants() -> decltype(auto) {
230+
auto import_associated_constants() -> const SemIR::AssociatedConstantStore& {
227231
return import_ir().associated_constants();
228232
}
229-
auto import_classes() -> decltype(auto) { return import_ir().classes(); }
230-
auto import_vtables() -> decltype(auto) { return import_ir().vtables(); }
231-
auto import_constant_values() -> decltype(auto) {
233+
auto import_classes() -> const SemIR::ClassStore& {
234+
return import_ir().classes();
235+
}
236+
auto import_vtables() -> const SemIR::VtableStore& {
237+
return import_ir().vtables();
238+
}
239+
auto import_constant_values() -> const SemIR::ConstantValueStore& {
232240
return import_ir().constant_values();
233241
}
234-
auto import_entity_names() -> decltype(auto) {
242+
auto import_entity_names() -> const SemIR::EntityNameStore& {
235243
return import_ir().entity_names();
236244
}
237-
auto import_facet_types() -> decltype(auto) {
245+
auto import_facet_types() -> const SemIR::FacetTypeInfoStore& {
238246
return import_ir().facet_types();
239247
}
240-
auto import_functions() -> decltype(auto) { return import_ir().functions(); }
241-
auto import_generics() -> decltype(auto) { return import_ir().generics(); }
242-
auto import_identifiers() -> decltype(auto) {
248+
auto import_functions() -> const SemIR::FunctionStore& {
249+
return import_ir().functions();
250+
}
251+
auto import_generics() -> const SemIR::GenericStore& {
252+
return import_ir().generics();
253+
}
254+
auto import_identifiers() -> const SharedValueStores::IdentifierStore& {
243255
return import_ir().identifiers();
244256
}
245-
auto import_impls() -> decltype(auto) { return import_ir().impls(); }
246-
auto import_inst_blocks() -> decltype(auto) {
257+
auto import_impls() -> const SemIR::ImplStore& { return import_ir().impls(); }
258+
auto import_inst_blocks() -> const SemIR::InstBlockStore& {
247259
return import_ir().inst_blocks();
248260
}
249-
auto import_insts() -> decltype(auto) { return import_ir().insts(); }
250-
auto import_interfaces() -> decltype(auto) {
261+
auto import_insts() -> const SemIR::InstStore& { return import_ir().insts(); }
262+
auto import_interfaces() -> const SemIR::InterfaceStore& {
251263
return import_ir().interfaces();
252264
}
253-
auto import_ints() -> decltype(auto) { return import_ir().ints(); }
254-
auto import_name_scopes() -> decltype(auto) {
265+
auto import_ints() -> const SharedValueStores::IntStore& {
266+
return import_ir().ints();
267+
}
268+
auto import_name_scopes() -> const SemIR::NameScopeStore& {
255269
return import_ir().name_scopes();
256270
}
257-
auto import_specifics() -> decltype(auto) { return import_ir().specifics(); }
258-
auto import_specific_interfaces() -> decltype(auto) {
271+
auto import_specifics() -> const SemIR::SpecificStore& {
272+
return import_ir().specifics();
273+
}
274+
auto import_specific_interfaces() -> const SemIR::SpecificInterfaceStore& {
259275
return import_ir().specific_interfaces();
260276
}
261-
auto import_string_literal_values() -> decltype(auto) {
277+
auto import_string_literal_values()
278+
-> const SharedValueStores::StringLiteralStore& {
262279
return import_ir().string_literal_values();
263280
}
264-
auto import_struct_type_fields() -> decltype(auto) {
281+
auto import_struct_type_fields() -> const SemIR::StructTypeFieldsStore& {
265282
return import_ir().struct_type_fields();
266283
}
267-
auto import_types() -> decltype(auto) { return import_ir().types(); }
284+
auto import_types() -> const SemIR::TypeStore& { return import_ir().types(); }
268285

269286
// Returns the local file's import ID for the IR we are importing from.
270287
auto import_ir_id() -> SemIR::ImportIRId { return import_ir_id_; }
271288

272289
// A value store for local constant values of imported instructions. This maps
273290
// from `InstId`s in the import IR to corresponding `ConstantId`s in the local
274291
// IR.
275-
auto local_constant_values_for_import_insts() -> decltype(auto) {
292+
auto local_constant_values_for_import_insts() -> SemIR::ConstantValueStore& {
276293
return local_context().import_ir_constant_values()[import_ir_id_.index];
277294
}
278295

@@ -283,49 +300,59 @@ class ImportContext {
283300
auto local_context() -> Context& { return *context_; }
284301

285302
// Accessors into value stores of the file we are importing into.
286-
auto local_associated_constants() -> decltype(auto) {
303+
auto local_associated_constants() -> SemIR::AssociatedConstantStore& {
287304
return local_ir().associated_constants();
288305
}
289-
auto local_classes() -> decltype(auto) { return local_ir().classes(); }
290-
auto local_vtables() -> decltype(auto) { return local_ir().vtables(); }
291-
auto local_constant_values() -> decltype(auto) {
306+
auto local_classes() -> SemIR::ClassStore& { return local_ir().classes(); }
307+
auto local_vtables() -> SemIR::VtableStore& { return local_ir().vtables(); }
308+
auto local_constant_values() -> SemIR::ConstantValueStore& {
292309
return local_ir().constant_values();
293310
}
294-
auto local_entity_names() -> decltype(auto) {
311+
auto local_entity_names() -> SemIR::EntityNameStore& {
295312
return local_ir().entity_names();
296313
}
297-
auto local_facet_types() -> decltype(auto) {
314+
auto local_facet_types() -> SemIR::FacetTypeInfoStore& {
298315
return local_ir().facet_types();
299316
}
300-
auto local_functions() -> decltype(auto) { return local_ir().functions(); }
301-
auto local_generics() -> decltype(auto) { return local_ir().generics(); }
302-
auto local_identifiers() -> decltype(auto) {
317+
auto local_functions() -> SemIR::FunctionStore& {
318+
return local_ir().functions();
319+
}
320+
auto local_generics() -> SemIR::GenericStore& {
321+
return local_ir().generics();
322+
}
323+
auto local_identifiers() -> SharedValueStores::IdentifierStore& {
303324
return local_ir().identifiers();
304325
}
305-
auto local_impls() -> decltype(auto) { return local_ir().impls(); }
306-
auto local_import_ir_insts() -> decltype(auto) {
326+
auto local_impls() -> SemIR::ImplStore& { return local_ir().impls(); }
327+
auto local_import_ir_insts() -> SemIR::ImportIRInstStore& {
307328
return local_ir().import_ir_insts();
308329
}
309-
auto local_inst_blocks() -> decltype(auto) {
330+
auto local_inst_blocks() -> SemIR::InstBlockStore& {
310331
return local_ir().inst_blocks();
311332
}
312-
auto local_insts() -> decltype(auto) { return local_ir().insts(); }
313-
auto local_interfaces() -> decltype(auto) { return local_ir().interfaces(); }
314-
auto local_ints() -> decltype(auto) { return local_ir().ints(); }
315-
auto local_name_scopes() -> decltype(auto) {
333+
auto local_insts() -> SemIR::InstStore& { return local_ir().insts(); }
334+
auto local_interfaces() -> SemIR::InterfaceStore& {
335+
return local_ir().interfaces();
336+
}
337+
auto local_ints() -> SharedValueStores::IntStore& {
338+
return local_ir().ints();
339+
}
340+
auto local_name_scopes() -> SemIR::NameScopeStore& {
316341
return local_ir().name_scopes();
317342
}
318-
auto local_specifics() -> decltype(auto) { return local_ir().specifics(); }
319-
auto local_specific_interfaces() -> decltype(auto) {
343+
auto local_specifics() -> SemIR::SpecificStore& {
344+
return local_ir().specifics();
345+
}
346+
auto local_specific_interfaces() -> SemIR::SpecificInterfaceStore& {
320347
return local_ir().specific_interfaces();
321348
}
322-
auto local_string_literal_values() -> decltype(auto) {
349+
auto local_string_literal_values() -> SharedValueStores::StringLiteralStore& {
323350
return local_ir().string_literal_values();
324351
}
325-
auto local_struct_type_fields() -> decltype(auto) {
352+
auto local_struct_type_fields() -> SemIR::StructTypeFieldsStore& {
326353
return local_ir().struct_type_fields();
327354
}
328-
auto local_types() -> decltype(auto) { return local_ir().types(); }
355+
auto local_types() -> SemIR::TypeStore& { return local_ir().types(); }
329356

330357
// Add a generic that has been partially imported but needs to be finished.
331358
auto AddPendingGeneric(PendingGeneric pending) -> void {

0 commit comments

Comments
 (0)