Skip to content
Open
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
10 changes: 8 additions & 2 deletions src/analyze/annot_fn.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use pretty::{termcolor, Pretty};
use rustc_hir::pat_util::EnumerateAndAdjustIterator as _;
use rustc_hir::{def_id::LocalDefId, HirId};
use rustc_index::IndexVec;
use rustc_middle::mir;
Expand Down Expand Up @@ -377,8 +378,13 @@ impl<'a, 'tcx> AnnotFnTranslator<'a, 'tcx> {
PatKind::Binding(_, hir_id, _, None) => {
self.env.insert(hir_id, param);
}
PatKind::TupleStruct(_, subpats, _) | PatKind::Tuple(subpats, _) => {
for (idx, subpat) in subpats.iter().enumerate() {
PatKind::TupleStruct(_, subpats, dotdot_pos) | PatKind::Tuple(subpats, dotdot_pos) => {
let pat_ty = self.pat_ty(pat);
let field_count = match pat_ty.ty_adt_def() {
Some(adt) => adt.non_enum_variant().fields.len(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Select the matched enum variant before counting fields

When an annotated function uses an irrefutable tuple-style enum pattern, such as a single-variant enum E { V(i32) } with fn f(E::V(_): E), the generated formula function preserves that parameter pattern and reaches this branch. Its pattern type returns the enum AdtDef, but non_enum_variant() is only valid for structs/unions and panics for enums, so translating even a trivial requires(true) now ICEs despite no .. being present. Resolve the tuple-struct pattern's matched variant and count that variant's fields instead.

Useful? React with 馃憤聽/ 馃憥.

None => pat_ty.tuple_fields().len(),
};
for (idx, subpat) in subpats.iter().enumerate_and_adjust(field_count, dotdot_pos) {
let field_term = param.clone().tuple_proj(idx);
self.build_env_from_pat(field_term, subpat);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/fail/annot_param_rest_pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@error-in-other-file: Unsat
//@compile-flags: -C debug-assertions=off

#[thrust_macros::requires(true)]
#[thrust_macros::ensures(result == d)]
fn last((_a, .., _c, d): (i64, i64, i64, i64)) -> i64 {
_c
}

fn main() {
last((1, 2, 3, 4));
}
12 changes: 12 additions & 0 deletions tests/ui/pass/annot_param_rest_pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@check-pass
//@compile-flags: -C debug-assertions=off

#[thrust_macros::requires(true)]
#[thrust_macros::ensures(result == d)]
fn last((_a, .., _c, d): (i64, i64, i64, i64)) -> i64 {
d
}

fn main() {
last((1, 2, 3, 4));
}
Loading