merge.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/merge.h"
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/import.h"
  7. #include "toolchain/check/import_ref.h"
  8. #include "toolchain/diagnostics/format_providers.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::Check {
  12. CARBON_DIAGNOSTIC(RedeclPrevDecl, Note, "previously declared here");
  13. // Diagnoses a redeclaration which is redundant.
  14. static auto DiagnoseRedundant(Context& context, Lex::TokenKind decl_kind,
  15. SemIR::NameId name_id, SemIR::LocId new_loc_id,
  16. SemIR::LocId prev_loc_id) -> void {
  17. CARBON_DIAGNOSTIC(RedeclRedundant, Error,
  18. "redeclaration of `{0} {1}` is redundant", Lex::TokenKind,
  19. SemIR::NameId);
  20. context.emitter()
  21. .Build(new_loc_id, RedeclRedundant, decl_kind, name_id)
  22. .Note(prev_loc_id, RedeclPrevDecl)
  23. .Emit();
  24. }
  25. // Diagnoses a redefinition.
  26. static auto DiagnoseRedef(Context& context, Lex::TokenKind decl_kind,
  27. SemIR::NameId name_id, SemIR::LocId new_loc_id,
  28. SemIR::LocId prev_loc_id) -> void {
  29. CARBON_DIAGNOSTIC(RedeclRedef, Error, "redefinition of `{0} {1}`",
  30. Lex::TokenKind, SemIR::NameId);
  31. CARBON_DIAGNOSTIC(RedeclPrevDef, Note, "previously defined here");
  32. context.emitter()
  33. .Build(new_loc_id, RedeclRedef, decl_kind, name_id)
  34. .Note(prev_loc_id, RedeclPrevDef)
  35. .Emit();
  36. }
  37. // Diagnoses an `extern` versus non-`extern` mismatch.
  38. static auto DiagnoseExternMismatch(Context& context, Lex::TokenKind decl_kind,
  39. SemIR::NameId name_id,
  40. SemIR::LocId new_loc_id,
  41. SemIR::LocId prev_loc_id) -> void {
  42. CARBON_DIAGNOSTIC(RedeclExternMismatch, Error,
  43. "redeclarations of `{0} {1}` must match use of `extern`",
  44. Lex::TokenKind, SemIR::NameId);
  45. context.emitter()
  46. .Build(new_loc_id, RedeclExternMismatch, decl_kind, name_id)
  47. .Note(prev_loc_id, RedeclPrevDecl)
  48. .Emit();
  49. }
  50. // Diagnoses `extern library` declared in a library importing the owned entity.
  51. static auto DiagnoseExternLibraryInImporter(Context& context,
  52. Lex::TokenKind decl_kind,
  53. SemIR::NameId name_id,
  54. SemIR::LocId new_loc_id,
  55. SemIR::LocId prev_loc_id) -> void {
  56. CARBON_DIAGNOSTIC(ExternLibraryInImporter, Error,
  57. "cannot declare imported `{0} {1}` as `extern library`",
  58. Lex::TokenKind, SemIR::NameId);
  59. context.emitter()
  60. .Build(new_loc_id, ExternLibraryInImporter, decl_kind, name_id)
  61. .Note(prev_loc_id, RedeclPrevDecl)
  62. .Emit();
  63. }
  64. // Diagnoses `extern library` pointing to the wrong library.
  65. static auto DiagnoseExternLibraryIncorrect(Context& context,
  66. SemIR::LocId new_loc_id,
  67. SemIR::LocId prev_loc_id) -> void {
  68. CARBON_DIAGNOSTIC(
  69. ExternLibraryIncorrect, Error,
  70. "declaration in {0} doesn't match `extern library` declaration",
  71. SemIR::LibraryNameId);
  72. CARBON_DIAGNOSTIC(ExternLibraryExpected, Note,
  73. "previously declared with `extern library` here");
  74. context.emitter()
  75. .Build(new_loc_id, ExternLibraryIncorrect, context.sem_ir().library_id())
  76. .Note(prev_loc_id, ExternLibraryExpected)
  77. .Emit();
  78. }
  79. auto DiagnoseExternRequiresDeclInApiFile(Context& context, SemIR::LocId loc_id)
  80. -> void {
  81. CARBON_DIAGNOSTIC(
  82. ExternRequiresDeclInApiFile, Error,
  83. "`extern` entities must have a declaration in the API file");
  84. context.emitter().Emit(loc_id, ExternRequiresDeclInApiFile);
  85. }
  86. auto DiagnoseIfInvalidRedecl(Context& context, Lex::TokenKind decl_kind,
  87. SemIR::NameId name_id, RedeclInfo new_decl,
  88. RedeclInfo prev_decl,
  89. SemIR::ImportIRId import_ir_id) -> void {
  90. if (!import_ir_id.has_value()) {
  91. // Check for disallowed redeclarations in the same file.
  92. if (!new_decl.is_definition) {
  93. DiagnoseRedundant(context, decl_kind, name_id, new_decl.loc_id,
  94. prev_decl.loc_id);
  95. return;
  96. }
  97. if (prev_decl.is_definition) {
  98. DiagnoseRedef(context, decl_kind, name_id, new_decl.loc_id,
  99. prev_decl.loc_id);
  100. return;
  101. }
  102. if (prev_decl.is_extern != new_decl.is_extern) {
  103. DiagnoseExternMismatch(context, decl_kind, name_id, new_decl.loc_id,
  104. prev_decl.loc_id);
  105. return;
  106. }
  107. return;
  108. }
  109. if (import_ir_id == SemIR::ImportIRId::ApiForImpl) {
  110. // Check for disallowed redeclarations in the same library. Note that a
  111. // forward declaration in the impl is allowed.
  112. if (prev_decl.is_definition) {
  113. if (new_decl.is_definition) {
  114. DiagnoseRedef(context, decl_kind, name_id, new_decl.loc_id,
  115. prev_decl.loc_id);
  116. } else {
  117. DiagnoseRedundant(context, decl_kind, name_id, new_decl.loc_id,
  118. prev_decl.loc_id);
  119. }
  120. return;
  121. }
  122. if (prev_decl.is_extern != new_decl.is_extern) {
  123. DiagnoseExternMismatch(context, decl_kind, name_id, new_decl.loc_id,
  124. prev_decl.loc_id);
  125. return;
  126. }
  127. return;
  128. }
  129. // Check for disallowed redeclarations cross-library.
  130. if (new_decl.is_extern && context.sem_ir().is_impl()) {
  131. // We continue after issuing the "missing API declaration" diagnostic,
  132. // because it may still be helpful to note other issues with the
  133. // declarations.
  134. DiagnoseExternRequiresDeclInApiFile(context, new_decl.loc_id);
  135. }
  136. if (prev_decl.is_extern != new_decl.is_extern) {
  137. DiagnoseExternMismatch(context, decl_kind, name_id, new_decl.loc_id,
  138. prev_decl.loc_id);
  139. return;
  140. }
  141. if (!prev_decl.extern_library_id.has_value()) {
  142. if (new_decl.extern_library_id.has_value()) {
  143. DiagnoseExternLibraryInImporter(context, decl_kind, name_id,
  144. new_decl.loc_id, prev_decl.loc_id);
  145. } else {
  146. DiagnoseRedundant(context, decl_kind, name_id, new_decl.loc_id,
  147. prev_decl.loc_id);
  148. }
  149. return;
  150. }
  151. if (prev_decl.extern_library_id != SemIR::LibraryNameId::Error &&
  152. prev_decl.extern_library_id != context.sem_ir().library_id()) {
  153. DiagnoseExternLibraryIncorrect(context, new_decl.loc_id, prev_decl.loc_id);
  154. return;
  155. }
  156. }
  157. auto ReplacePrevInstForMerge(Context& context, SemIR::NameScopeId scope_id,
  158. SemIR::NameId name_id, SemIR::InstId new_inst_id)
  159. -> void {
  160. auto& scope = context.name_scopes().Get(scope_id);
  161. auto entry_id = scope.Lookup(name_id);
  162. if (entry_id) {
  163. auto& result = scope.GetEntry(*entry_id).result;
  164. result = SemIR::ScopeLookupResult::MakeWrappedLookupResult(
  165. new_inst_id, result.access_kind());
  166. }
  167. }
  168. // Returns true if there was an error in declaring the entity, which will have
  169. // previously been diagnosed.
  170. static auto EntityHasParamError(Context& context, const DeclParams& info)
  171. -> bool {
  172. for (auto param_patterns_id :
  173. {info.implicit_param_patterns_id, info.param_patterns_id}) {
  174. if (param_patterns_id.has_value() &&
  175. param_patterns_id != SemIR::InstBlockId::Empty) {
  176. for (auto param_id : context.inst_blocks().Get(param_patterns_id)) {
  177. if (context.insts().Get(param_id).type_id() ==
  178. SemIR::ErrorInst::TypeId) {
  179. return true;
  180. }
  181. }
  182. }
  183. }
  184. return false;
  185. }
  186. // Returns false if a param differs for a redeclaration. The caller is expected
  187. // to provide a diagnostic.
  188. static auto CheckRedeclParam(Context& context, bool is_implicit_param,
  189. int32_t param_index,
  190. SemIR::InstId orig_new_param_pattern_id,
  191. SemIR::InstId orig_prev_param_pattern_id,
  192. SemIR::SpecificId prev_specific_id, bool diagnose,
  193. bool check_syntax, bool check_self) -> bool {
  194. CARBON_DIAGNOSTIC(
  195. RedeclParamPrevious, Note,
  196. "previous declaration's corresponding {0:implicit |}parameter here",
  197. Diagnostics::BoolAsSelect);
  198. auto emit_general_diagnostic = [&]() {
  199. if (!diagnose) {
  200. return;
  201. }
  202. CARBON_DIAGNOSTIC(RedeclParamDiffers, Error,
  203. "redeclaration differs at {0:implicit |}parameter {1}",
  204. Diagnostics::BoolAsSelect, int32_t);
  205. context.emitter()
  206. .Build(orig_new_param_pattern_id, RedeclParamDiffers, is_implicit_param,
  207. param_index + 1)
  208. .Note(orig_prev_param_pattern_id, RedeclParamPrevious,
  209. is_implicit_param)
  210. .Emit();
  211. };
  212. struct PatternPair {
  213. SemIR::InstId prev_id;
  214. SemIR::InstId new_id;
  215. };
  216. llvm::SmallVector<PatternPair, 1> pattern_stack;
  217. pattern_stack.push_back({.prev_id = orig_prev_param_pattern_id,
  218. .new_id = orig_new_param_pattern_id});
  219. // When `check_self` is false, we need to disable type checking as soon as we
  220. // determine this is a `self` parameter, and that decision needs to persist
  221. // across the handling of any subpatterns.
  222. bool check_type = true;
  223. do {
  224. auto patterns = pattern_stack.pop_back_val();
  225. auto new_param_pattern = context.insts().Get(patterns.new_id);
  226. auto prev_param_pattern = context.insts().Get(patterns.prev_id);
  227. if (new_param_pattern.kind() != prev_param_pattern.kind()) {
  228. emit_general_diagnostic();
  229. return false;
  230. }
  231. // Conditionally checks for and diagnoses a type mismatch between the old
  232. // and new parameter patterns. Returns false if a mismatch was found.
  233. auto check_for_type_mismatch = [&]() {
  234. auto prev_param_type_id = SemIR::GetTypeOfInstInSpecific(
  235. context.sem_ir(), prev_specific_id, patterns.prev_id);
  236. if (check_type && !context.types().AreEqualAcrossDeclarations(
  237. new_param_pattern.type_id(), prev_param_type_id)) {
  238. if (diagnose) {
  239. CARBON_DIAGNOSTIC(
  240. RedeclParamDiffersType, Error,
  241. "type {3} of {0:implicit |}parameter {1} in "
  242. "redeclaration differs from previous parameter type {2}",
  243. Diagnostics::BoolAsSelect, int32_t, SemIR::TypeId, SemIR::TypeId);
  244. context.emitter()
  245. .Build(orig_new_param_pattern_id, RedeclParamDiffersType,
  246. is_implicit_param, param_index + 1, prev_param_type_id,
  247. new_param_pattern.type_id())
  248. .Note(orig_prev_param_pattern_id, RedeclParamPrevious,
  249. is_implicit_param)
  250. .Emit();
  251. }
  252. return false;
  253. }
  254. return true;
  255. };
  256. CARBON_KIND_SWITCH(new_param_pattern) {
  257. case CARBON_KIND_ANY(SemIR::AnyLeafParamPattern, _): {
  258. if (!check_for_type_mismatch()) {
  259. return false;
  260. }
  261. break;
  262. }
  263. case CARBON_KIND_ANY(SemIR::AnyVarPattern, new_var_param_pattern): {
  264. auto prev_var_param_pattern =
  265. prev_param_pattern.As<SemIR::AnyVarPattern>();
  266. pattern_stack.push_back(
  267. {.prev_id = prev_var_param_pattern.subpattern_id,
  268. .new_id = new_var_param_pattern.subpattern_id});
  269. break;
  270. }
  271. case CARBON_KIND_ANY(SemIR::AnyBindingPattern, new_any_binding_pattern): {
  272. auto prev_any_binding_pattern =
  273. prev_param_pattern.As<SemIR::AnyBindingPattern>();
  274. auto new_name_id = context.entity_names()
  275. .Get(new_any_binding_pattern.entity_name_id)
  276. .name_id;
  277. auto prev_name_id = context.entity_names()
  278. .Get(prev_any_binding_pattern.entity_name_id)
  279. .name_id;
  280. if (!check_self && new_name_id == SemIR::NameId::SelfValue &&
  281. prev_name_id == SemIR::NameId::SelfValue) {
  282. check_type = false;
  283. }
  284. if (new_any_binding_pattern.kind ==
  285. SemIR::WrapperBindingPattern::Kind) {
  286. // The subpattern handling will take care of checking for type
  287. // mismatch.
  288. pattern_stack.push_back(
  289. {.prev_id = prev_any_binding_pattern.subpattern_id,
  290. .new_id = new_any_binding_pattern.subpattern_id});
  291. } else if (!check_for_type_mismatch()) {
  292. return false;
  293. }
  294. if (check_syntax && new_name_id != prev_name_id) {
  295. emit_general_diagnostic();
  296. return false;
  297. }
  298. break;
  299. }
  300. default: {
  301. CARBON_FATAL("Unexpected inst kind in parameter pattern: {0}",
  302. new_param_pattern.kind());
  303. }
  304. }
  305. } while (!pattern_stack.empty());
  306. return true;
  307. }
  308. // Returns false if the param refs differ for a redeclaration.
  309. static auto CheckRedeclParams(Context& context, SemIR::LocId new_decl_loc_id,
  310. SemIR::InstBlockId new_param_patterns_id,
  311. SemIR::LocId prev_decl_loc_id,
  312. SemIR::InstBlockId prev_param_patterns_id,
  313. bool is_implicit_param,
  314. SemIR::SpecificId prev_specific_id, bool diagnose,
  315. bool check_syntax, bool check_self) -> bool {
  316. // This will often occur for empty params.
  317. if (new_param_patterns_id == prev_param_patterns_id) {
  318. return true;
  319. }
  320. // If exactly one of the parameter lists was present, they differ.
  321. if (new_param_patterns_id.has_value() != prev_param_patterns_id.has_value()) {
  322. if (!diagnose) {
  323. return false;
  324. }
  325. CARBON_DIAGNOSTIC(RedeclParamListDiffers, Error,
  326. "redeclaration differs because of "
  327. "{1:|missing }{0:implicit |}parameter list",
  328. Diagnostics::BoolAsSelect, Diagnostics::BoolAsSelect);
  329. CARBON_DIAGNOSTIC(RedeclParamListPrevious, Note,
  330. "previously declared "
  331. "{1:with|without} {0:implicit |}parameter list",
  332. Diagnostics::BoolAsSelect, Diagnostics::BoolAsSelect);
  333. context.emitter()
  334. .Build(new_decl_loc_id, RedeclParamListDiffers, is_implicit_param,
  335. new_param_patterns_id.has_value())
  336. .Note(prev_decl_loc_id, RedeclParamListPrevious, is_implicit_param,
  337. prev_param_patterns_id.has_value())
  338. .Emit();
  339. return false;
  340. }
  341. CARBON_CHECK(new_param_patterns_id.has_value() &&
  342. prev_param_patterns_id.has_value());
  343. const auto new_param_pattern_ids =
  344. context.inst_blocks().Get(new_param_patterns_id);
  345. const auto prev_param_pattern_ids =
  346. context.inst_blocks().Get(prev_param_patterns_id);
  347. if (new_param_pattern_ids.size() != prev_param_pattern_ids.size()) {
  348. if (!diagnose) {
  349. return false;
  350. }
  351. CARBON_DIAGNOSTIC(
  352. RedeclParamCountDiffers, Error,
  353. "redeclaration differs because of {0:implicit |}parameter count of {1}",
  354. Diagnostics::BoolAsSelect, int32_t);
  355. CARBON_DIAGNOSTIC(
  356. RedeclParamCountPrevious, Note,
  357. "previously declared with {0:implicit |}parameter count of {1}",
  358. Diagnostics::BoolAsSelect, int32_t);
  359. context.emitter()
  360. .Build(new_decl_loc_id, RedeclParamCountDiffers, is_implicit_param,
  361. new_param_pattern_ids.size())
  362. .Note(prev_decl_loc_id, RedeclParamCountPrevious, is_implicit_param,
  363. prev_param_pattern_ids.size())
  364. .Emit();
  365. return false;
  366. }
  367. for (auto [index, new_param_pattern_id, prev_param_pattern_id] :
  368. llvm::enumerate(new_param_pattern_ids, prev_param_pattern_ids)) {
  369. if (!CheckRedeclParam(context, is_implicit_param, index,
  370. new_param_pattern_id, prev_param_pattern_id,
  371. prev_specific_id, diagnose, check_syntax,
  372. check_self)) {
  373. return false;
  374. }
  375. }
  376. return true;
  377. }
  378. // Returns true if the two nodes represent the same syntax.
  379. // TODO: Detect raw identifiers (will require token changes).
  380. static auto IsNodeSyntaxEqual(Context& context, Parse::NodeId new_node_id,
  381. Parse::NodeId prev_node_id) -> bool {
  382. if (context.parse_tree().node_kind(new_node_id) !=
  383. context.parse_tree().node_kind(prev_node_id)) {
  384. return false;
  385. }
  386. // TODO: Should there be a trivial way to check if we need to check spellings?
  387. // Identifiers and literals need their text checked for cross-file matching,
  388. // but not intra-file. Keywords and operators shouldn't need the token text
  389. // examined at all.
  390. auto new_spelling = context.tokens().GetTokenText(
  391. context.parse_tree().node_token(new_node_id));
  392. auto prev_spelling = context.tokens().GetTokenText(
  393. context.parse_tree().node_token(prev_node_id));
  394. return new_spelling == prev_spelling;
  395. }
  396. // Returns false if redeclaration parameter syntax doesn't match.
  397. static auto CheckRedeclParamSyntax(Context& context,
  398. Parse::NodeId new_first_param_node_id,
  399. Parse::NodeId new_last_param_node_id,
  400. Parse::NodeId prev_first_param_node_id,
  401. Parse::NodeId prev_last_param_node_id,
  402. bool diagnose) -> bool {
  403. // Parse nodes may not always be available to compare.
  404. // TODO: Support cross-file syntax checks. Right now imports provide
  405. // `NodeId::None`, and we'll need to follow the declaration to its original
  406. // file to get the parse tree.
  407. if (!new_first_param_node_id.has_value() ||
  408. !prev_first_param_node_id.has_value()) {
  409. return true;
  410. }
  411. CARBON_CHECK(new_last_param_node_id.has_value(),
  412. "new_last_param_node_id.has_value should match "
  413. "new_first_param_node_id.has_value");
  414. CARBON_CHECK(prev_last_param_node_id.has_value(),
  415. "prev_last_param_node_id.has_value should match "
  416. "prev_first_param_node_id.has_value");
  417. Parse::Tree::PostorderIterator new_iter(new_first_param_node_id);
  418. Parse::Tree::PostorderIterator new_end(new_last_param_node_id);
  419. Parse::Tree::PostorderIterator prev_iter(prev_first_param_node_id);
  420. Parse::Tree::PostorderIterator prev_end(prev_last_param_node_id);
  421. // Done when one past the last node to check.
  422. ++new_end;
  423. ++prev_end;
  424. // Compare up to the shortest length.
  425. for (; new_iter != new_end && prev_iter != prev_end;
  426. ++new_iter, ++prev_iter) {
  427. auto new_node_id = *new_iter;
  428. auto new_node_kind = context.parse_tree().node_kind(new_node_id);
  429. // Skip over "unused" markers.
  430. if (new_node_kind == Parse::NodeKind::UnusedPattern) {
  431. ++new_iter;
  432. new_node_id = *new_iter;
  433. new_node_kind = context.parse_tree().node_kind(new_node_id);
  434. }
  435. auto prev_node_id = *prev_iter;
  436. auto prev_node_kind = context.parse_tree().node_kind(prev_node_id);
  437. if (prev_node_kind == Parse::NodeKind::UnusedPattern) {
  438. ++prev_iter;
  439. prev_node_id = *prev_iter;
  440. prev_node_kind = context.parse_tree().node_kind(prev_node_id);
  441. }
  442. if (!IsNodeSyntaxEqual(context, new_node_id, prev_node_id)) {
  443. // Skip difference if it is `Self as` vs. `as` in an `impl` declaration.
  444. // https://github.com/carbon-language/carbon-lang/blob/trunk/proposals/p3763.md#redeclarations
  445. if (new_node_kind == Parse::NodeKind::ImplDefaultSelfAs &&
  446. prev_node_kind == Parse::NodeKind::SelfTypeNameExpr &&
  447. context.parse_tree().node_kind(prev_iter[1]) ==
  448. Parse::NodeKind::ImplTypeAs) {
  449. ++prev_iter;
  450. continue;
  451. }
  452. if (prev_node_kind == Parse::NodeKind::ImplDefaultSelfAs &&
  453. new_node_kind == Parse::NodeKind::SelfTypeNameExpr &&
  454. context.parse_tree().node_kind(new_iter[1]) ==
  455. Parse::NodeKind::ImplTypeAs) {
  456. ++new_iter;
  457. continue;
  458. }
  459. if (!diagnose) {
  460. return false;
  461. }
  462. CARBON_DIAGNOSTIC(RedeclParamSyntaxDiffers, Error,
  463. "redeclaration syntax differs here");
  464. CARBON_DIAGNOSTIC(RedeclParamSyntaxPrevious, Note,
  465. "comparing with previous declaration here");
  466. context.emitter()
  467. .Build(new_node_id, RedeclParamSyntaxDiffers)
  468. .Note(prev_node_id, RedeclParamSyntaxPrevious)
  469. .Emit();
  470. return false;
  471. }
  472. }
  473. // The prefixes are the same, but the lengths may still be different. This is
  474. // only relevant for `impl` declarations where the final bracketing node is
  475. // not included in the range of nodes being compared, and in those cases
  476. // `diagnose` is false.
  477. if (new_iter != new_end) {
  478. CARBON_CHECK(!diagnose);
  479. return false;
  480. } else if (prev_iter != prev_end) {
  481. CARBON_CHECK(!diagnose);
  482. return false;
  483. }
  484. return true;
  485. }
  486. auto CheckRedeclParamsMatch(Context& context, const DeclParams& new_entity,
  487. const DeclParams& prev_entity,
  488. SemIR::SpecificId prev_specific_id, bool diagnose,
  489. bool check_syntax, bool check_self) -> bool {
  490. if (EntityHasParamError(context, new_entity) ||
  491. EntityHasParamError(context, prev_entity)) {
  492. return false;
  493. }
  494. if (!CheckRedeclParams(
  495. context, new_entity.loc_id, new_entity.implicit_param_patterns_id,
  496. prev_entity.loc_id, prev_entity.implicit_param_patterns_id,
  497. /*is_implicit_param=*/true, prev_specific_id, diagnose, check_syntax,
  498. check_self)) {
  499. return false;
  500. }
  501. // Don't forward `check_self` here because it's extra cost, and `self` is only
  502. // allowed in implicit params.
  503. if (!CheckRedeclParams(context, new_entity.loc_id,
  504. new_entity.param_patterns_id, prev_entity.loc_id,
  505. prev_entity.param_patterns_id,
  506. /*is_implicit_param=*/false, prev_specific_id,
  507. diagnose, check_syntax, /*check_self=*/true)) {
  508. return false;
  509. }
  510. if (check_syntax &&
  511. !CheckRedeclParamSyntax(context, new_entity.first_param_node_id,
  512. new_entity.last_param_node_id,
  513. prev_entity.first_param_node_id,
  514. prev_entity.last_param_node_id, diagnose)) {
  515. return false;
  516. }
  517. return true;
  518. }
  519. } // namespace Carbon::Check