Skip to content

Commit 6d7b35b

Browse files
committed
Address review comments + fix rebase
1 parent e783a0a commit 6d7b35b

File tree

7 files changed

+51
-167
lines changed

7 files changed

+51
-167
lines changed

src/librustc/middle/mem_categorization.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -1105,17 +1105,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
11051105

11061106
(*op)(self, cmt.clone(), pat);
11071107

1108-
// This function can be used during region checking when not all paths are fully
1109-
// resolved. Partially resolved paths in patterns can only legally refer to
1110-
// associated constants which don't require categorization.
1111-
let opt_def = if let Some(path_res) = self.tcx().def_map.borrow().get(&pat.id) {
1112-
if path_res.depth != 0 || path_res.base_def == Def::Err {
1113-
return Err(());
1114-
}
1115-
Some(path_res.full_def())
1116-
} else {
1117-
None
1118-
};
1108+
let opt_def = self.tcx().expect_def_or_none(pat.id);
1109+
if opt_def == Some(Def::Err) {
1110+
return Err(());
1111+
}
11191112

11201113
// Note: This goes up here (rather than within the PatKind::TupleStruct arm
11211114
// alone) because struct patterns can refer to struct types or

src/librustc_mir/hair/cx/pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'patcx, 'cx, 'gcx, 'tcx> PatCx<'patcx, 'cx, 'gcx, 'tcx> {
162162
}
163163

