Skip to content

Commit a4da6ee

Browse files
xunilrjJoshuaBatty
andauthored
Removing todo const generics (#7391)
## Description Continuation of #6860. The summary of removed `todo!()` are: 1 - Some `todo!()` are actually unreachable. In future PRs I will try to model the AST better and remove these match arms; 2 - Some are trivial impls like spans; 3 - Some are trivial from traits like `PartialEq` and `PartialOrd`. Others will be removed in future PRs. ## Checklist - [x] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
1 parent 3c6f718 commit a4da6ee

File tree

21 files changed

+226
-114
lines changed

21 files changed

+226
-114
lines changed

sway-core/src/control_flow_analysis/analyze_return_paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn connect_declaration<'eng: 'cfg, 'cfg>(
227227
Ok(Some(entry_node))
228228
}
229229
ty::TyDecl::ConstGenericDecl(_) => {
230-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
230+
unreachable!("ConstGenericDecl is not reachable from AstNode")
231231
}
232232
ty::TyDecl::FunctionDecl(ty::FunctionDecl { decl_id, .. }) => {
233233
let fn_decl = decl_engine.get_function(decl_id);

sway-core/src/control_flow_analysis/dead_code_analysis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ fn connect_declaration<'eng: 'cfg, 'cfg>(
600600
}
601601
}
602602
ty::TyDecl::ConstGenericDecl(_) => {
603-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860");
603+
unreachable!("ConstGenericDecl is not reachable from AstNode")
604604
}
605605
ty::TyDecl::FunctionDecl(ty::FunctionDecl { decl_id, .. }) => {
606606
let fn_decl = decl_engine.get_function(decl_id);
@@ -2589,7 +2589,7 @@ fn allow_dead_code_ast_node(decl_engine: &DeclEngine, node: &ty::TyAstNode) -> b
25892589
allow_dead_code(decl_engine.get_configurable(decl_id).attributes.clone())
25902590
}
25912591
ty::TyDecl::ConstGenericDecl(_) => {
2592-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
2592+
unreachable!("ConstGenericDecl is not reachable from AstNode")
25932593
}
25942594
ty::TyDecl::TraitTypeDecl(ty::TraitTypeDecl { decl_id, .. }) => {
25952595
allow_dead_code(decl_engine.get_type(decl_id).attributes.clone())

sway-core/src/ir_generation/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl<'a> FnCompiler<'a> {
366366
unreachable!()
367367
}
368368
ty::TyDecl::ConstGenericDecl(_) => {
369-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
369+
unreachable!("ConstGenericDecl is not reachable from AstNode")
370370
}
371371
ty::TyDecl::EnumDecl(ty::EnumDecl { decl_id, .. }) => {
372372
let ted = self.engines.de().get_enum(decl_id);

sway-core/src/language/parsed/declaration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl Declaration {
232232
| Declaration::TraitTypeDeclaration(_)
233233
| Declaration::TraitFnDeclaration(_) => Visibility::Public,
234234
Declaration::ConstGenericDeclaration(_) => {
235-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
235+
unreachable!("Const generics do not have visibility")
236236
}
237237
}
238238
}

sway-core/src/language/ty/ast_node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl TyAstNode {
274274
}
275275
}
276276
TyDecl::ConstGenericDecl(_) => {
277-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
277+
unreachable!("ConstGenericDecl is not reachable from AstNode")
278278
}
279279
TyDecl::TraitTypeDecl(_) => {}
280280
TyDecl::FunctionDecl(decl) => {
@@ -336,7 +336,7 @@ impl TyAstNode {
336336
TyDecl::ConstantDecl(_decl) => {}
337337
TyDecl::ConfigurableDecl(_decl) => {}
338338
TyDecl::ConstGenericDecl(_decl) => {
339-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
339+
unreachable!("ConstGenericDecl is not reachable from AstNode")
340340
}
341341
TyDecl::TraitTypeDecl(_) => {}
342342
TyDecl::FunctionDecl(decl) => {

sway-core/src/language/ty/declaration/const_generic.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use crate::{
22
decl_engine::MaterializeConstGenerics,
3+
engine_threading::HashWithEngines,
34
has_changes,
45
language::{parsed::ConstGenericDeclaration, ty::TyExpression, CallPath},
56
semantic_analysis::{TypeCheckAnalysis, TypeCheckAnalysisContext},
6-
HasChanges, SubstTypes, TypeId,
7+
Engines, HasChanges, SubstTypes, TypeId,
78
};
89
use serde::{Deserialize, Serialize};
10+
use std::hash::{Hash as _, Hasher};
911
use sway_error::handler::{ErrorEmitted, Handler};
10-
use sway_types::{BaseIdent, Ident, Named, Span, Spanned};
12+
use sway_types::{Ident, Named, Span, Spanned};
1113

1214
use super::TyDeclParsedType;
1315

@@ -19,6 +21,20 @@ pub struct TyConstGenericDecl {
1921
pub value: Option<TyExpression>,
2022
}
2123

24+
impl HashWithEngines for TyConstGenericDecl {
25+
fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
26+
let type_engine = engines.te();
27+
let TyConstGenericDecl {
28+
call_path,
29+
return_type,
30+
span: _,
31+
value: _,
32+
} = self;
33+
call_path.hash(state);
34+
type_engine.get(*return_type).hash(state, engines);
35+
}
36+
}
37+
2238
impl SubstTypes for TyConstGenericDecl {
2339
fn subst_inner(&mut self, ctx: &crate::SubstTypesContext) -> crate::HasChanges {
2440
has_changes! {
@@ -76,12 +92,6 @@ impl TypeCheckAnalysis for TyConstGenericDecl {
7692
}
7793
}
7894

79-
impl TyConstGenericDecl {
80-
pub fn name(&self) -> &BaseIdent {
81-
&self.call_path.suffix
82-
}
83-
}
84-
8595
impl Named for TyConstGenericDecl {
8696
fn name(&self) -> &Ident {
8797
&self.call_path.suffix

sway-core/src/language/ty/declaration/declaration.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ impl HashWithEngines for TyDecl {
211211
TyDecl::ConfigurableDecl(ConfigurableDecl { decl_id, .. }) => {
212212
decl_engine.get(decl_id).hash(state, engines);
213213
}
214-
TyDecl::ConstGenericDecl(_) => {
215-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
214+
TyDecl::ConstGenericDecl(ConstGenericDecl { decl_id }) => {
215+
decl_engine.get(decl_id).hash(state, engines);
216216
}
217217
TyDecl::TraitTypeDecl(TraitTypeDecl { decl_id, .. }) => {
218218
decl_engine.get(decl_id).hash(state, engines);
@@ -293,9 +293,7 @@ impl SubstTypes for TyDecl {
293293
| TyDecl::StorageDecl(_)
294294
| TyDecl::GenericTypeForFunctionScope(_)
295295
| TyDecl::ErrorRecovery(..) => HasChanges::No,
296-
TyDecl::ConstGenericDecl(_) => {
297-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
298-
}
296+
TyDecl::ConstGenericDecl(_) => HasChanges::No,
299297
}
300298
}
301299
}
@@ -311,8 +309,9 @@ impl SpannedWithEngines for TyDecl {
311309
let decl = engines.de().get(decl_id);
312310
decl.span.clone()
313311
}
314-
TyDecl::ConstGenericDecl(_) => {
315-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
312+
TyDecl::ConstGenericDecl(ConstGenericDecl { decl_id }) => {
313+
let decl = engines.de().get(decl_id);
314+
decl.span.clone()
316315
}
317316
TyDecl::TraitTypeDecl(TraitTypeDecl { decl_id }) => {
318317
engines.de().get_type(decl_id).span.clone()
@@ -501,9 +500,6 @@ impl CollectTypesMetadata for TyDecl {
501500
return Ok(vec![]);
502501
}
503502
}
504-
TyDecl::ConstGenericDecl(_) => {
505-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
506-
}
507503
TyDecl::ErrorRecovery(..)
508504
| TyDecl::StorageDecl(_)
509505
| TyDecl::TraitDecl(_)
@@ -514,7 +510,8 @@ impl CollectTypesMetadata for TyDecl {
514510
| TyDecl::AbiDecl(_)
515511
| TyDecl::TypeAliasDecl(_)
516512
| TyDecl::TraitTypeDecl(_)
517-
| TyDecl::GenericTypeForFunctionScope(_) => vec![],
513+
| TyDecl::GenericTypeForFunctionScope(_)
514+
| TyDecl::ConstGenericDecl(_) => vec![],
518515
};
519516
Ok(metadata)
520517
}
@@ -529,8 +526,8 @@ impl GetDeclIdent for TyDecl {
529526
TyDecl::ConfigurableDecl(ConfigurableDecl { decl_id }) => {
530527
Some(engines.de().get_configurable(decl_id).name().clone())
531528
}
532-
TyDecl::ConstGenericDecl(_) => {
533-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
529+
TyDecl::ConstGenericDecl(ConstGenericDecl { decl_id }) => {
530+
Some(engines.de().get_const_generic(decl_id).name().clone())
534531
}
535532
TyDecl::TraitTypeDecl(TraitTypeDecl { decl_id }) => {
536533
Some(engines.de().get_type(decl_id).name().clone())
@@ -915,7 +912,7 @@ impl TyDecl {
915912
decl_engine.get_configurable(decl_id).visibility
916913
}
917914
TyDecl::ConstGenericDecl(_) => {
918-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
915+
unreachable!("Const generics do not have visibility");
919916
}
920917
TyDecl::StructDecl(StructDecl { decl_id, .. }) => {
921918
decl_engine.get_struct(decl_id).visibility

sway-core/src/language/ty/expression/expression_variant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,8 +1525,8 @@ impl DisplayWithEngines for TyExpressionVariant {
15251525
impl DebugWithEngines for TyExpressionVariant {
15261526
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
15271527
let s = match self {
1528-
TyExpressionVariant::ConstGenericExpression { .. } => {
1529-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
1528+
TyExpressionVariant::ConstGenericExpression { call_path, .. } => {
1529+
format!("const generic {}", call_path.span().as_str())
15301530
}
15311531
TyExpressionVariant::Literal(lit) => format!("literal {lit}"),
15321532
TyExpressionVariant::FunctionApplication {

sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,12 @@ impl TyDecl {
536536
ty: GenericArgument::Type(GenericTypeArgument {
537537
initial_type_id: ty.initial_type_id(),
538538
type_id: new_ty,
539-
call_path_tree: ty.call_path_tree().cloned(),
539+
call_path_tree: ty
540+
.as_type_argument()
541+
.unwrap()
542+
.call_path_tree
543+
.as_ref()
544+
.cloned(),
540545
span: ty.span(),
541546
}),
542547
visibility: decl.visibility,
@@ -553,7 +558,7 @@ impl TyDecl {
553558
unreachable!();
554559
}
555560
parsed::Declaration::ConstGenericDeclaration(_) => {
556-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
561+
unreachable!("ConstGenericDecl is not reachable from AstNode")
557562
}
558563
};
559564

@@ -580,7 +585,7 @@ impl TypeCheckAnalysis for TyDecl {
580585
const_decl.type_check_analyze(handler, ctx)?;
581586
}
582587
TyDecl::ConstGenericDecl(_) => {
583-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
588+
unreachable!("ConstGenericDecl is not reachable from AstNode")
584589
}
585590
TyDecl::FunctionDecl(node) => {
586591
let fn_decl = ctx.engines.de().get_function(&node.decl_id);
@@ -640,7 +645,7 @@ impl TypeCheckFinalization for TyDecl {
640645
config_decl.type_check_finalize(handler, ctx)?;
641646
}
642647
TyDecl::ConstGenericDecl(_) => {
643-
todo!("Will be implemented by https://github.com/FuelLabs/sway/issues/6860")
648+
unreachable!("ConstGenericDecl is not reachable from AstNode")
644649
}
645650
TyDecl::FunctionDecl(node) => {
646651
let mut fn_decl = (*ctx.engines.de().get_function(&node.decl_id)).clone();

sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,12 @@ fn type_check_size_of_type(
695695
type_id,
696696
initial_type_id,
697697
span: targ.span(),
698-
call_path_tree: targ.call_path_tree().cloned(),
698+
call_path_tree: targ
699+
.as_type_argument()
700+
.unwrap()
701+
.call_path_tree
702+
.as_ref()
703+
.cloned(),
699704
})],
700705
span,
701706
};
@@ -745,7 +750,12 @@ fn type_check_is_reference_type(
745750
type_id,
746751
initial_type_id,
747752
span: targ.span(),
748-
call_path_tree: targ.call_path_tree().cloned(),
753+
call_path_tree: targ
754+
.as_type_argument()
755+
.unwrap()
756+
.call_path_tree
757+
.as_ref()
758+
.cloned(),
749759
})],
750760
span,
751761
};
@@ -795,7 +805,12 @@ fn type_check_assert_is_str_array(
795805
type_id,
796806
initial_type_id,
797807
span: targ.span(),
798-
call_path_tree: targ.call_path_tree().cloned(),
808+
call_path_tree: targ
809+
.as_type_argument()
810+
.unwrap()
811+
.call_path_tree
812+
.as_ref()
813+
.cloned(),
799814
})],
800815
span,
801816
};
@@ -975,7 +990,12 @@ fn type_check_gtf(
975990
type_id,
976991
initial_type_id,
977992
span: targ.span(),
978-
call_path_tree: targ.call_path_tree().cloned(),
993+
call_path_tree: targ
994+
.as_type_argument()
995+
.unwrap()
996+
.call_path_tree
997+
.as_ref()
998+
.cloned(),
979999
})],
9801000
span,
9811001
},
@@ -1184,7 +1204,12 @@ fn type_check_state_store_word(
11841204
type_id,
11851205
initial_type_id,
11861206
span: span.clone(),
1187-
call_path_tree: targ.call_path_tree().cloned(),
1207+
call_path_tree: targ
1208+
.as_type_argument()
1209+
.unwrap()
1210+
.call_path_tree
1211+
.as_ref()
1212+
.cloned(),
11881213
})
11891214
});
11901215
let intrinsic_function = ty::TyIntrinsicFunctionKind {
@@ -1275,7 +1300,12 @@ fn type_check_state_quad(
12751300
type_id,
12761301
initial_type_id,
12771302
span: span.clone(),
1278-
call_path_tree: targ.call_path_tree().cloned(),
1303+
call_path_tree: targ
1304+
.as_type_argument()
1305+
.unwrap()
1306+
.call_path_tree
1307+
.as_ref()
1308+
.cloned(),
12791309
})
12801310
});
12811311
let intrinsic_function = ty::TyIntrinsicFunctionKind {
@@ -1692,7 +1722,12 @@ fn type_check_ptr_ops(
16921722
type_id,
16931723
initial_type_id,
16941724
span: targ.span(),
1695-
call_path_tree: targ.call_path_tree().cloned(),
1725+
call_path_tree: targ
1726+
.as_type_argument()
1727+
.unwrap()
1728+
.call_path_tree
1729+
.as_ref()
1730+
.cloned(),
16961731
})],
16971732
span,
16981733
},
@@ -1756,7 +1791,12 @@ fn type_check_smo(
17561791
type_id,
17571792
initial_type_id,
17581793
span: span.clone(),
1759-
call_path_tree: targ.call_path_tree().cloned(),
1794+
call_path_tree: targ
1795+
.as_type_argument()
1796+
.unwrap()
1797+
.call_path_tree
1798+
.as_ref()
1799+
.cloned(),
17601800
})
17611801
});
17621802

0 commit comments

Comments
 (0)