Skip to content

Commit 98cdaac

Browse files
authored
New encoding fixes (#5745)
## Description This PR contains three fixes: 1 - Fix to partial monomorphization. The fix was just removing a "quick-fix" that was merged and it is not needed anymore after #5684; 2 - To play safe, `compile_fn_call` cache will not be used for autogenerated code; 3 - `experimental-new-encoding` was not being copied in all cases. Two other small changes are: 1 - `Buffer` struct does not offer a generic `push` anymore, because this function was extremely deceiving for primitive datatypes which have memory layouts very different from the encoding memory layout (u16, u32 etc...); 2 - Changing name of one test to make the test more obvious and easier to run just one specific test. ## Checklist - [ ] 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). - [ ] 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). - [ ] I have requested a review from the relevant team or maintainers.
1 parent 9e51eeb commit 98cdaac

File tree

43 files changed

+532
-134
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+532
-134
lines changed

sway-core/src/build_config.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ impl BuildConfig {
114114
time_phases: false,
115115
metrics_outfile: None,
116116
optimization_level: OptLevel::Opt0,
117-
experimental: ExperimentalFlags::default(),
117+
experimental: ExperimentalFlags {
118+
new_encoding: false,
119+
},
118120
lsp_mode: None,
119121
}
120122
}
@@ -203,7 +205,7 @@ impl BuildConfig {
203205
}
204206
}
205207

206-
#[derive(Debug, Default, Clone, Copy)]
208+
#[derive(Debug, Clone, Copy)]
207209
pub struct ExperimentalFlags {
208210
pub new_encoding: bool,
209211
}

