Skip to content
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

support ascription for patterns in NLL #53873

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fa1f564
document the purpose of `ScopeInstantiator`
nikomatsakis Aug 28, 2018
09f4172
remove extra lifetime bound
nikomatsakis Aug 28, 2018
d6a98db
factor out `lookup_bound_region`
nikomatsakis Aug 29, 2018
5c5741b
add some comments
nikomatsakis Aug 29, 2018
3f60043
infer/mod.rs: rustfmt
nikomatsakis Aug 29, 2018
5c016f4
add ability to create region vars with explicit universe
nikomatsakis Aug 29, 2018
39b9281
add a `first_free_index` parameter
nikomatsakis Aug 31, 2018
5f43b09
instantiate traversed binders rather than saving the scopes
nikomatsakis Aug 31, 2018
aa52e12
add generalization
nikomatsakis Aug 31, 2018
07e21b1
add a test for variables used twice
nikomatsakis Aug 31, 2018
34575e6
now that we can handle subtyping, fix higher-ranked equality
nikomatsakis Aug 31, 2018
4b5f19a
remove the old `UserAssertTy` support
nikomatsakis Aug 31, 2018
50754d0
add a `AscribeUserType` pattern, largely ignored
nikomatsakis Aug 31, 2018
22f9bcc
matches/mod.rs: rustfmt
nikomatsakis Aug 31, 2018
dd3cc96
add the `AscribeUserType` statement kind
nikomatsakis Aug 31, 2018
7e1b978
insert `AscribeUserType` for ascriptions
nikomatsakis Aug 31, 2018
05a6e1e
pacify the mercilous tidy
nikomatsakis Sep 3, 2018
f0e3a09
fixup: rename `UserAssertTy` to `AscribeUserType`
nikomatsakis Sep 4, 2018
fced2b1
fix SCCs containing mixture of universes
nikomatsakis Sep 5, 2018
9c5e794
WIP remove incorrect nll.stderr reference files
nikomatsakis Sep 5, 2018
16f4e8a
generalize `AscribeUserType` to handle sub or super type
nikomatsakis Sep 5, 2018
2f6628e
optimize `let x: T = ..` to avoid a temporary
nikomatsakis Sep 5, 2018
a871053
expand the patterns test with a bunch more scenarios
nikomatsakis Sep 10, 2018
e87bf30
propagate user-ascribes types down onto resulting bindings
nikomatsakis Sep 10, 2018
2b6f966
add link to https://github.com/rust-lang/rust/issues/54105
nikomatsakis Sep 10, 2018
df37678
add FIXME related to `ref x` bindings
nikomatsakis Sep 10, 2018
f95f23f
fix incremental test
nikomatsakis Sep 10, 2018
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
1 change: 1 addition & 0 deletions src/librustc/ich/impls_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl_stable_hash_for!(enum mir::LocalKind { Var, Temp, Arg, ReturnPointer });
impl_stable_hash_for!(struct mir::LocalDecl<'tcx> {
mutability,
ty,
user_ty,
name,
source_info,
visibility_scope,
Expand Down
9 changes: 9 additions & 0 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,12 @@ pub struct LocalDecl<'tcx> {
/// Type of this local.
pub ty: Ty<'tcx>,

/// If the user manually ascribed a type to this variable,
/// e.g. via `let x: T`, then we carry that type here. The MIR
/// borrow checker needs this information since it can affect
/// region inference.
pub user_ty: Option<CanonicalTy<'tcx>>,

/// Name of the local, used in debuginfo and pretty-printing.
///
/// Note that function arguments can also have this set to `Some(_)`
Expand Down Expand Up @@ -802,6 +808,7 @@ impl<'tcx> LocalDecl<'tcx> {
LocalDecl {
mutability,
ty,
user_ty: None,
name: None,
source_info: SourceInfo {
span,
Expand All @@ -821,6 +828,7 @@ impl<'tcx> LocalDecl<'tcx> {
LocalDecl {
mutability: Mutability::Mut,
ty: return_ty,
user_ty: None,
source_info: SourceInfo {
span,
scope: OUTERMOST_SOURCE_SCOPE,
Expand Down Expand Up @@ -2613,6 +2621,7 @@ BraceStructTypeFoldableImpl! {
is_user_variable,
internal,
ty,
user_ty,
name,
source_info,
visibility_scope,
Expand Down
4 changes: 4 additions & 0 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ macro_rules! make_mir_visitor {
let LocalDecl {
mutability: _,
ref $($mutability)* ty,
ref $($mutability)* user_ty,
name: _,
ref $($mutability)* source_info,
ref $($mutability)* visibility_scope,
Expand All @@ -732,6 +733,9 @@ macro_rules! make_mir_visitor {
local,
source_info: *source_info,
});
if let Some(user_ty) = user_ty {
self.visit_canonical_ty(user_ty);
}
self.visit_source_info(source_info);
self.visit_source_scope(visibility_scope);
}
Expand Down
19 changes: 19 additions & 0 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,25 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
self.super_local_decl(local, local_decl);
self.sanitize_type(local_decl, local_decl.ty);

if let Some(user_ty) = local_decl.user_ty {
if let Err(terr) = self.cx.relate_type_and_user_type(
local_decl.ty,
ty::Variance::Invariant,
user_ty,
Locations::All,
) {
span_mirbug!(
self,
local,
"bad user type on variable {:?}: {:?} != {:?} ({:?})",
local,
local_decl.ty,
local_decl.user_ty,
terr,
);
}
}
}

fn visit_mir(&mut self, mir: &Mir<'tcx>) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
None, remainder_span, lint_level, slice::from_ref(&pattern),
ArmHasGuard(false), None);

this.visit_bindings(&pattern, &mut |this, _, _, _, node, span, _| {
this.visit_bindings(&pattern, None, &mut |this, _, _, _, node, span, _, _| {
this.storage_live_binding(block, node, span, OutsideGuard);
this.schedule_drop_for_binding(node, span, OutsideGuard);
})
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let ptr_temp = this.local_decls.push(LocalDecl {
mutability: Mutability::Mut,
ty: ptr_ty,
user_ty: None,
name: None,
source_info,
visibility_scope: source_info.scope,
Expand Down
55 changes: 42 additions & 13 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let num_patterns = patterns.len();
self.visit_bindings(
&patterns[0],
&mut |this, mutability, name, mode, var, span, ty| {
None,
&mut |this, mutability, name, mode, var, span, ty, user_ty| {
if visibility_scope.is_none() {
visibility_scope =
Some(this.new_source_scope(scope_span, LintLevel::Inherited, None));
Expand All @@ -421,6 +422,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
num_patterns,
var,
ty,
user_ty,
has_guard,
opt_match_place.map(|(x, y)| (x.cloned(), y)),
patterns[0].span,
Expand Down Expand Up @@ -470,10 +472,21 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
);
}

pub fn visit_bindings<F>(&mut self, pattern: &Pattern<'tcx>, f: &mut F)
where
F: FnMut(&mut Self, Mutability, Name, BindingMode, NodeId, Span, Ty<'tcx>),
{
pub fn visit_bindings(
&mut self,
pattern: &Pattern<'tcx>,
pattern_user_ty: Option<CanonicalTy<'tcx>>,
f: &mut impl FnMut(
&mut Self,
Mutability,
Name,
BindingMode,
NodeId,
Span,
Ty<'tcx>,
Option<CanonicalTy<'tcx>>,
),
) {
match *pattern.kind {
PatternKind::Binding {
mutability,
Expand All @@ -484,9 +497,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
ref subpattern,
..
} => {
f(self, mutability, name, mode, var, pattern.span, ty);
f(self, mutability, name, mode, var, pattern.span, ty, pattern_user_ty);
if let Some(subpattern) = subpattern.as_ref() {
self.visit_bindings(subpattern, f);
self.visit_bindings(subpattern, pattern_user_ty, f);
}
}
PatternKind::Array {
Expand All @@ -499,21 +512,34 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
ref slice,
ref suffix,
} => {
// FIXME(#47184): extract or handle `pattern_user_ty` somehow
for subpattern in prefix.iter().chain(slice).chain(suffix) {
self.visit_bindings(subpattern, f);
self.visit_bindings(subpattern, None, f);
}
}
PatternKind::Constant { .. } | PatternKind::Range { .. } | PatternKind::Wild => {}
PatternKind::AscribeUserType { ref subpattern, .. }
| PatternKind::Deref { ref subpattern } => {
self.visit_bindings(subpattern, f);
PatternKind::Deref { ref subpattern } => {
// FIXME(#47184): extract or handle `pattern_user_ty` somehow
self.visit_bindings(subpattern, None, f);
}
PatternKind::AscribeUserType { ref subpattern, user_ty } => {
// This corresponds to something like
//
// ```
// let (p1: T1): T2 = ...;
// ```
//
// Not presently possible, though maybe someday.
assert!(pattern_user_ty.is_none());
self.visit_bindings(subpattern, Some(user_ty), f)
}
PatternKind::Leaf { ref subpatterns }
| PatternKind::Variant {
ref subpatterns, ..
} => {
// FIXME(#47184): extract or handle `pattern_user_ty` somehow
for subpattern in subpatterns {
self.visit_bindings(&subpattern.pattern, f);
self.visit_bindings(&subpattern.pattern, None, f);
}
}
}
Expand Down Expand Up @@ -1375,6 +1401,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
num_patterns: usize,
var_id: NodeId,
var_ty: Ty<'tcx>,
user_var_ty: Option<CanonicalTy<'tcx>>,
has_guard: ArmHasGuard,
opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
pat_span: Span,
Expand All @@ -1392,7 +1419,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
};
let local = LocalDecl::<'tcx> {
mutability,
ty: var_ty.clone(),
ty: var_ty,
user_ty: user_var_ty,
name: Some(name),
source_info,
visibility_scope,
Expand Down Expand Up @@ -1424,6 +1452,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
// See previous comment.
mutability: Mutability::Not,
ty: tcx.mk_imm_ref(tcx.types.re_empty, var_ty),
user_ty: None,
name: Some(name),
source_info,
visibility_scope,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
self.local_decls.push(LocalDecl {
mutability: Mutability::Mut,
ty,
user_ty: None,
source_info,
visibility_scope: source_info.scope,
name,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
#![feature(if_while_or_patterns)]
#![feature(try_from)]
#![feature(reverse_bits)]
#![feature(underscore_imports)]

#![recursion_limit="256"]

Expand Down
4 changes: 3 additions & 1 deletion src/librustc_mir/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ enum CallKind {
fn temp_decl(mutability: Mutability, ty: Ty, span: Span) -> LocalDecl {
let source_info = SourceInfo { scope: OUTERMOST_SOURCE_SCOPE, span };
LocalDecl {
mutability, ty, name: None,
mutability, ty,
user_ty: None,
name: None,
source_info,
visibility_scope: source_info.scope,
internal: false,
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ fn replace_result_variable<'tcx>(
let new_ret = LocalDecl {
mutability: Mutability::Mut,
ty: ret_ty,
user_ty: None,
name: None,
source_info,
visibility_scope: source_info.scope,
Expand Down Expand Up @@ -656,6 +657,7 @@ fn create_generator_drop_shim<'a, 'tcx>(
mir.local_decls[RETURN_PLACE] = LocalDecl {
mutability: Mutability::Mut,
ty: tcx.mk_nil(),
user_ty: None,
name: None,
source_info,
visibility_scope: source_info.scope,
Expand All @@ -672,6 +674,7 @@ fn create_generator_drop_shim<'a, 'tcx>(
ty: gen_ty,
mutbl: hir::Mutability::MutMutable,
}),
user_ty: None,
name: None,
source_info,
visibility_scope: source_info.scope,
Expand Down
9 changes: 7 additions & 2 deletions src/librustc_mir/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rustc::ty::item_path;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::indexed_vec::Idx;
use std::fmt::Display;
use std::fmt::Write as _;
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -493,14 +494,18 @@ fn write_scope_tree(
};

let indent = indent + INDENT.len();
let indented_var = format!(
"{0:1$}let {2}{3:?}: {4:?};",
let mut indented_var = format!(
"{0:1$}let {2}{3:?}: {4:?}",
INDENT,
indent,
mut_str,
local,
var.ty
);
if let Some(user_ty) = var.user_ty {
write!(indented_var, " as {:?}", user_ty).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

hmm. I'd have to think about this syntax and whether it makes sense. (But I won't let that block landing this PR; the printed MIR output format is unstable, afterall...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah it...probably doesn't make sense =)

}
indented_var.push_str(";");
writeln!(
w,
"{0:1$} // \"{2}\" in {3}",
Expand Down
39 changes: 34 additions & 5 deletions src/test/ui/nll/user-annotations/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,41 @@
#![feature(nll)]

fn variable_no_initializer() {
// FIXME: It is unclear to me whether this should be an error or not.

let x = 22;
let y: &'static u32;
y = &x; //~ ERROR
Copy link
Member

Choose a reason for hiding this comment

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

🎉

}

fn tuple_no_initializer() {
// FIXME(#47187): We are not propagating ascribed type through tuples.

let x = 22;
let (y, z): (&'static u32, &'static u32);
y = &x;
}

fn ref_with_ascribed_static_type() -> u32 {
// Check the behavior in some wacky cases.
let x = 22;
let y = &x; //~ ERROR
let ref z: &'static u32 = y; //~ ERROR
**z
}

fn ref_with_ascribed_any_type() -> u32 {
let x = 22;
let y = &x;
let ref z: &u32 = y;
**z
}

struct Single<T> { value: T }

fn struct_no_initializer() {
// FIXME(#47187): We are not propagating ascribed type through patterns.

let x = 22;
let Single { value: y }: Single<&'static u32>;
y = &x;
}

Expand Down Expand Up @@ -39,8 +70,6 @@ fn pair_variable_with_initializer() {
let (y, _): (&'static u32, u32) = (&x, 44); //~ ERROR
}

struct Single<T> { value: T }

fn struct_single_field_variable_with_initializer() {
let x = 22;
let Single { value: y }: Single<&'static u32> = Single { value: &x }; //~ ERROR
Expand Down Expand Up @@ -73,7 +102,7 @@ fn static_to_a_to_static_through_variable<'a>(x: &'a u32) -> &'static u32 {
}

fn static_to_a_to_static_through_tuple<'a>(x: &'a u32) -> &'static u32 {
// FIXME: The fact that this type-checks is perhaps surprising.
// FIXME(#47187): The fact that this type-checks is perhaps surprising.
// What happens is that the right-hand side is constrained to have
// type `&'a u32`, which is possible, because it has type
// `&'static u32`. The variable `y` is then forced to have type
Expand Down
Loading