66
77#include " toolchain/base/kind_switch.h"
88#include " toolchain/check/import_ref.h"
9+ #include " toolchain/diagnostics/format_providers.h"
910#include " toolchain/sem_ir/ids.h"
1011#include " toolchain/sem_ir/typed_insts.h"
1112
@@ -193,10 +194,12 @@ static auto EntityHasParamError(Context& context, const DeclParams& info)
193194
194195// Returns false if a param differs for a redeclaration. The caller is expected
195196// to provide a diagnostic.
196- static auto CheckRedeclParam (
197- Context& context, llvm::StringLiteral param_diag_label, int32_t param_index,
198- SemIR::InstId new_param_pattern_id, SemIR::InstId prev_param_pattern_id,
199- SemIR::SpecificId prev_specific_id, bool diagnose) -> bool {
197+ static auto CheckRedeclParam (Context& context, bool is_implicit_param,
198+ int32_t param_index,
199+ SemIR::InstId new_param_pattern_id,
200+ SemIR::InstId prev_param_pattern_id,
201+ SemIR::SpecificId prev_specific_id, bool diagnose)
202+ -> bool {
200203 // TODO: Consider differentiating between type and name mistakes. For now,
201204 // taking the simpler approach because I also think we may want to refactor
202205 // params.
@@ -205,15 +208,16 @@ static auto CheckRedeclParam(
205208 return ;
206209 }
207210 CARBON_DIAGNOSTIC (RedeclParamDiffers, Error,
208- " redeclaration differs at {0}parameter {1}" ,
209- llvm::StringLiteral, int32_t );
210- CARBON_DIAGNOSTIC (RedeclParamPrevious, Note,
211- " previous declaration's corresponding {0}parameter here" ,
212- llvm::StringLiteral);
211+ " redeclaration differs at {0:implicit |}parameter {1}" ,
212+ BoolAsSelect, int32_t );
213+ CARBON_DIAGNOSTIC (
214+ RedeclParamPrevious, Note,
215+ " previous declaration's corresponding {0:implicit |}parameter here" ,
216+ BoolAsSelect);
213217 context.emitter ()
214- .Build (new_param_pattern_id, RedeclParamDiffers, param_diag_label ,
218+ .Build (new_param_pattern_id, RedeclParamDiffers, is_implicit_param ,
215219 param_index + 1 )
216- .Note (prev_param_pattern_id, RedeclParamPrevious, param_diag_label )
220+ .Note (prev_param_pattern_id, RedeclParamPrevious, is_implicit_param )
217221 .Emit ();
218222 };
219223
@@ -265,7 +269,7 @@ static auto CheckRedeclParams(Context& context, SemIRLoc new_decl_loc,
265269 SemIR::InstBlockId new_param_patterns_id,
266270 SemIRLoc prev_decl_loc,
267271 SemIR::InstBlockId prev_param_patterns_id,
268- llvm::StringLiteral param_diag_label ,
272+ bool is_implicit_param ,
269273 SemIR::SpecificId prev_specific_id, bool diagnose)
270274 -> bool {
271275 // This will often occur for empty params.
@@ -279,18 +283,18 @@ static auto CheckRedeclParams(Context& context, SemIRLoc new_decl_loc,
279283 return false ;
280284 }
281285 CARBON_DIAGNOSTIC (RedeclParamListDiffers, Error,
282- " redeclaration differs because of {1}{0}parameter list" ,
283- llvm::StringLiteral, llvm::StringLiteral);
286+ " redeclaration differs because of "
287+ " {1:'|missing '}{0:implicit |}parameter list" ,
288+ BoolAsSelect, BoolAsSelect);
284289 CARBON_DIAGNOSTIC (RedeclParamListPrevious, Note,
285- " previously declared with{1} {0}parameter list" ,
286- llvm::StringLiteral, llvm::StringLiteral);
290+ " previously declared "
291+ " {1:with|without} {0:implicit |}parameter list" ,
292+ BoolAsSelect, BoolAsSelect);
287293 context.emitter ()
288- .Build (new_decl_loc, RedeclParamListDiffers, param_diag_label,
289- new_param_patterns_id.is_valid () ? llvm::StringLiteral (" " )
290- : " missing " )
291- .Note (
292- prev_decl_loc, RedeclParamListPrevious, param_diag_label,
293- prev_param_patterns_id.is_valid () ? llvm::StringLiteral (" " ) : " out" )
294+ .Build (new_decl_loc, RedeclParamListDiffers, is_implicit_param,
295+ new_param_patterns_id.is_valid ())
296+ .Note (prev_decl_loc, RedeclParamListPrevious, is_implicit_param,
297+ prev_param_patterns_id.is_valid ())
294298 .Emit ();
295299 return false ;
296300 }
@@ -307,22 +311,23 @@ static auto CheckRedeclParams(Context& context, SemIRLoc new_decl_loc,
307311 }
308312 CARBON_DIAGNOSTIC (
309313 RedeclParamCountDiffers, Error,
310- " redeclaration differs because of {0}parameter count of {1}" ,
311- llvm::StringLiteral, int32_t );
312- CARBON_DIAGNOSTIC (RedeclParamCountPrevious, Note,
313- " previously declared with {0}parameter count of {1}" ,
314- llvm::StringLiteral, int32_t );
314+ " redeclaration differs because of {0:implicit |}parameter count of {1}" ,
315+ BoolAsSelect, int32_t );
316+ CARBON_DIAGNOSTIC (
317+ RedeclParamCountPrevious, Note,
318+ " previously declared with {0:implicit |}parameter count of {1}" ,
319+ BoolAsSelect, int32_t );
315320 context.emitter ()
316- .Build (new_decl_loc, RedeclParamCountDiffers, param_diag_label ,
321+ .Build (new_decl_loc, RedeclParamCountDiffers, is_implicit_param ,
317322 new_param_pattern_ids.size ())
318- .Note (prev_decl_loc, RedeclParamCountPrevious, param_diag_label ,
323+ .Note (prev_decl_loc, RedeclParamCountPrevious, is_implicit_param ,
319324 prev_param_pattern_ids.size ())
320325 .Emit ();
321326 return false ;
322327 }
323328 for (auto [index, new_param_pattern_id, prev_param_pattern_id] :
324329 llvm::enumerate (new_param_pattern_ids, prev_param_pattern_ids)) {
325- if (!CheckRedeclParam (context, param_diag_label , index,
330+ if (!CheckRedeclParam (context, is_implicit_param , index,
326331 new_param_pattern_id, prev_param_pattern_id,
327332 prev_specific_id, diagnose)) {
328333 return false ;
@@ -410,15 +415,16 @@ auto CheckRedeclParamsMatch(Context& context, const DeclParams& new_entity,
410415 EntityHasParamError (context, prev_entity)) {
411416 return false ;
412417 }
413- if (!CheckRedeclParams (context, new_entity. loc ,
414- new_entity.implicit_param_patterns_id , prev_entity. loc ,
415- prev_entity.implicit_param_patterns_id , " implicit " ,
416- prev_specific_id, diagnose)) {
418+ if (!CheckRedeclParams (
419+ context, new_entity.loc , new_entity. implicit_param_patterns_id ,
420+ prev_entity.loc , prev_entity. implicit_param_patterns_id ,
421+ /* is_implicit_param= */ true , prev_specific_id, diagnose)) {
417422 return false ;
418423 }
419424 if (!CheckRedeclParams (context, new_entity.loc , new_entity.param_patterns_id ,
420- prev_entity.loc , prev_entity.param_patterns_id , " " ,
421- prev_specific_id, diagnose)) {
425+ prev_entity.loc , prev_entity.param_patterns_id ,
426+ /* is_implicit_param=*/ false , prev_specific_id,
427+ diagnose)) {
422428 return false ;
423429 }
424430 if (check_syntax &&
0 commit comments