Skip to content

Commit c3e5ea7

Browse files
authored
Forbid const generics log (#7510)
## Description Continuation of #7494. ## 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) - [x] 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. - [x] 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.
1 parent dc81a40 commit c3e5ea7

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{
1616
},
1717
semantic_analysis::TypeCheckContext,
1818
type_system::*,
19+
types::TypeMetadata,
1920
};
2021

2122
impl ty::TyIntrinsicFunctionKind {
@@ -1496,11 +1497,25 @@ fn type_check_log(
14961497
span,
14971498
}));
14981499
}
1499-
let ctx = ctx
1500-
.by_ref()
1501-
.with_help_text("")
1502-
.with_type_annotation(type_engine.new_unknown());
1503-
let exp = ty::TyExpression::type_check(handler, ctx, &arguments[0])?;
1500+
1501+
let exp = {
1502+
let ctx = ctx
1503+
.by_ref()
1504+
.with_help_text("")
1505+
.with_type_annotation(type_engine.new_unknown());
1506+
ty::TyExpression::type_check(handler, ctx, &arguments[0])?
1507+
};
1508+
1509+
// Forbid types with const generics on __log
1510+
let logged_expr = TypeMetadata::get_logged_expression(&exp, ctx.experimental.new_encoding)
1511+
.map_err(|err| handler.emit_err(err))?;
1512+
if logged_expr.return_type.has_const_generics(ctx.engines) {
1513+
let err = handler.emit_err(CompileError::ConstGenericNotSupportedHere {
1514+
span: logged_expr.span.clone(),
1515+
});
1516+
return Err(err);
1517+
}
1518+
15041519
let intrinsic_function = ty::TyIntrinsicFunctionKind {
15051520
kind,
15061521
arguments: vec![exp],

test/src/e2e_vm_tests/test_programs/should_fail/unsupported_const_generics/src/main.sw

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,6 @@ fn main() {
7979
let _: CrazyEnum<UNKNOWN> = CrazyEnum::A;
8080
let _: [u8; UNKNOWN] = [1u8];
8181
let _: str[UNKNOWN] = __to_str_array("abc");
82+
83+
__log(CrazyStruct::<1>{});
8284
}

test/src/e2e_vm_tests/test_programs/should_fail/unsupported_const_generics/stdout.snap

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ error
319319
80 | let _: [u8; UNKNOWN] = [1u8];
320320
| ^^^^^^^ Could not find symbol "UNKNOWN" in this scope.
321321
81 | let _: str[UNKNOWN] = __to_str_array("abc");
322-
82 | }
322+
82 |
323323
|
324324
____
325325

@@ -330,9 +330,21 @@ error
330330
80 | let _: [u8; UNKNOWN] = [1u8];
331331
81 | let _: str[UNKNOWN] = __to_str_array("abc");
332332
| ^^^^^^^ Could not find symbol "UNKNOWN" in this scope.
333-
82 | }
333+
82 |
334+
83 | __log(CrazyStruct::<1>{});
334335
|
335336
____
336337

337-
Aborting due to 23 errors.
338+
error
339+
--> test/src/e2e_vm_tests/test_programs/should_fail/unsupported_const_generics/src/main.sw:83:11
340+
|
341+
81 | let _: str[UNKNOWN] = __to_str_array("abc");
342+
82 |
343+
83 | __log(CrazyStruct::<1>{});
344+
| ^^^^^^^^^^^^^^^^^^ "const generics" are not supported here.
345+
84 | }
346+
|
347+
____
348+
349+
Aborting due to 24 errors.
338350
error: Failed to compile unsupported_const_generics

0 commit comments

Comments
 (0)