(Instruction decode in PDP emulator)
I have a huge match set where each arm returns a function pointer and a name. Here is an extract
match (inst & 0o170000) >> 12 {
0o00 => match (inst & 0o007700) >> 6 {
0o00 => match inst & 0o77 {
00 => (Cpu::halt, "halt"),
01 => (Cpu::halt, "wait"),
02 => (Cpu::halt, "rti"),
03 => (Cpu::halt, "bpt"),
04 => (Cpu::halt, "iot"),
every arm of these matches says (Cpu::halt,"xxx"). This happily compiles. But of course I want real functions in there so I changed the first one.
match (inst & 0o170000) >> 12 {
0o00 => match (inst & 0o007700) >> 6 {
0o00 => match inst & 0o77 {
00 => (Cpu::mov, "halt"),
01 => (Cpu::halt, "wait"),
02 => (Cpu::halt, "rti"),
03 => (Cpu::halt, "bpt"),
04 => (Cpu::halt, "iot"),
Both halt and mov have the same signatures
impl Cpu{
pub fn halt(&mut self, z:Word)->Result<(), Exception>{Ok(())}
pub fn mov(&mut self, z:Word) ->Result<(), Exception>{
let (mut ss,mut dd) = self.decode_ssdd(z, false)?;
let t = self.fetch_word(&mut ss)?;
self.psw &= !statusflags::PS_V;
self.set_status(t);
self.store_word(&mut dd, t)?;
Ok(())
}
}
but rustc then complains
error[E0308]: `match` arms have incompatible types
--> src\cpu.rs:83:31
|
81 | 0o00 => match inst & 0o77 {
| _____________________________-
82 | | 00 => (Cpu::mov, "halt"),
| | ------------------ this is found to be of type `(for<'r> fn(&'r mut cpu::Cpu, u16) -> std::result::Result<(), common::Exception> {instructions::<impl cpu::Cpu>::mov}, &str)`
83 | | 01 => (Cpu::halt, "wait"),
| | ^^^^^^^^^^^^^^^^^^^ expected fn item, found a different fn item
84 | | 02 => (Cpu::halt, "rti"),
... |
90 | | _ => unreachable!(),
91 | | },
| |_____________________- `match` arms have incompatible types
|
= note: expected type `(for<'r> fn(&'r mut cpu::Cpu, _) -> std::result::Result<_, _> {instructions::<impl cpu::Cpu>::mov}, &str)`
found tuple `(for<'r> fn(&'r mut cpu::Cpu, _) -> std::result::Result<_, _> {instructions::<impl cpu::Cpu>::halt}, &'static str)`
the essential part of the error seems to be the last 2 lines that says the difference between the tuples it found is that one is (fn, &str) and the other is (fn, &'static str). And yet they are identical except the function name.
I also note the the earlier error says "expected fn item, found a different fn item" but thats not what the last 2 lines say.