Skip to content

Commit ec6a013

Browse files
committed
Add file and line number info for eprintln
1 parent 61469aa commit ec6a013

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed

src/generate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn generate(syntax: Vec<(Syntax, Span)>) -> TokenStream {
2323
fn generate_opcode(opcode: Opcode, span: Span) -> TokenStream {
2424
let ident = Ident::new(opcode.to_string().as_ref(), span);
2525
quote_spanned!(span=>
26-
.push_opcode(::bitcoin::blockdata::opcodes::all::#ident)
26+
.push_opcode(::bitcoin::blockdata::opcodes::all::#ident, file!(), line!())
2727
)
2828
}
2929

@@ -41,6 +41,6 @@ fn generate_int(n: i64, span: Span) -> TokenStream {
4141

4242
fn generate_escape(expression: TokenStream, span: Span) -> TokenStream {
4343
quote_spanned!(span=>
44-
.push_expression(#expression)
44+
.push_expression(#expression, file!(), line!())
4545
)
4646
}

src/lib.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,27 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
148148

149149
pub struct Builder(pub BitcoinBuilder);
150150

151-
pub fn check_optimality(opcode: Opcode, next_opcode: Opcode) {
151+
pub fn check_optimality(opcode: Opcode, next_opcode: Opcode, file: &str, line: u32) {
152152
match (opcode, next_opcode) {
153-
(OP_PUSHNUM_1, OP_ADD) => eprintln!("Script can be optimized: 1 OP_ADD => OP_1ADD"),
154-
(OP_PUSHNUM_1, OP_SUB) => eprintln!("Script can be optimized: 1 OP_SUB => OP_1SUB"),
153+
(OP_PUSHNUM_1, OP_ADD) => eprintln!("Script at {}:{} can be optimized: 1 OP_ADD => OP_1ADD", file, line),
154+
(OP_PUSHNUM_1, OP_SUB) => eprintln!("Script at {}:{} can be optimized: 1 OP_SUB => OP_1SUB", file, line),
155155
(OP_DROP, OP_DROP) => {
156-
eprintln!("Script can be optimized: OP_DROP OP_DROP => OP_2DROP")
156+
eprintln!("Script at {}:{} can be optimized: OP_DROP OP_DROP => OP_2DROP", file, line)
157157
}
158-
(OP_PUSHBYTES_0, OP_ROLL) => eprintln!("Script can be optimized: Remove 0 OP_ROLL"),
158+
(OP_PUSHBYTES_0, OP_ROLL) => eprintln!("Script at {}:{} can be optimized: Remove 0 OP_ROLL", file, line),
159159
(OP_PUSHNUM_1, OP_ROLL) => {
160-
eprintln!("Script can be optimized: 1 OP_ROLL => OP_SWAP")
160+
eprintln!("Script at {}:{} can be optimized: 1 OP_ROLL => OP_SWAP", file, line)
161161
}
162162
(OP_PUSHNUM_2, OP_ROLL) => {
163-
eprintln!("Script can be optimized: 2 OP_ROLL => OP_ROT")
163+
eprintln!("Script at {}:{} can be optimized: 2 OP_ROLL => OP_ROT", file, line)
164164
}
165165
(OP_PUSHBYTES_0, OP_PICK) => {
166-
eprintln!("Script can be optimized: 0 OP_PICK => OP_DUP")
166+
eprintln!("Script at {}:{} can be optimized: 0 OP_PICK => OP_DUP", file, line)
167167
}
168168
(OP_PUSHBYTES_1, OP_PICK) => {
169-
eprintln!("Script can be optimized: 1 OP_PICK => OP_OVER")
169+
eprintln!("Script at {}:{} can be optimized: 1 OP_PICK => OP_OVER", file, line)
170170
}
171-
(OP_IF, OP_ELSE) => eprintln!("Script can be optimized: OP_IF OP_ELSE => OP_NOTIF"),
171+
(OP_IF, OP_ELSE) => eprintln!("Script at {}:{} can be optimized: OP_IF OP_ELSE => OP_NOTIF", file, line),
172172
(_, _) => (),
173173
}
174174
}
@@ -187,20 +187,20 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
187187
self.0.as_script()
188188
}
189189

190-
pub fn push_opcode(mut self, opcode: Opcode) -> Builder {
190+
pub fn push_opcode(mut self, opcode: Opcode, file: &str, line: u32) -> Builder {
191191
match self.as_script().instructions_minimal().last() {
192192
Some(instr_result) => match instr_result {
193193
Ok(instr) => match instr {
194194
bitcoin::script::Instruction::PushBytes(push_bytes) => {
195195
if push_bytes.as_bytes() == [] {
196-
check_optimality(::bitcoin::opcodes::all::OP_PUSHBYTES_0, opcode)
196+
check_optimality(::bitcoin::opcodes::all::OP_PUSHBYTES_0, opcode, file, line)
197197
}
198198
},
199199
bitcoin::script::Instruction::Op(previous_opcode) => {
200-
check_optimality(previous_opcode, opcode)
200+
check_optimality(previous_opcode, opcode, file, line)
201201
}
202202
},
203-
Err(_) => eprintln!("Script includes non-minimal pushes."),
203+
Err(_) => eprintln!("Script at {}:{} includes non-minimal pushes.", file, line),
204204
},
205205
None => (),
206206
};
@@ -223,7 +223,7 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
223223
self
224224
}
225225

226-
pub fn push_expression<T: Pushable>(self, expression: T) -> Builder {
226+
pub fn push_expression<T: Pushable>(self, expression: T, file: &str, line: u32) -> Builder {
227227
let last_opcode_index = match self.as_script().instruction_indices_minimal().last()
228228
{
229229
Some(instr_result) => match instr_result {
@@ -240,7 +240,7 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
240240
bitcoin::script::Instruction::Op(opcode) => Some((index, opcode)),
241241
},
242242
Err(_) => {
243-
eprintln!("Script includes non-minimal pushes.");
243+
eprintln!("Script at {}:{} includes non-minimal pushes.", file, line);
244244
None
245245
}
246246
},
@@ -258,12 +258,12 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
258258
Ok(instr) => match instr {
259259
bitcoin::script::Instruction::PushBytes(_) => (),
260260
bitcoin::script::Instruction::Op(opcode) => {
261-
check_optimality(previous_opcode, opcode)
261+
check_optimality(previous_opcode, opcode, file, line)
262262
}
263263
},
264-
Err(_) => eprintln!("Script includes non-minimal pushes."),
264+
Err(_) => eprintln!("Script at {}:{} includes non-minimal pushes.", file, line),
265265
},
266-
None => eprintln!("Script extends an empty script!"),
266+
None => eprintln!("Script at {}:{} extends an empty script!", file, line),
267267
};
268268
}
269269
builder
@@ -325,7 +325,7 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
325325
}
326326
},
327327
Err(_) => {
328-
eprintln!("Script includes non-minimal pushes.");
328+
eprintln!("A Script includes non-minimal pushes.");
329329
None
330330
}
331331
},
@@ -338,10 +338,10 @@ pub fn define_pushable(_: TokenStream) -> TokenStream {
338338
Ok(instr) => match instr {
339339
bitcoin::script::Instruction::PushBytes(_) => (),
340340
bitcoin::script::Instruction::Op(opcode) => {
341-
check_optimality(previous_opcode, opcode)
341+
check_optimality(previous_opcode, opcode, file!(), line!())
342342
}
343343
},
344-
Err(_) => eprintln!("Script includes non-minimal pushes."),
344+
Err(_) => eprintln!("A Script includes non-minimal pushes."),
345345
},
346346
None => (),
347347
}

src/parse.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ where
165165
Ok(instr) => match instr {
166166
bitcoin::script::Instruction::PushBytes(push_bytes) => {
167167
if push_bytes.as_bytes() == [] {
168-
pushable::check_optimality(previous_opcode, ::bitcoin::opcodes::all::OP_PUSHBYTES_0)
168+
pushable::check_optimality(previous_opcode, ::bitcoin::opcodes::all::OP_PUSHBYTES_0, file!(), line!())
169169
}
170170
},
171-
bitcoin::script::Instruction::Op(opcode) => pushable::check_optimality(previous_opcode, opcode),
171+
bitcoin::script::Instruction::Op(opcode) => pushable::check_optimality(previous_opcode, opcode, file!(), line!()),
172172
},
173-
Err(_) => eprintln!("Script includes non-minimal pushes."),
173+
Err(_) => eprintln!("Script at {}:{} includes non-minimal pushes.", file!(), line!()),
174174
},
175-
None => eprintln!("Script can be optimized: Inner block of a for loop iteration is empty."),
175+
None => eprintln!("Script at {}:{} can be optimized: Inner block of a for loop iteration is empty.", file!(), line!()),
176176
};
177177
}
178178
// Store last instruction of next_script as Opcode in last_opcode.
@@ -186,7 +186,7 @@ where
186186
},
187187
bitcoin::script::Instruction::Op(new_last_opcode) => last_opcode = Some(new_last_opcode),
188188
},
189-
Err(_) => eprintln!("Script includes non-minimal pushes."),
189+
Err(_) => eprintln!("Script at {}:{} includes non-minimal pushes.", file!(), line!()),
190190
},
191191
None => (),
192192
};

tests/test.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,35 @@ fn test_simple() {
195195
}
196196

197197
#[test]
198-
//#[should_panic]
198+
#[should_panic]
199199
// TODO: How to check whether some eprintln was called?
200200
fn test_non_optimal_opcodes() {
201+
202+
203+
204+
205+
201206
let script = script! {
202207
for i in 0..10 {
208+
if false {
209+
} else {
203210
OP_ROLL
204211
{ i }
212+
}
213+
}
214+
215+
for i in 0..4 {
216+
{i}
217+
OP_ROLL
205218
}
219+
220+
1
221+
OP_ROLL
222+
2
223+
OP_ROLL
224+
225+
OP_DROP
226+
OP_DROP
206227
};
207228

208229
println!("{:?}", script);

0 commit comments

Comments
 (0)