Skip to content

Commit

Permalink
Fix ICE when a struct variant enum contains multiple fields
Browse files Browse the repository at this point in the history
Fixes the second case of #19340.
  • Loading branch information
barosl committed Dec 11, 2014
1 parent 418d1bf commit 086c949
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/librustc/middle/borrowck/fragments.rs
Expand Up @@ -346,9 +346,10 @@ fn add_fragment_siblings_for_extension<'tcx>(this: &MoveData<'tcx>,
Rc<LoanPath<'tcx>>)>) {
let parent_ty = parent_lp.to_type();

let add_fragment_sibling_local = |field_name| {
let add_fragment_sibling_local = |field_name, variant_did| {
add_fragment_sibling_core(
this, tcx, gathered_fragments, parent_lp.clone(), mc, field_name, origin_lp);
this, tcx, gathered_fragments, parent_lp.clone(), mc, field_name, origin_lp,
variant_did);
};

match (&parent_ty.sty, enum_variant_info) {
Expand All @@ -363,7 +364,7 @@ fn add_fragment_siblings_for_extension<'tcx>(this: &MoveData<'tcx>,
for i in range(0, tuple_len) {
if i == tuple_idx { continue }
let field_name = mc::PositionalField(i);
add_fragment_sibling_local(field_name);
add_fragment_sibling_local(field_name, None);
}
}

Expand All @@ -376,7 +377,7 @@ fn add_fragment_siblings_for_extension<'tcx>(this: &MoveData<'tcx>,
continue;
}
let field_name = mc::NamedField(f.name);
add_fragment_sibling_local(field_name);
add_fragment_sibling_local(field_name, None);
}
}
mc::PositionalField(tuple_idx) => {
Expand All @@ -385,7 +386,7 @@ fn add_fragment_siblings_for_extension<'tcx>(this: &MoveData<'tcx>,
continue
}
let field_name = mc::PositionalField(i);
add_fragment_sibling_local(field_name);
add_fragment_sibling_local(field_name, None);
}
}
}
Expand Down Expand Up @@ -414,7 +415,7 @@ fn add_fragment_siblings_for_extension<'tcx>(this: &MoveData<'tcx>,
continue;
}
let field_name = mc::NamedField(variant_arg_ident.name);
add_fragment_sibling_local(field_name);
add_fragment_sibling_local(field_name, Some(variant_info.id));
}
}
mc::PositionalField(tuple_idx) => {
Expand All @@ -424,7 +425,7 @@ fn add_fragment_siblings_for_extension<'tcx>(this: &MoveData<'tcx>,
continue;
}
let field_name = mc::PositionalField(i);
add_fragment_sibling_local(field_name);
add_fragment_sibling_local(field_name, None);
}
}
}
Expand All @@ -447,10 +448,11 @@ fn add_fragment_sibling_core<'tcx>(this: &MoveData<'tcx>,
parent: Rc<LoanPath<'tcx>>,
mc: mc::MutabilityCategory,
new_field_name: mc::FieldName,
origin_lp: &Rc<LoanPath<'tcx>>) -> MovePathIndex {
origin_lp: &Rc<LoanPath<'tcx>>,
enum_variant_did: Option<ast::DefId>) -> MovePathIndex {
let opt_variant_did = match parent.kind {
LpDowncast(_, variant_did) => Some(variant_did),
LpVar(..) | LpUpvar(..) | LpExtend(..) => None,
LpVar(..) | LpUpvar(..) | LpExtend(..) => enum_variant_did,
};

let loan_path_elem = LpInterior(mc::InteriorField(new_field_name));
Expand Down
30 changes: 30 additions & 0 deletions src/test/run-pass/issue-19340-2.rs
@@ -0,0 +1,30 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

enum Homura {
Madoka {
name: String,
age: u32,
},
}

fn main() {
let homura = Homura::Madoka {
name: "Akemi".into_string(),
age: 14,
};

match homura {
Homura::Madoka {
name,
age,
} => (),
};
}

5 comments on commit 086c949

@bors
Copy link
Contributor

@bors bors commented on 086c949 Dec 12, 2014

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at barosl@086c949

@bors
Copy link
Contributor

@bors bors commented on 086c949 Dec 12, 2014

Choose a reason for hiding this comment

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

merging barosl/rust/enum-struct-variants-ice = 086c949 into auto

@bors
Copy link
Contributor

@bors bors commented on 086c949 Dec 12, 2014

Choose a reason for hiding this comment

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

barosl/rust/enum-struct-variants-ice = 086c949 merged ok, testing candidate = d2e2bd1

@bors
Copy link
Contributor

@bors bors commented on 086c949 Dec 12, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = d2e2bd1

Please sign in to comment.