forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mir tycheck aggregate rvalues #13
Merged
nikomatsakis
merged 7 commits into
nikomatsakis:nll-master
from
Nashenas88:mir-tycheck-aggregate-rvalues-for-nll-master
Nov 12, 2017
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b30ec6c
Check rvalue aggregates during check_stmt in tycheck, add initial, (n…
Nashenas88 31ef286
Fix failing test
Nashenas88 ba0d264
Remove attributes and test comments accidentally left behind, add in …
Nashenas88 1041211
Normalize LvalueTy for ops and format code to satisfy tidy check
Nashenas88 658d4e3
only normalize operand types when in an ADT constructor
nikomatsakis 00444fc
avoid early return
nikomatsakis ad44d7e
handle the active field index in unions
nikomatsakis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -549,6 +549,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { | |
terr | ||
); | ||
} | ||
self.check_rvalue(mir, rv, location); | ||
} | ||
StatementKind::SetDiscriminant { | ||
ref lvalue, | ||
|
@@ -1008,6 +1009,102 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { | |
} | ||
} | ||
|
||
fn aggregate_field_ty(&mut self, ak: &Box<AggregateKind<'tcx>>, field: usize, location: Location) | ||
-> Result<Ty<'tcx>, FieldAccessError> | ||
{ | ||
let tcx = self.tcx(); | ||
|
||
let (variant, substs) = match **ak { | ||
AggregateKind::Adt(def, variant, substs, _) => { // handle unions? | ||
(&def.variants[variant], substs) | ||
}, | ||
AggregateKind::Closure(def_id, substs) => { | ||
return match substs.upvar_tys(def_id, tcx).nth(field) { | ||
Some(ty) => Ok(ty), | ||
None => Err(FieldAccessError::OutOfRange { | ||
field_count: substs.upvar_tys(def_id, tcx).count() | ||
}), | ||
} | ||
}, | ||
AggregateKind::Generator(def_id, substs, _) => { | ||
if let Some(ty) = substs.upvar_tys(def_id, tcx).nth(field) { | ||
return Ok(ty); | ||
} | ||
|
||
return match substs.field_tys(def_id, tcx).nth(field) { | ||
Some(ty) => Ok(ty), | ||
None => Err(FieldAccessError::OutOfRange { | ||
field_count: substs.field_tys(def_id, tcx).count() + 1 | ||
}), | ||
} | ||
}, | ||
AggregateKind::Array(ty) => { | ||
return Ok(ty); | ||
}, | ||
AggregateKind::Tuple => { | ||
unreachable!("This should have been covered in check_rvalues"); | ||
}, | ||
}; | ||
|
||
if let Some(field) = variant.fields.get(field) { | ||
Ok(self.normalize(&field.ty(tcx, substs), location)) | ||
} else { | ||
Err(FieldAccessError::OutOfRange { field_count: variant.fields.len() }) | ||
} | ||
} | ||
|
||
fn check_rvalue(&mut self, mir: &Mir<'tcx>, rv: &Rvalue<'tcx>, location: Location) { | ||
let tcx = self.tcx(); | ||
match rv { | ||
Rvalue::Aggregate(ak, ops) => { | ||
match **ak { | ||
// tuple rvalue field type is always the type of the op. Nothing to check here. | ||
AggregateKind::Tuple => { }, | ||
_ => { | ||
for (i, op) in ops.iter().enumerate() { | ||
let field_ty = match self.aggregate_field_ty(ak, i, location) { | ||
Ok(field_ty) => field_ty, | ||
Err(FieldAccessError::OutOfRange { field_count }) => { | ||
span_mirbug!( | ||
self, | ||
rv, | ||
"accessed field #{} but variant only has {}", | ||
i, | ||
field_count); | ||
continue; | ||
}, | ||
}; | ||
let op_ty = match op { | ||
Operand::Consume(lv) => lv.ty(mir, tcx).to_ty(tcx), | ||
Operand::Constant(c) => c.ty, | ||
}; | ||
if let Err(terr) = self.sub_types(op_ty, field_ty, location.at_successor_within_block()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line causes the follow error to occur during compilation:
Do I need to do additional work to get the types to match or is this a legitimate bug? |
||
span_mirbug!( | ||
self, | ||
rv, | ||
"{:?} is not a subtype of {:?}: {:?}", | ||
op_ty, | ||
field_ty, | ||
terr); | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
// FIXME: These other cases have to be implemented in future PRs | ||
Rvalue::Use(..) | | ||
Rvalue::Repeat(..) | | ||
Rvalue::Ref(..) | | ||
Rvalue::Len(..) | | ||
Rvalue::Cast(..) | | ||
Rvalue::BinaryOp(..) | | ||
Rvalue::CheckedBinaryOp(..) | | ||
Rvalue::UnaryOp(..) | | ||
Rvalue::Discriminant(..) | | ||
Rvalue::NullaryOp(..) => { } | ||
} | ||
} | ||
|
||
fn typeck_mir(&mut self, mir: &Mir<'tcx>) { | ||
self.last_span = mir.span; | ||
debug!("run_on_mir: {:?}", mir.span); | ||
|
27 changes: 27 additions & 0 deletions
27
src/test/compile-fail/nll/reference-carried-through-struct-field.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2017 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. | ||
//revisions: ast mir | ||
//[mir] compile-flags: -Z emit-end-regions -Z borrowck-mir -Z nll | ||
|
||
#![allow(unused_assignments)] | ||
|
||
struct Wrap<'a> { w: &'a mut u32 } | ||
|
||
fn foo() { | ||
let mut x = 22; | ||
let wrapper = Wrap { w: &mut x }; | ||
x += 1; //[ast]~ ERROR cannot assign to `x` because it is borrowed [E0506] | ||
//[mir]~^ ERROR cannot assign to `x` because it is borrowed (Ast) [E0506] | ||
//[mir]~^^ ERROR cannot assign to `x` because it is borrowed (Mir) [E0506] | ||
//[mir]~^^^ ERROR cannot use `x` because it was mutably borrowed (Mir) [E0503] | ||
*wrapper.w += 1; | ||
} | ||
|
||
fn main() { } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: you no longer need
match **ak
, thanks to#[feature(match_default_bindings)]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the latest commit I still need to because this is a
&Box
rather than&&
.