(
&mut self,
p: &ast::PatternMatchAs,
pc: &mut PatternContext,
)
| 6152 | } |
| 6153 | |
| 6154 | fn compile_pattern_as( |
| 6155 | &mut self, |
| 6156 | p: &ast::PatternMatchAs, |
| 6157 | pc: &mut PatternContext, |
| 6158 | ) -> CompileResult<()> { |
| 6159 | // If there is no sub-pattern, then it's an irrefutable match. |
| 6160 | if p.pattern.is_none() { |
| 6161 | if !pc.allow_irrefutable { |
| 6162 | if let Some(_name) = p.name.as_ref() { |
| 6163 | // TODO: This error message does not match cpython exactly |
| 6164 | // A name capture makes subsequent patterns unreachable. |
| 6165 | return Err(self.error(CodegenErrorType::UnreachablePattern( |
| 6166 | PatternUnreachableReason::NameCapture, |
| 6167 | ))); |
| 6168 | } else { |
| 6169 | // A wildcard makes remaining patterns unreachable. |
| 6170 | return Err(self.error(CodegenErrorType::UnreachablePattern( |
| 6171 | PatternUnreachableReason::Wildcard, |
| 6172 | ))); |
| 6173 | } |
| 6174 | } |
| 6175 | // If irrefutable matches are allowed, store the name (if any). |
| 6176 | return self.pattern_helper_store_name(p.name.as_ref(), pc); |
| 6177 | } |
| 6178 | |
| 6179 | // Otherwise, there is a sub-pattern. Duplicate the object on top of the stack. |
| 6180 | pc.on_top += 1; |
| 6181 | emit!(self, Instruction::Copy { i: 1 }); |
| 6182 | // Compile the sub-pattern. |
| 6183 | self.compile_pattern(p.pattern.as_ref().unwrap(), pc)?; |
| 6184 | // After success, decrement the on_top counter. |
| 6185 | pc.on_top -= 1; |
| 6186 | // Store the captured name (if any). |
| 6187 | self.pattern_helper_store_name(p.name.as_ref(), pc)?; |
| 6188 | Ok(()) |
| 6189 | } |
| 6190 | |
| 6191 | fn compile_pattern_star( |
| 6192 | &mut self, |
no test coverage detected