Skip to content

Rollup of 3 pull requests #31214

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

Merged
merged 12 commits into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
80 changes: 50 additions & 30 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ enum FallbackSuggestion {
}

#[derive(Copy, Clone)]
enum TypeParameters<'a> {
enum TypeParameters<'tcx, 'a> {
NoTypeParameters,
HasTypeParameters(// Type parameters.
&'a Generics,
Expand All @@ -733,13 +733,13 @@ enum TypeParameters<'a> {
ParamSpace,

// The kind of the rib used for type parameters.
RibKind),
RibKind<'tcx>),
}

// The rib kind controls the translation of local
// definitions (`Def::Local`) to upvars (`Def::Upvar`).
#[derive(Copy, Clone, Debug)]
enum RibKind {
enum RibKind<'a> {
// No translation needs to be applied.
NormalRibKind,

Expand All @@ -758,6 +758,9 @@ enum RibKind {

// We're in a constant item. Can't refer to dynamic stuff.
ConstantItemRibKind,

// We passed through an anonymous module.
AnonymousModuleRibKind(Module<'a>),
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -799,13 +802,13 @@ enum BareIdentifierPatternResolution {

/// One local scope.
#[derive(Debug)]
struct Rib {
struct Rib<'a> {
bindings: HashMap<Name, DefLike>,
kind: RibKind,
kind: RibKind<'a>,
}

impl Rib {
fn new(kind: RibKind) -> Rib {
impl<'a> Rib<'a> {
fn new(kind: RibKind<'a>) -> Rib<'a> {
Rib {
bindings: HashMap::new(),
kind: kind,
Expand Down Expand Up @@ -1180,13 +1183,13 @@ pub struct Resolver<'a, 'tcx: 'a> {

// The current set of local scopes, for values.
// FIXME #4948: Reuse ribs to avoid allocation.
value_ribs: Vec<Rib>,
value_ribs: Vec<Rib<'a>>,

// The current set of local scopes, for types.
type_ribs: Vec<Rib>,
type_ribs: Vec<Rib<'a>>,

// The current set of local scopes, for labels.
label_ribs: Vec<Rib>,
label_ribs: Vec<Rib<'a>>,

// The trait that the current context can refer to.
current_trait_ref: Option<(DefId, TraitRef)>,
Expand Down Expand Up @@ -1304,6 +1307,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.arenas.modules.alloc(ModuleS::new(parent_link, def, external, is_public))
}

fn get_ribs<'b>(&'b mut self, ns: Namespace) -> &'b mut Vec<Rib<'a>> {
match ns { ValueNS => &mut self.value_ribs, TypeNS => &mut self.type_ribs }
}

#[inline]
fn record_import_use(&mut self, import_id: NodeId, name: Name) {
if !self.make_glob_map {
Expand Down Expand Up @@ -2122,7 +2129,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
}

fn with_type_parameter_rib<F>(&mut self, type_parameters: TypeParameters, f: F)
fn with_type_parameter_rib<'b, F>(&'b mut self, type_parameters: TypeParameters<'a, 'b>, f: F)
where F: FnOnce(&mut Resolver)
{
match type_parameters {
Expand Down Expand Up @@ -2191,7 +2198,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
}

fn resolve_function(&mut self, rib_kind: RibKind, declaration: &FnDecl, block: &Block) {
fn resolve_function(&mut self, rib_kind: RibKind<'a>, declaration: &FnDecl, block: &Block) {
// Create a value rib for the function.
self.value_ribs.push(Rib::new(rib_kind));

Expand Down Expand Up @@ -2494,18 +2501,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {

fn resolve_block(&mut self, block: &Block) {
debug!("(resolving block) entering block");
self.value_ribs.push(Rib::new(NormalRibKind));

// Move down in the graph, if there's an anonymous module rooted here.
let orig_module = self.current_module;
match orig_module.anonymous_children.borrow().get(&block.id) {
None => {
// Nothing to do.
}
Some(anonymous_module) => {
debug!("(resolving block) found anonymous module, moving down");
self.current_module = anonymous_module;
}
let anonymous_module =
orig_module.anonymous_children.borrow().get(&block.id).map(|module| *module);

if let Some(anonymous_module) = anonymous_module {
debug!("(resolving block) found anonymous module, moving down");
self.value_ribs.push(Rib::new(AnonymousModuleRibKind(anonymous_module)));
self.type_ribs.push(Rib::new(AnonymousModuleRibKind(anonymous_module)));
self.current_module = anonymous_module;
} else {
self.value_ribs.push(Rib::new(NormalRibKind));
}

// Check for imports appearing after non-item statements.
Expand Down Expand Up @@ -2538,6 +2545,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if !self.resolved {
self.current_module = orig_module;
self.value_ribs.pop();
if let Some(_) = anonymous_module {
self.type_ribs.pop();
}
}
debug!("(resolving block) leaving block");
}
Expand Down Expand Up @@ -3076,7 +3086,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Def::Local(_, node_id) => {
for rib in ribs {
match rib.kind {
NormalRibKind => {
NormalRibKind | AnonymousModuleRibKind(..) => {
// Nothing to do. Continue.
}
ClosureRibKind(function_id) => {
Expand Down Expand Up @@ -3124,7 +3134,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Def::TyParam(..) | Def::SelfTy(..) => {
for rib in ribs {
match rib.kind {
NormalRibKind | MethodRibKind | ClosureRibKind(..) => {
NormalRibKind | MethodRibKind | ClosureRibKind(..) |
AnonymousModuleRibKind(..) => {
// Nothing to do. Continue.
}
ItemRibKind => {
Expand Down Expand Up @@ -3275,13 +3286,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
namespace: Namespace)
-> Option<LocalDef> {
// Check the local set of ribs.
let (name, ribs) = match namespace {
ValueNS => (ident.name, &self.value_ribs),
TypeNS => (ident.unhygienic_name, &self.type_ribs),
};
let name = match namespace { ValueNS => ident.name, TypeNS => ident.unhygienic_name };

for (i, rib) in ribs.iter().enumerate().rev() {
if let Some(def_like) = rib.bindings.get(&name).cloned() {
for i in (0 .. self.get_ribs(namespace).len()).rev() {
if let Some(def_like) = self.get_ribs(namespace)[i].bindings.get(&name).cloned() {
match def_like {
DlDef(def) => {
debug!("(resolving path in local ribs) resolved `{}` to {:?} at {}",
Expand All @@ -3301,6 +3309,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
}
}

if let AnonymousModuleRibKind(module) = self.get_ribs(namespace)[i].kind {
if let Success((target, _)) = self.resolve_name_in_module(module,
ident.unhygienic_name,
namespace,
PathSearch,
true) {
if let Some(def) = target.binding.def() {
return Some(LocalDef::from_def(def));
}
}
}
}

None
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<W: io::Write> io::Write for Maybe<W> {
impl<R: io::Read> io::Read for Maybe<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match *self {
Maybe::Real(ref mut r) => handle_ebadf(r.read(buf), buf.len()),
Maybe::Real(ref mut r) => handle_ebadf(r.read(buf), 0),
Maybe::Fake => Ok(0)
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/libstd/sys/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,15 @@ fn make_dirp(d: Option<&OsString>) -> (*const u16, Vec<u16>) {
impl Stdio {
fn to_handle(&self, stdio_id: c::DWORD) -> io::Result<Handle> {
match *self {
// If no stdio handle is available, then inherit means that it
// should still be unavailable so propagate the
// INVALID_HANDLE_VALUE.
Stdio::Inherit => {
stdio::get(stdio_id).and_then(|io| {
io.handle().duplicate(0, true, c::DUPLICATE_SAME_ACCESS)
})
match stdio::get(stdio_id) {
Ok(io) => io.handle().duplicate(0, true,
c::DUPLICATE_SAME_ACCESS),
Err(..) => Ok(Handle::new(c::INVALID_HANDLE_VALUE)),
}
}
Stdio::Raw(handle) => {
RawHandle::new(handle).duplicate(0, true, c::DUPLICATE_SAME_ACCESS)
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use self::Destination::*;

use codemap::{self, COMMAND_LINE_SP, COMMAND_LINE_EXPN, Pos, Span};
use codemap::{self, COMMAND_LINE_SP, COMMAND_LINE_EXPN, DUMMY_SP, Pos, Span};
use diagnostics;

use errors::{Level, RenderSpan, DiagnosticBuilder};
Expand Down Expand Up @@ -109,8 +109,8 @@ impl Emitter for EmitterWriter {
lvl: Level) {
let error = match sp {
Some(COMMAND_LINE_SP) => self.emit_(FileLine(COMMAND_LINE_SP), msg, code, lvl),
Some(DUMMY_SP) | None => print_diagnostic(&mut self.dst, "", lvl, msg, code),
Some(sp) => self.emit_(FullSpan(sp), msg, code, lvl),
None => print_diagnostic(&mut self.dst, "", lvl, msg, code),
};

if let Err(e) = error {
Expand Down
6 changes: 6 additions & 0 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,12 @@ impl<'a> Parser<'a> {
ex = ExprBreak(None);
}
hi = self.last_span.hi;
} else if self.token.is_keyword(keywords::Let) {
// Catch this syntax error here, instead of in `check_strict_keywords`, so
// that we can explicitly mention that let is not to be used as an expression
let mut db = self.fatal("expected expression, found statement (`let`)");
db.note("variable declaration using `let` is a statement");
return Err(db);
} else if self.check(&token::ModSep) ||
self.token.is_ident() &&
!self.check_keyword(keywords::True) &&
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail-fulldeps/qquote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// ignore-cross-compile

// error-pattern:expected identifier, found keyword `let`
// error-pattern:expected expression, found statement (`let`)

#![feature(quote, rustc_private)]

Expand Down
28 changes: 28 additions & 0 deletions src/test/run-pass/lexical-scoping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2016 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.

// Tests that items in subscopes can shadow type parameters and local variables (see issue #23880).

#![allow(unused)]
struct Foo<X> { x: Box<X> }
impl<Bar> Foo<Bar> {
fn foo(&self) {
type Bar = i32;
let _: Bar = 42;
}
}

fn main() {
let f = 1;
{
fn f() {}
f();
}
}
Loading