Skip to content

Commit fbde112

Browse files
committed
Allow constant struct fields and tuple indexing
1 parent c637cab commit fbde112

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/librustc/middle/const_eval.rs

+28
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,34 @@ pub fn eval_const_expr_partial(tcx: &ty::ctxt, e: &Expr) -> Result<const_val, St
567567
None => Ok(const_int(0i64))
568568
}
569569
}
570+
ast::ExprTupField(ref base, index) => {
571+
// Get the base tuple if it is constant
572+
if let Some(&ast::ExprTup(ref fields)) = lookup_const(tcx, &**base).map(|s| &s.node) {
573+
// Check that the given index is within bounds and evaluate its value
574+
if fields.len() > index.node {
575+
return eval_const_expr_partial(tcx, &*fields[index.node])
576+
} else {
577+
return Err("tuple index out of bounds".to_string())
578+
}
579+
}
580+
581+
Err("non-constant struct in constant expr".to_string())
582+
}
583+
ast::ExprField(ref base, field_name) => {
584+
// Get the base expression if it is a struct and it is constant
585+
if let Some(&ast::ExprStruct(_, ref fields, _)) = lookup_const(tcx, &**base)
586+
.map(|s| &s.node) {
587+
// Check that the given field exists and evaluate it
588+
if let Some(f) = fields.iter().find(|f|
589+
f.ident.node.as_str() == field_name.node.as_str()) {
590+
return eval_const_expr_partial(tcx, &*f.expr)
591+
} else {
592+
return Err("nonexistent struct field".to_string())
593+
}
594+
}
595+
596+
Err("non-constant struct in constant expr".to_string())
597+
}
570598
_ => Err("unsupported constant expr".to_string())
571599
}
572600
}

0 commit comments

Comments
 (0)