164164
PatKind::Binding(bm, ref ident, ref sub) => {
165-
let id = self.cx.tcx.def_map.borrow()[&pat.id].full_def().var_id();
165+
let id = self.cx.tcx.expect_def(pat.id).var_id();
166166
let var_ty = self.cx.tcx.node_id_to_type(pat.id);
167167
let region = match var_ty.sty {
168168
ty::TyRef(&r, _) => Some(r),

src/librustc_passes/consts.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
584584
}
585585
}
586586
hir::ExprStruct(..) => {
587-
if v.tcx.expect_def(e.id).def_id() == v.tcx.lang_items.unsafe_cell_type().unwrap() {
587+
// unsafe_cell_type doesn't necessarily exist with no_core
588+
if Some(v.tcx.expect_def(e.id).def_id()) == v.tcx.lang_items.unsafe_cell_type() {
588589
v.add_qualif(ConstQualif::MUTABLE_MEM);
589590
}
590591
}

src/librustc_resolve/diagnostics.rs

+16-126
Original file line numberDiff line numberDiff line change
@@ -677,100 +677,6 @@ fn foo<T>(x: T) {} // ok!
677677
```
678678
"##,
679679

680-
E0413: r##"
681-
A declaration shadows an enum variant or unit-like struct in scope. Example of
682-
erroneous code:
683-
684-
```compile_fail
685-
struct Foo;
686-
687-
let Foo = 12i32; // error: declaration of `Foo` shadows an enum variant or
688-
// unit-like struct in scope
689-
```
690-
691-
To fix this error, rename the variable such that it doesn't shadow any enum
692-
variable or structure in scope. Example:
693-
694-
```
695-
struct Foo;
696-
697-
let foo = 12i32; // ok!
698-
```
699-
700-
Or:
701-
702-
```
703-
struct FooStruct;
704-
705-
let Foo = 12i32; // ok!
706-
```
707-
708-
The goal here is to avoid a conflict of names.
709-
"##,
710-
711-
E0414: r##"
712-
A variable binding in an irrefutable pattern is shadowing the name of a
713-
constant. Example of erroneous code:
714-
715-
```compile_fail
716-
const FOO: u8 = 7;
717-
718-
let FOO = 5; // error: variable bindings cannot shadow constants
719-
720-
// or
721-
722-
fn bar(FOO: u8) { // error: variable bindings cannot shadow constants
723-
724-
}
725-
726-
// or
727-
728-
for FOO in bar {
729-
730-
}
731-
```
732-
733-
Introducing a new variable in Rust is done through a pattern. Thus you can have
734-
`let` bindings like `let (a, b) = ...`. However, patterns also allow constants
735-
in them, e.g. if you want to match over a constant:
736-
737-
```ignore
738-
const FOO: u8 = 1;
739-
740-
match (x,y) {
741-
(3, 4) => { .. }, // it is (3,4)
742-
(FOO, 1) => { .. }, // it is (1,1)
743-
(foo, 1) => { .. }, // it is (anything, 1)
744-
// call the value in the first slot "foo"
745-
_ => { .. } // it is anything
746-
}
747-
```
748-
749-
Here, the second arm matches the value of `x` against the constant `FOO`,
750-
whereas the third arm will accept any value of `x` and call it `foo`.
751-
752-
This works for `match`, however in cases where an irrefutable pattern is
753-
required, constants can't be used. An irrefutable pattern is one which always
754-
matches, whose purpose is only to bind variable names to values. These are
755-
required by let, for, and function argument patterns.
756-
757-
Refutable patterns in such a situation do not make sense, for example:
758-
759-
```ignore
760-
let Some(x) = foo; // what if foo is None, instead?
761-
762-
let (1, x) = foo; // what if foo.0 is not 1?
763-
764-
let (SOME_CONST, x) = foo; // what if foo.0 is not SOME_CONST?
765-
766-
let SOME_CONST = foo; // what if foo is not SOME_CONST?
767-
```
768-
769-
Thus, an irrefutable variable binding can't contain a constant.
770-
771-
To fix this error, just give the marked variable a different name.
772-
"##,
773-
774680
E0415: r##"
775681
More than one function parameter have the same name. Example of erroneous code:
776682
@@ -814,34 +720,6 @@ match (A, B, C) {
814720
```
815721
"##,
816722

817-
E0417: r##"
818-
A static variable was referenced in a pattern. Example of erroneous code:
819-
820-
```compile_fail
821-
static FOO : i32 = 0;
822-
823-
match 0 {
824-
FOO => {} // error: static variables cannot be referenced in a
825-
// pattern, use a `const` instead
826-
_ => {}
827-
}
828-
```
829-
830-
The compiler needs to know the value of the pattern at compile time;
831-
compile-time patterns can defined via const or enum items. Please verify
832-
that the identifier is spelled correctly, and if so, use a const instead
833-
of static to define it. Example:
834-
835-
```
836-
const FOO : i32 = 0;
837-
838-
match 0 {
839-
FOO => {} // ok!
840-
_ => {}
841-
}
842-
```
843-
"##,
844-
845723
E0422: r##"
846724
You are trying to use an identifier that is either undefined or not a struct.
847725
For instance:
@@ -1221,11 +1099,23 @@ impl Foo for i32 {}
12211099
}
12221100

12231101
register_diagnostics! {
1102+
// E0153, unused error code
1103+
// E0157, unused error code
12241104
E0254, // import conflicts with imported crate in this module
1105+
// E0257,
1106+
// E0258,
12251107
E0402, // cannot use an outer type parameter in this context
12261108
E0406, // undeclared associated type
1227-
E0418, // X bindings cannot shadow Ys
1228-
E0419, // unresolved pattern path kind `name`
1229-
E0420, // expected pattern path kind, found another pattern path kind
1230-
E0427, // cannot use `ref` binding mode with ...
1109+
// E0410, merged into 408
1110+
// E0413, merged into 530
1111+
// E0414, merged into 530
1112+
// E0417, merged into 532
1113+
// E0418, merged into 532
1114+
// E0419, merged into 531
1115+
// E0420, merged into 532
1116+
// E0421, merged into 531
1117+
E0530, // X bindings cannot shadow Ys
1118+
E0531, // unresolved pattern path kind `name`
1119+
E0532, // expected pattern path kind, found another pattern path kind
1120+
// E0427, merged into 530
12311121
}

