|
| 1 | +use crate::{ |
| 2 | + migrations::MutProgramInfo, |
| 3 | + modifying::*, |
| 4 | + visiting::{ |
| 5 | + InvalidateTypedElement, LexedFnCallInfoMut, ProgramVisitorMut, TreesVisitorMut, |
| 6 | + TyFnCallInfo, VisitingContext, |
| 7 | + }, |
| 8 | +}; |
| 9 | +use anyhow::{bail, Ok, Result}; |
| 10 | +use sway_ast::Expr; |
| 11 | +use sway_core::language::{ty::TyExpression, CallPath}; |
| 12 | +use sway_types::{Span, Spanned}; |
| 13 | + |
| 14 | +use super::{ContinueMigrationProcess, DryRun, MigrationStep, MigrationStepKind}; |
| 15 | + |
| 16 | +// NOTE: We do not fully support cases when `b256::from` is nested within another `b256::from`. |
| 17 | +// E.g.: `b256::from(Bytes::from(b256::from(nested_bytes)))`. |
| 18 | +// In such cases, only the outermost `b256::from` will be migrated. |
| 19 | +// In practice, this does not happen. |
| 20 | + |
| 21 | +#[allow(dead_code)] |
| 22 | +pub(super) const REPLACE_B256_FROM_BYTES_TO_TRY_FROM_BYTES_STEP: MigrationStep = MigrationStep { |
| 23 | + title: "Replace calls to `b256::from(<bytes>)` with `b256::try_from(<bytes>).unwrap()`", |
| 24 | + duration: 0, |
| 25 | + kind: MigrationStepKind::CodeModification( |
| 26 | + replace_b256_from_bytes_to_try_from_bytes_step, |
| 27 | + &[], |
| 28 | + ContinueMigrationProcess::IfNoManualMigrationActionsNeeded, |
| 29 | + ), |
| 30 | + help: &[ |
| 31 | + "Migration will replace all the calls to `b256::from(<bytes>)` with", |
| 32 | + "`b256::try_from(<bytes>).unwrap()`.", |
| 33 | + " ", |
| 34 | + "E.g.:", |
| 35 | + " let result = b256::from(some_bytes);", |
| 36 | + "will become:", |
| 37 | + " let result = b256::try_from(some_bytes).unwrap();", |
| 38 | + ], |
| 39 | +}; |
| 40 | + |
| 41 | +fn replace_b256_from_bytes_to_try_from_bytes_step( |
| 42 | + program_info: &mut MutProgramInfo, |
| 43 | + dry_run: DryRun, |
| 44 | +) -> Result<Vec<Span>> { |
| 45 | + struct Visitor; |
| 46 | + impl TreesVisitorMut<Span> for Visitor { |
| 47 | + fn visit_fn_call( |
| 48 | + &mut self, |
| 49 | + ctx: &VisitingContext, |
| 50 | + lexed_fn_call: &mut Expr, |
| 51 | + ty_fn_call: Option<&TyExpression>, |
| 52 | + output: &mut Vec<Span>, |
| 53 | + ) -> Result<InvalidateTypedElement> { |
| 54 | + let lexed_fn_call_info = LexedFnCallInfoMut::new(lexed_fn_call)?; |
| 55 | + let ty_fn_call_info = ty_fn_call |
| 56 | + .map(|ty_fn_call| TyFnCallInfo::new(ctx.engines.de(), ty_fn_call)) |
| 57 | + .transpose()?; |
| 58 | + |
| 59 | + // We need the typed info in order to ensure that the `from` function |
| 60 | + // is really the `b256::from(Bytes)` function. |
| 61 | + let Some(ty_fn_call_info) = ty_fn_call_info else { |
| 62 | + return Ok(InvalidateTypedElement::No); |
| 63 | + }; |
| 64 | + |
| 65 | + // Note that neither the implementing for type not the trait are a |
| 66 | + // part of the `from` function call path. All associated `from` functions |
| 67 | + // in the `std::bytes` will have the same call path. |
| 68 | + // We will filter further below to target exactly the `<From<Bytes> for b256>::from`. |
| 69 | + let from_call_path = CallPath::fullpath(&["std", "bytes", "from"]); |
| 70 | + |
| 71 | + let Some(implementing_for_type_id) = ty_fn_call_info.fn_decl.implementing_for_typeid |
| 72 | + else { |
| 73 | + return Ok(InvalidateTypedElement::No); |
| 74 | + }; |
| 75 | + |
| 76 | + // This check is sufficient. The only `from` in `std::bytes` that |
| 77 | + // satisfies it is the `<From<Bytes> for b256>::from`. |
| 78 | + if !(ty_fn_call_info.fn_decl.call_path == from_call_path |
| 79 | + && implementing_for_type_id == ctx.engines.te().id_of_b256()) |
| 80 | + { |
| 81 | + return Ok(InvalidateTypedElement::No); |
| 82 | + } |
| 83 | + |
| 84 | + // We have found a `b256::from(Bytes)` call. |
| 85 | + output.push(lexed_fn_call_info.func.span()); |
| 86 | + |
| 87 | + if ctx.dry_run == DryRun::Yes { |
| 88 | + return Ok(InvalidateTypedElement::No); |
| 89 | + } |
| 90 | + |
| 91 | + let lexed_from_call_path = match lexed_fn_call { |
| 92 | + Expr::FuncApp { func, args: _ } => match func.as_mut() { |
| 93 | + Expr::Path(path_expr) => path_expr, |
| 94 | + _ => bail!("`func` of the `lexed_fn_call` must be of the variant `Expr::Path`."), |
| 95 | + }, |
| 96 | + _ => bail!("`lexed_fn_call` must be of the variant `Expr::FuncApp`."), |
| 97 | + }; |
| 98 | + |
| 99 | + // Rename the call to `from` to `try_from`. |
| 100 | + let from_ident = lexed_from_call_path.last_segment_mut(); |
| 101 | + modify(from_ident).set_name("try_from"); |
| 102 | + |
| 103 | + // The call to `try_from` becomes the target of the `unwrap` method call. |
| 104 | + let target = lexed_fn_call.clone(); |
| 105 | + let insert_span = Span::empty_at_end(&target.span()); |
| 106 | + *lexed_fn_call = New::method_call(insert_span, target, "unwrap"); |
| 107 | + |
| 108 | + Ok(InvalidateTypedElement::Yes) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + ProgramVisitorMut::visit_program(program_info, dry_run, &mut Visitor {}) |
| 113 | +} |
0 commit comments