sway-core/src/ir_generation/const_eval.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,12 @@ mod tests {
11831183
fn assert_is_constant(is_constant: bool, prefix: &str, expr: &str) {
11841184
let engines = Engines::default();
11851185
let handler = Handler::default();
1186-
let mut context = Context::new(engines.se(), sway_ir::ExperimentalFlags::default());
1186+
let mut context = Context::new(
1187+
engines.se(),
1188+
sway_ir::ExperimentalFlags {
1189+
new_encoding: false,
1190+
},
1191+
);
11871192
let mut md_mgr = MetadataManager::default();
11881193
let core_lib = namespace::Root::from(namespace::Module {
11891194
name: Some(sway_types::Ident::new_no_span(

sway-core/src/ir_generation/function.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,18 @@ impl<'eng> FnCompiler<'eng> {
18231823
// args and type parameters. It's using the Sway types rather than IR types, which would
18241824
// be more accurate but also more fiddly.
18251825

1826-
let (fn_key, item) = if !callee.span().as_str().is_empty() {
1826+
let no_span = callee.span().as_str().is_empty();
1827+
let is_autogenerated = if let Some(s) = callee.span.source_id() {
1828+
context
1829+
.source_engine
1830+
.get_path(s)
1831+
.starts_with("<autogenerated>")
1832+
} else {
1833+
false
1834+
};
1835+
let (fn_key, item) = if no_span || is_autogenerated {
1836+
(None, None)
1837+
} else {
18271838
let fn_key = (
18281839
callee.span(),
18291840
callee
@@ -1833,12 +1844,11 @@ impl<'eng> FnCompiler<'eng> {
18331844
.collect(),
18341845
callee.type_parameters.iter().map(|tp| tp.type_id).collect(),
18351846
);
1847+
18361848
(
18371849
Some(fn_key.clone()),
18381850
self.recreated_fns.get(&fn_key).copied(),
18391851
)
1840-
} else {
1841-
(None, None)
18421852
};
18431853

18441854
let new_callee = match item {

sway-core/src/lib.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ pub fn parse(
9191
config: Option<&BuildConfig>,
9292
) -> Result<(lexed::LexedProgram, parsed::ParseProgram), ErrorEmitted> {
9393
match config {
94-
None => parse_in_memory(handler, engines, input),
94+
None => parse_in_memory(
95+
handler,
96+
engines,
97+
input,
98+
ExperimentalFlags {
99+
new_encoding: false,
100+
},
101+
),
95102
// When a `BuildConfig` is given,
96103
// the module source may declare `dep`s that must be parsed from other files.
97104
Some(config) => parse_module_tree(
@@ -190,14 +197,15 @@ fn parse_in_memory(
190197
handler: &Handler,
191198
engines: &Engines,
192199
src: Arc<str>,
200+
experimental: ExperimentalFlags,
193201
) -> Result<(lexed::LexedProgram, parsed::ParseProgram), ErrorEmitted> {
194202
let mut hasher = DefaultHasher::new();
195203
src.hash(&mut hasher);
196204
let hash = hasher.finish();
197205
let module = sway_parse::parse_file(handler, src, None)?;
198206

199207
let (kind, tree) = to_parsed_lang::convert_parse_tree(
200-
&mut to_parsed_lang::Context::default(),
208+
&mut to_parsed_lang::Context::new(BuildTarget::EVM, experimental),
201209
handler,
202210
engines,
203211
module.value.clone(),
@@ -481,7 +489,11 @@ pub fn parsed_to_ast(
481489
package_name: &str,
482490
retrigger_compilation: Option<Arc<AtomicBool>>,
483491
) -> Result<ty::TyProgram, ErrorEmitted> {
484-
let experimental = build_config.map(|x| x.experimental).unwrap_or_default();
492+
let experimental = build_config
493+
.map(|x| x.experimental)
494+
.unwrap_or(ExperimentalFlags {
495+
new_encoding: false,
496+
});
485497
let lsp_config = build_config.map(|x| x.lsp_mode.clone()).unwrap_or_default();
486498

487499
// Build the dependency graph for the submodules.

sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs

-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,6 @@ pub(crate) fn monomorphize_method_application(
767767
type_binding.as_mut().unwrap().type_arguments.to_vec_mut(),
768768
)?;
769769
let mut method = (*decl_engine.get_function(fn_ref)).clone();
770-
method.is_trait_method_dummy = false;
771770

772771
// Unify method type parameters with implementing type type parameters.
773772
if let Some(implementing_for_typeid) = method.implementing_for_typeid {

sway-core/src/semantic_analysis/namespace/module.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl Module {
133133
let attributes = Default::default();
134134
// convert to const decl
135135
let const_decl_id = to_parsed_lang::item_const_to_constant_declaration(
136-
&mut to_parsed_lang::Context::default(),
136+
&mut to_parsed_lang::Context::new(crate::BuildTarget::EVM, experimental),
137137
handler,
138138
engines,
139139
const_item,

sway-core/src/semantic_analysis/program.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ impl TyProgram {
5151
build_config: Option<&BuildConfig>,
5252
module_eval_order: ModuleEvaluationOrder,
5353
) -> Result<Self, ErrorEmitted> {
54-
let experimental = build_config.map(|x| x.experimental).unwrap_or_default();
54+
let experimental =
55+
build_config
56+
.map(|x| x.experimental)
57+
.unwrap_or(crate::ExperimentalFlags {
58+
new_encoding: false,
59+
});
5560

5661
let mut namespace = Namespace::init_root(initial_namespace);
5762
let mut ctx = TypeCheckContext::from_root(&mut namespace, engines, experimental)

sway-core/src/transform/to_parsed_lang/context.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{build_config::ExperimentalFlags, language::parsed::TreeType, BuildTarget};
22

3-
#[derive(Default)]
43
pub struct Context {
54
pub experimental: ExperimentalFlags,
65

@@ -30,7 +29,11 @@ impl Context {
3029
Self {
3130
build_target,
3231
experimental,
33-
..Default::default()
32+
module_has_configurable_block: std::default::Default::default(),
33+
destructured_struct_unique_suffix: std::default::Default::default(),
34+
destructured_tuple_unique_suffix: std::default::Default::default(),
35+
match_expression_matched_value_unique_suffix: std::default::Default::default(),
36+
program_type: std::default::Default::default(),
3437
}
3538
}
3639

sway-ir/src/bin/opt.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ fn main() -> Result<(), anyhow::Error> {
2626
let source_engine = SourceEngine::default();
2727

2828
// Parse it. XXX Improve this error message too.
29-
let mut ir = sway_ir::parser::parse(&input_str, &source_engine, ExperimentalFlags::default())?;
29+
let mut ir = sway_ir::parser::parse(
30+
&input_str,
31+
&source_engine,
32+
ExperimentalFlags {
33+
new_encoding: false,
34+
},
35+
)?;
3036

3137
// Perform optimisation passes in order.
3238
let mut passes = PassGroup::default();

sway-ir/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct Context<'eng> {
3939
pub experimental: ExperimentalFlags,
4040
}
4141

42-
#[derive(Copy, Clone, Default)]
42+
#[derive(Copy, Clone)]
4343
pub struct ExperimentalFlags {
4444
pub new_encoding: bool,
4545
}

sway-ir/src/irtype.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,12 @@ mod tests {
944944
static SOURCE_ENGINE: Lazy<SourceEngine> = Lazy::new(SourceEngine::default);
945945

946946
fn create_context() -> Context<'static> {
947-
Context::new(&SOURCE_ENGINE, ExperimentalFlags::default())
947+
Context::new(
948+
&SOURCE_ENGINE,
949+
ExperimentalFlags {
950+
new_encoding: false,
951+
},
952+
)
948953
}
949954

950955
/// Creates sample types that are not aggregates and do not point to

sway-ir/src/optimize.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ pub mod tests {
8181
"
8282
),
8383
&source_engine,
84-
ExperimentalFlags::default(),
84+
ExperimentalFlags {
85+
new_encoding: false,
86+
},
8587
)
8688
.unwrap();
8789

sway-ir/tests/tests.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ fn run_tests<F: Fn(&str, &mut Context) -> bool>(sub_dir: &str, opt_fn: F) {
2222
let input_bytes = std::fs::read(&path).unwrap();
2323
let input = String::from_utf8_lossy(&input_bytes);
2424

25-
let mut ir = sway_ir::parser::parse(&input, &source_engine, ExperimentalFlags::default())
26-
.unwrap_or_else(|parse_err| {
27-
println!("{}: {parse_err}", path.display());
28-
panic!()
29-
});
25+
let mut ir = sway_ir::parser::parse(
26+
&input,
27+
&source_engine,
28+
ExperimentalFlags {
29+
new_encoding: false,
30+
},
31+
)
32+
.unwrap_or_else(|parse_err| {
33+
println!("{}: {parse_err}", path.display());
34+
panic!()
35+
});
3036

3137
let first_line = input.split('\n').next().unwrap();
3238

sway-lib-core/generate.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ remove_generated_code "STRARRAY_ENCODE"
1313
START=1
1414
END=64
1515
for ((i=END;i>=START;i--)); do
16-
CODE="impl AbiEncode for str[$i] { fn abi_encode(self, ref mut buffer: Buffer) { use ::str::*; let s = from_str_array(self); let len = s.len(); let ptr = s.as_ptr(); let mut i = 0; while i < len { let byte = ptr.add::<u8>(i).read::<u8>(); buffer.push(byte); i += 1; } } }"
16+
CODE="impl AbiEncode for str[$i] { fn abi_encode(self, ref mut buffer: Buffer) { use ::str::*; let s = from_str_array(self); let len = s.len(); let ptr = s.as_ptr(); let mut i = 0; while i < len { let byte = ptr.add::<u8>(i).read::<u8>(); buffer.push_byte(byte); i += 1; } } }"
1717
sed -i "s/\/\/ BEGIN STRARRAY_ENCODE/\/\/ BEGIN STRARRAY_ENCODE\n$CODE/g" ./src/codec.sw
1818
done
1919

0 commit comments

Comments
 (0)