src/librustc_resolve/lib.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ enum ResolutionError<'a> {
155155
CannotCaptureDynamicEnvironmentInFnItem,
156156
/// error E0435: attempt to use a non-constant value in a constant
157157
AttemptToUseNonConstantValueInConstant,
158-
/// error E0418: X bindings cannot shadow Ys
158+
/// error E0530: X bindings cannot shadow Ys
159159
BindingShadowsSomethingUnacceptable(&'a str, &'a str, Name),
160-
/// error E0419: unresolved pattern path kind `name`
160+
/// error E0531: unresolved pattern path kind `name`
161161
PatPathUnresolved(&'a str, &'a Path),
162-
/// error E0420: expected pattern path kind, found another pattern path kind
162+
/// error E0532: expected pattern path kind, found another pattern path kind
163163
PatPathUnexpected(&'a str, &'a str, &'a Path),
164164
}
165165

@@ -426,11 +426,10 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
426426
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, shadows_what, name) => {
427427
let mut err = struct_span_err!(resolver.session,
428428
span,
429-
E0418,
429+
E0530,
430430
"{}s cannot shadow {}s", what_binding, shadows_what);
431431
err.span_label(span, &format!("cannot be named the same as a {}", shadows_what));
432-
if let Some(binding) = resolver.current_module
433-
.resolve_name_in_lexical_scope(name, ValueNS) {
432+
if let Success(binding) = resolver.current_module.resolve_name(name, ValueNS, true) {
434433
let participle = if binding.is_import() { "imported" } else { "defined" };
435434
err.span_label(binding.span, &format!("a {} `{}` is {} here",
436435
shadows_what, name, participle));
@@ -440,15 +439,15 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
440439
ResolutionError::PatPathUnresolved(expected_what, path) => {
441440
struct_span_err!(resolver.session,
442441
span,
443-
E0419,
442+
E0531,
444443
"unresolved {} `{}`",
445444
expected_what,
446445
path.segments.last().unwrap().identifier)
447446
}
448447
ResolutionError::PatPathUnexpected(expected_what, found_what, path) => {
449448
struct_span_err!(resolver.session,
450449
span,
451-
E0420,
450+
E0532,
452451
"expected {}, found {} `{}`",
453452
expected_what,
454453
found_what,
@@ -2201,15 +2200,15 @@ impl<'a> Resolver<'a> {
22012200
pat_id: NodeId,
22022201
outer_pat_id: NodeId,
22032202
pat_src: PatternSource,
2204-
bindings_list: &mut HashMap<Name, NodeId>)
2203+
bindings: &mut HashMap<Name, NodeId>)
22052204
-> PathResolution {
22062205
// Add the binding to the local ribs, if it
2207-
// doesn't already exist in the bindings list. (We
2208-
// must not add it if it's in the bindings list
2206+
// doesn't already exist in the bindings map. (We
2207+
// must not add it if it's in the bindings map
22092208
// because that breaks the assumptions later
22102209
// passes make about or-patterns.)
22112210
let renamed = mtwt::resolve(ident.node);
2212-
let def = match bindings_list.get(&renamed).cloned() {
2211+
let def = match bindings.get(&renamed).cloned() {
22132212
Some(id) if id == outer_pat_id => {
22142213
// `Variant(a, a)`, error
22152214
resolve_error(
@@ -2231,8 +2230,9 @@ impl<'a> Resolver<'a> {
22312230
Def::Err
22322231
}
22332232
Some(..) if pat_src == PatternSource::Match => {
2234-
// `Varian1(a) | Varian2(a)`, ok
2235-
Def::Local(self.definitions.local_def_id(pat_id), pat_id)
2233+
// `Variant1(a) | Variant2(a)`, ok
2234+
// Reuse definition from the first `a`.
2235+
self.value_ribs.last_mut().unwrap().bindings[&renamed]
22362236
}
22372237
Some(..) => {
22382238
span_bug!(ident.span, "two bindings with the same name from \
@@ -2244,7 +2244,7 @@ impl<'a> Resolver<'a> {
22442244
// define `Invalid` bindings as `Def::Local`, just don't add them to the lists.
22452245
let def = Def::Local(self.definitions.local_def_id(pat_id), pat_id);
22462246
if ident.node.name != keywords::Invalid.name() {
2247-
bindings_list.insert(renamed, outer_pat_id);
2247+
bindings.insert(renamed, outer_pat_id);
22482248
self.value_ribs.last_mut().unwrap().bindings.insert(renamed, def);
22492249
}
22502250
def
@@ -2255,12 +2255,12 @@ impl<'a> Resolver<'a> {
22552255
}
22562256

22572257
fn resolve_pattern_path<ExpectedFn>(&mut self,
2258-
pat_id: NodeId,
2259-
qself: Option<&QSelf>,
2260-
path: &Path,
2261-
namespace: Namespace,
2262-
expected_fn: ExpectedFn,
2263-
expected_what: &'static str)
2258+
pat_id: NodeId,
2259+
qself: Option<&QSelf>,
2260+
path: &Path,
2261+
namespace: Namespace,
2262+
expected_fn: ExpectedFn,
2263+
expected_what: &str)
22642264
where ExpectedFn: FnOnce(Def) -> bool
22652265
{
22662266
let resolution = if let Some(resolution) = self.resolve_possibly_assoc_item(pat_id,
@@ -2307,8 +2307,8 @@ impl<'a> Resolver<'a> {
23072307
pat_src: PatternSource,
23082308
// Maps idents to the node ID for the
23092309
// outermost pattern that binds them.
2310-
bindings_list: &mut HashMap<Name, NodeId>) {
2311-
// Visit all direct subpatterns of this pattern with the same PatternBindingMode.
2310+
bindings: &mut HashMap<Name, NodeId>) {
2311+
// Visit all direct subpatterns of this pattern.
23122312
let outer_pat_id = pat.id;
23132313
pat.walk(&mut |pat| {
23142314
match pat.node {
@@ -2340,7 +2340,7 @@ impl<'a> Resolver<'a> {
23402340
// These entities are explicitly allowed
23412341
// to be shadowed by fresh bindings.
23422342
self.fresh_binding(ident, pat.id, outer_pat_id,
2343-
pat_src, bindings_list)
2343+
pat_src, bindings)
23442344
}
23452345
def => {
23462346
span_bug!(ident.span, "unexpected definition for an \
@@ -2349,7 +2349,7 @@ impl<'a> Resolver<'a> {
23492349
}
23502350
} else {
23512351
// Fall back to a fresh binding.
2352-
self.fresh_binding(ident, pat.id, outer_pat_id, pat_src, bindings_list)
2352+
self.fresh_binding(ident, pat.id, outer_pat_id, pat_src, bindings)
23532353
};
23542354

23552355
self.record_def(pat.id, resolution);

src/librustc_typeck/check/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
151151
// is good enough.
152152
self.demand_suptype(pat.span, expected, const_ty);
153153
}
154-
PatKind::Binding(bm, ref path, ref sub) => {
154+
PatKind::Binding(bm, _, ref sub) => {
155155
let typ = self.local_ty(pat.span, pat.id);
156156
match bm {
157157
hir::BindByRef(mutbl) => {
@@ -180,7 +180,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
180180

181181
// if there are multiple arms, make sure they all agree on
182182
// what the type of the binding `x` ought to be
183-
match tcx.def_map.borrow()[&pat.id].full_def() {
183+
match tcx.expect_def(pat.id) {
184184
Def::Err => {}
185185
Def::Local(_, var_id) => {
186186
if var_id != pat.id {

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ use fmt_macros::{Parser, Piece, Position};
8686
use middle::cstore::LOCAL_CRATE;
8787
use hir::def::{self, Def};
8888
use hir::def_id::DefId;
89+
use hir::pat_util;
8990
use rustc::infer::{self, InferCtxt, InferOk, TypeOrigin, TypeTrace, type_variable};
90-
use hir::pat_util::{self};
9191
use rustc::ty::subst::{self, Subst, Substs, VecPerParamSpace, ParamSpace};
9292
use rustc::traits::{self, ProjectionMode};
9393
use rustc::ty::{GenericPredicates, TypeScheme};

0 commit comments

Comments
 (0)