Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves error message when log is used in predicate. #4484

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion sway-core/src/ir_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod storage;
mod types;

use sway_error::error::CompileError;
use sway_ir::Context;
use sway_ir::{Context, Kind};
use sway_types::span::Span;

pub(crate) use purity::{check_function_purity, PurityEnv};
Expand Down Expand Up @@ -47,6 +47,13 @@ pub fn compile_program(
.collect();

let mut ctx = Context::default();
ctx.program_kind = match kind {
ty::TyProgramKind::Script { .. } => Kind::Script,
ty::TyProgramKind::Predicate { .. } => Kind::Predicate,
ty::TyProgramKind::Contract { .. } => Kind::Contract,
ty::TyProgramKind::Library { .. } => Kind::Library,
};

match kind {
// predicates and scripts have the same codegen, their only difference is static
// type-check time checks.
Expand Down
7 changes: 7 additions & 0 deletions sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,13 @@ impl<'eng> FnCompiler<'eng> {
}
}
Intrinsic::Log => {
if context.program_kind == Kind::Predicate {
return Err(CompileError::DisallowedIntrinsicInPredicate {
intrinsic: kind.to_string(),
span: span.clone(),
});
}

// The log value and the log ID are just Value.
let log_val = self.compile_expression_to_value(context, md_mgr, &arguments[0])?;
let log_id = match self.logged_types_map.get(&arguments[0].return_type) {
Expand Down
3 changes: 3 additions & 0 deletions sway-error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ pub enum CompileError {
CallingPrivateLibraryMethod { name: String, span: Span },
#[error("Using \"while\" in a predicate is not allowed.")]
DisallowedWhileInPredicate { span: Span },
#[error("Using intrinsic \"{intrinsic}\" in a predicate is not allowed.")]
DisallowedIntrinsicInPredicate { intrinsic: String, span: Span },
#[error("Possibly non-zero amount of coins transferred to non-payable contract method \"{fn_name}\".")]
CoinsPassedToNonPayableMethod { fn_name: Ident, span: Span },
#[error(
Expand Down Expand Up @@ -799,6 +801,7 @@ impl Spanned for CompileError {
DisallowedControlFlowInstruction { span, .. } => span.clone(),
CallingPrivateLibraryMethod { span, .. } => span.clone(),
DisallowedWhileInPredicate { span } => span.clone(),
DisallowedIntrinsicInPredicate { span, .. } => span.clone(),
CoinsPassedToNonPayableMethod { span, .. } => span.clone(),
TraitImplPayabilityMismatch { span, .. } => span.clone(),
ConfigurableInLibrary { span } => span.clone(),
Expand Down
7 changes: 5 additions & 2 deletions sway-ir/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use rustc_hash::FxHashMap;

use crate::{
asm::AsmBlockContent, block::BlockContent, function::FunctionContent,
local_var::LocalVarContent, metadata::Metadatum, module::ModuleContent, module::ModuleIterator,
value::ValueContent, Type, TypeContent,
local_var::LocalVarContent, metadata::Metadatum, module::Kind, module::ModuleContent,
module::ModuleIterator, value::ValueContent, Type, TypeContent,
};

/// The main IR context handle.
Expand All @@ -30,6 +30,8 @@ pub struct Context {
pub(crate) asm_blocks: Arena<AsmBlockContent>,
pub(crate) metadata: Arena<Metadatum>,

pub program_kind: Kind,

next_unique_sym_tag: u64,
}

Expand All @@ -46,6 +48,7 @@ impl Default for Context {
asm_blocks: Default::default(),
metadata: Default::default(),
next_unique_sym_tag: Default::default(),
program_kind: Kind::Contract,
};
Type::create_basic_types(&mut def);
def
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-58F9DCE1E1853B21'

[[package]]
name = 'predicate_log'
source = 'member'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-58F9DCE1E1853B21'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "predicate_log"
entry = "main.sw"
implicit-std = false

[dependencies]
std = { path = "../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
predicate;

use std::{
inputs::input_owner,
logging::log,
};

fn main() -> bool {
log::<Address>(input_owner(0).unwrap());

true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "fail"

# check: __log::<T>(value);
# nextln: $()Using intrinsic "log" in a predicate is not allowed.
Loading