diff --git a/src/doc/book/closures.md b/src/doc/book/closures.md index dedf9d5c28abd..a6b4e9492181c 100644 --- a/src/doc/book/closures.md +++ b/src/doc/book/closures.md @@ -322,7 +322,7 @@ to our closure when we pass it to `call_with_one`, so we use `&||`. A quick note about closures that use explicit lifetimes. Sometimes you might have a closure that takes a reference like so: -``` +```rust fn call_with_ref(some_closure:F) -> i32 where F: Fn(&i32) -> i32 { @@ -334,8 +334,8 @@ fn call_with_ref(some_closure:F) -> i32 Normally you can specify the lifetime of the parameter to our closure. We could annotate it on the function declaration: -```ignore -fn call_with_ref<'a, F>(some_closure:F) -> i32 +```rust,ignore +fn call_with_ref<'a, F>(some_closure:F) -> i32 where F: Fn(&'a 32) -> i32 { ``` @@ -353,11 +353,11 @@ fn call_with_ref(some_closure:F) -> i32 where F: for<'a> Fn(&'a 32) -> i32 { ``` -This lets the Rust compiler find the minimum lifetime to invoke our closure and +This lets the Rust compiler find the minimum lifetime to invoke our closure and satisfy the borrow checker's rules. Our function then compiles and excutes as we expect. -``` +```rust fn call_with_ref(some_closure:F) -> i32 where F: for<'a> Fn(&'a i32) -> i32 { diff --git a/src/doc/book/crates-and-modules.md b/src/doc/book/crates-and-modules.md index 43ac30c35c6c6..67fe8ba2c11a4 100644 --- a/src/doc/book/crates-and-modules.md +++ b/src/doc/book/crates-and-modules.md @@ -22,12 +22,10 @@ As an example, let’s make a *phrases* crate, which will give us various phrase in different languages. To keep things simple, we’ll stick to ‘greetings’ and ‘farewells’ as two kinds of phrases, and use English and Japanese (日本語) as two languages for those phrases to be in. We’ll use this module layout: - ```text +-----------+ +---| greetings | - | +-----------+ - +---------+ | + +---------+ | +-----------+ +---| english |---+ | +---------+ | +-----------+ | +---| farewells | @@ -37,8 +35,7 @@ two languages for those phrases to be in. We’ll use this module layout: | +---| greetings | | +----------+ | +-----------+ +---| japanese |--+ - +----------+ | - | +-----------+ + +----------+ | +-----------+ +---| farewells | +-----------+ ``` diff --git a/src/doc/book/ownership.md b/src/doc/book/ownership.md index f445bed015c00..c3e32e56c4266 100644 --- a/src/doc/book/ownership.md +++ b/src/doc/book/ownership.md @@ -67,7 +67,7 @@ Vectors have a [generic type][generics] `Vec`, so in this example `v` will ha [arrays]: primitive-types.html#arrays [vectors]: vectors.html -[heap]: the-stack-and-the-heap.html +[heap]: the-stack-and-the-heap.html#the-heap [stack]: the-stack-and-the-heap.html#the-stack [bindings]: variable-bindings.html [generics]: generics.html @@ -136,6 +136,8 @@ Rust allocates memory for an integer [i32] on the [stack][sh], copies the bit pattern representing the value of 10 to the allocated memory and binds the variable name x to this memory region for future reference. +[i32]: primitive-types.html#numeric-types + Now consider the following code fragment: ```rust diff --git a/src/doc/book/traits.md b/src/doc/book/traits.md index 107ef2b44d5eb..e685cb129b939 100644 --- a/src/doc/book/traits.md +++ b/src/doc/book/traits.md @@ -397,10 +397,10 @@ fn normal>(x: &T) -> i64 { } // can be called with T == i64 -fn inverse() -> T +fn inverse(x: i32) -> T // this is using ConvertTo as if it were "ConvertTo" where i32: ConvertTo { - 42.convert() + x.convert() } ``` diff --git a/src/doc/reference.md b/src/doc/reference.md index fb8ea0f5661d3..59dbffd6e28e7 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -114,12 +114,20 @@ Non-doc comments are interpreted as a form of whitespace. ## Whitespace -Whitespace is any non-empty string containing only the following characters: - +Whitespace is any non-empty string containing only characters that have the +`Pattern_White_Space` Unicode property, namely: + +- `U+0009` (horizontal tab, `'\t'`) +- `U+000A` (line feed, `'\n'`) +- `U+000B` (vertical tab) +- `U+000C` (form feed) +- `U+000D` (carriage return, `'\r'`) - `U+0020` (space, `' '`) -- `U+0009` (tab, `'\t'`) -- `U+000A` (LF, `'\n'`) -- `U+000D` (CR, `'\r'`) +- `U+0085` (next line) +- `U+200E` (left-to-right mark) +- `U+200F` (right-to-left mark) +- `U+2028` (line separator) +- `U+2029` (paragraph separator) Rust is a "free-form" language, meaning that all forms of whitespace serve only to separate _tokens_ in the grammar, and have no semantic significance. diff --git a/src/libcollections/fmt.rs b/src/libcollections/fmt.rs index 6f77d79ab0bfe..15de0dd802d99 100644 --- a/src/libcollections/fmt.rs +++ b/src/libcollections/fmt.rs @@ -28,6 +28,7 @@ //! format!("{:?}", (3, 4)); // => "(3, 4)" //! format!("{value}", value=4); // => "4" //! format!("{} {}", 1, 2); // => "1 2" +//! format!("{:04}", 42); // => "0042" with leading zeros //! ``` //! //! From these, you can see that the first argument is a format string. It is diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 538613c7fac91..9040e4bf8db5f 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -673,45 +673,35 @@ extern "C" { "##, E0269: r##" -Functions must eventually return a value of their return type. For example, in -the following function: +A returned value was expected but not all control paths return one. + +Erroneous code example: ```compile_fail,E0269 fn abracada_FAIL() -> String { "this won't work".to_string(); + // error: not all control paths return a value } ``` -If the condition is true, the value `x` is returned, but if the condition is -false, control exits the `if` block and reaches a place where nothing is being -returned. All possible control paths must eventually return a `u8`, which is not -happening here. - -An easy fix for this in a complicated function is to specify a default return -value, if possible: +In the previous code, the function is supposed to return a `String`, however, +the code returns nothing (because of the ';'). Another erroneous code would be: -```ignore -fn foo(x: u8) -> u8 { - if x > 0 { - x // alternatively, `return x` +```compile_fail +fn abracada_FAIL(b: bool) -> u32 { + if b { + 0 + } else { + "a" // It fails because an `u32` was expected and something else is + // returned. } - // lots of other if branches - 0 // return 0 if all else fails } ``` It is advisable to find out what the unhandled cases are and check for them, returning an appropriate value or panicking if necessary. Check if you need -to remove a semicolon from the last expression, like in this case: - -```ignore -fn foo(x: u8) -> u8 { - inner(2*x + 1); -} -``` - -The semicolon discards the return value of `inner`, instead of returning -it from `foo`. +to remove a semicolon from the last expression, like in the first erroneous +code example. "##, E0270: r##" diff --git a/src/librustc_passes/consts.rs b/src/librustc_passes/consts.rs index 75bfe7c0f2f95..1ff53cfe992b4 100644 --- a/src/librustc_passes/consts.rs +++ b/src/librustc_passes/consts.rs @@ -25,10 +25,10 @@ // by borrowck::gather_loans use rustc::dep_graph::DepNode; -use rustc::ty::cast::{CastKind}; -use rustc_const_eval::{ConstEvalErr, lookup_const_fn_by_id, compare_lit_exprs}; +use rustc::ty::cast::CastKind; +use rustc_const_eval::{ConstEvalErr, compare_lit_exprs, lookup_const_fn_by_id}; use rustc_const_eval::{eval_const_expr_partial, lookup_const_by_id}; -use rustc_const_eval::ErrKind::{IndexOpFeatureGated, UnimplementedConstVal, MiscCatchAll, Math}; +use rustc_const_eval::ErrKind::{IndexOpFeatureGated, Math, MiscCatchAll, UnimplementedConstVal}; use rustc_const_eval::ErrKind::{ErroneousReferencedConstant, MiscBinaryOp, NonConstPath}; use rustc_const_eval::ErrKind::UnresolvedPath; use rustc_const_eval::EvalHint::ExprTypeChecked; @@ -70,12 +70,12 @@ struct CheckCrateVisitor<'a, 'tcx: 'a> { tcx: TyCtxt<'a, 'tcx, 'tcx>, mode: Mode, qualif: ConstQualif, - rvalue_borrows: NodeMap + rvalue_borrows: NodeMap, } impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> { - fn with_mode(&mut self, mode: Mode, f: F) -> R where - F: FnOnce(&mut CheckCrateVisitor<'a, 'gcx>) -> R, + fn with_mode(&mut self, mode: Mode, f: F) -> R + where F: FnOnce(&mut CheckCrateVisitor<'a, 'gcx>) -> R { let (old_mode, old_qualif) = (self.mode, self.qualif); self.mode = mode; @@ -86,17 +86,17 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> { r } - fn with_euv(&mut self, item_id: Option, f: F) -> R where - F: for<'b, 'tcx> FnOnce(&mut euv::ExprUseVisitor<'b, 'gcx, 'tcx>) -> R, + fn with_euv(&mut self, item_id: Option, f: F) -> R + where F: for<'b, 'tcx> FnOnce(&mut euv::ExprUseVisitor<'b, 'gcx, 'tcx>) -> R { let param_env = match item_id { Some(item_id) => ty::ParameterEnvironment::for_item(self.tcx, item_id), - None => self.tcx.empty_parameter_environment() + None => self.tcx.empty_parameter_environment(), }; - self.tcx.infer_ctxt(None, Some(param_env), ProjectionMode::AnyFinal).enter(|infcx| { - f(&mut euv::ExprUseVisitor::new(self, &infcx)) - }) + self.tcx + .infer_ctxt(None, Some(param_env), ProjectionMode::AnyFinal) + .enter(|infcx| f(&mut euv::ExprUseVisitor::new(self, &infcx))) } fn global_expr(&mut self, mode: Mode, expr: &hir::Expr) -> ConstQualif { @@ -110,13 +110,17 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> { } if let Err(err) = eval_const_expr_partial(self.tcx, expr, ExprTypeChecked, None) { match err.kind { - UnimplementedConstVal(_) => {}, - IndexOpFeatureGated => {}, - ErroneousReferencedConstant(_) => {}, - _ => self.tcx.sess.add_lint(CONST_ERR, expr.id, expr.span, - format!("constant evaluation error: {}. This will \ + UnimplementedConstVal(_) => {} + IndexOpFeatureGated => {} + ErroneousReferencedConstant(_) => {} + _ => { + self.tcx.sess.add_lint(CONST_ERR, + expr.id, + expr.span, + format!("constant evaluation error: {}. This will \ become a HARD ERROR in the future", - err.description())), + err.description())) + } } } self.with_mode(mode, |this| { @@ -142,9 +146,7 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> { } let mode = match fk { - FnKind::ItemFn(_, _, _, hir::Constness::Const, _, _, _) => { - Mode::ConstFn - } + FnKind::ItemFn(_, _, _, hir::Constness::Const, _, _, _) => Mode::ConstFn, FnKind::Method(_, m, _, _) => { if m.constness == hir::Constness::Const { Mode::ConstFn @@ -152,7 +154,7 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> { Mode::Var } } - _ => Mode::Var + _ => Mode::Var, }; let qualif = self.with_mode(mode, |this| { @@ -174,11 +176,7 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> { } /// Returns true if the call is to a const fn or method. - fn handle_const_fn_call(&mut self, - _expr: &hir::Expr, - def_id: DefId, - ret_ty: Ty<'gcx>) - -> bool { + fn handle_const_fn_call(&mut self, _expr: &hir::Expr, def_id: DefId, ret_ty: Ty<'gcx>) -> bool { if let Some(fn_like) = lookup_const_fn_by_id(self.tcx, def_id) { let qualif = self.fn_like(fn_like.kind(), fn_like.decl(), @@ -293,17 +291,21 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> { Some(Ordering::Less) | Some(Ordering::Equal) => {} Some(Ordering::Greater) => { - span_err!(self.tcx.sess, start.span, E0030, - "lower range bound must be less than or equal to upper"); + span_err!(self.tcx.sess, + start.span, + E0030, + "lower range bound must be less than or equal to upper"); } None => { - span_err!(self.tcx.sess, p.span, E0014, + span_err!(self.tcx.sess, + p.span, + E0014, "paths in {}s may only refer to constants", self.msg()); } } } - _ => intravisit::walk_pat(self, p) + _ => intravisit::walk_pat(self, p), } } @@ -313,13 +315,13 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> { match stmt.node { hir::StmtDecl(ref decl, _) => { match decl.node { - hir::DeclLocal(_) => {}, + hir::DeclLocal(_) => {} // Item statements are allowed - hir::DeclItem(_) => continue + hir::DeclItem(_) => continue, } } - hir::StmtExpr(_, _) => {}, - hir::StmtSemi(_, _) => {}, + hir::StmtExpr(_, _) => {} + hir::StmtSemi(_, _) => {} } self.add_qualif(ConstQualif::NOT_CONST); } @@ -352,7 +354,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> { // The count is checked elsewhere (typeck). let count = match node_ty.sty { ty::TyArray(_, n) => n, - _ => bug!() + _ => bug!(), }; // [element; 0] is always zero-sized. if count == 0 { @@ -377,7 +379,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> { } intravisit::walk_expr(self, ex); } - _ => intravisit::walk_expr(self, ex) + _ => intravisit::walk_expr(self, ex), } // Handle borrows on (or inside the autorefs of) this expression. @@ -417,19 +419,19 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> { if self.mode == Mode::Var && !self.qualif.intersects(ConstQualif::NOT_CONST) { match eval_const_expr_partial(self.tcx, ex, ExprTypeChecked, None) { Ok(_) => {} - Err(ConstEvalErr { kind: UnimplementedConstVal(_), ..}) | - Err(ConstEvalErr { kind: MiscCatchAll, ..}) | - Err(ConstEvalErr { kind: MiscBinaryOp, ..}) | - Err(ConstEvalErr { kind: NonConstPath, ..}) | - Err(ConstEvalErr { kind: UnresolvedPath, ..}) | - Err(ConstEvalErr { kind: ErroneousReferencedConstant(_), ..}) | - Err(ConstEvalErr { kind: Math(ConstMathErr::Overflow(Op::Shr)), ..}) | - Err(ConstEvalErr { kind: Math(ConstMathErr::Overflow(Op::Shl)), ..}) | - Err(ConstEvalErr { kind: IndexOpFeatureGated, ..}) => {}, + Err(ConstEvalErr { kind: UnimplementedConstVal(_), .. }) | + Err(ConstEvalErr { kind: MiscCatchAll, .. }) | + Err(ConstEvalErr { kind: MiscBinaryOp, .. }) | + Err(ConstEvalErr { kind: NonConstPath, .. }) | + Err(ConstEvalErr { kind: UnresolvedPath, .. }) | + Err(ConstEvalErr { kind: ErroneousReferencedConstant(_), .. }) | + Err(ConstEvalErr { kind: Math(ConstMathErr::Overflow(Op::Shr)), .. }) | + Err(ConstEvalErr { kind: Math(ConstMathErr::Overflow(Op::Shl)), .. }) | + Err(ConstEvalErr { kind: IndexOpFeatureGated, .. }) => {} Err(msg) => { - self.tcx.sess.add_lint(CONST_ERR, ex.id, - msg.span, - msg.description().into_owned()) + self.tcx + .sess + .add_lint(CONST_ERR, ex.id, msg.span, msg.description().into_owned()) } } } @@ -446,8 +448,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> { /// every nested expression. If the expression is not part /// of a const/static item, it is qualified for promotion /// instead of producing errors. -fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, - e: &hir::Expr, node_ty: Ty<'tcx>) { +fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node_ty: Ty<'tcx>) { match node_ty.sty { ty::TyStruct(def, _) | ty::TyEnum(def, _) if def.has_dtor() => { @@ -647,12 +648,9 @@ fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Exp Some(&ty::adjustment::AdjustUnsafeFnPointer) | Some(&ty::adjustment::AdjustMutToConstPointer) => {} - Some(&ty::adjustment::AdjustDerefRef( - ty::adjustment::AutoDerefRef { autoderefs, .. } - )) => { - if (0..autoderefs as u32).any(|autoderef| { - v.tcx.is_overloaded_autoderef(e.id, autoderef) - }) { + Some(&ty::adjustment::AdjustDerefRef(ty::adjustment::AutoDerefRef { autoderefs, .. })) => { + if (0..autoderefs as u32) + .any(|autoderef| v.tcx.is_overloaded_autoderef(e.id, autoderef)) { v.add_qualif(ConstQualif::NOT_CONST); } } @@ -660,12 +658,13 @@ fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Exp } pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) { - tcx.visit_all_items_in_krate(DepNode::CheckConst, &mut CheckCrateVisitor { - tcx: tcx, - mode: Mode::Var, - qualif: ConstQualif::NOT_CONST, - rvalue_borrows: NodeMap() - }); + tcx.visit_all_items_in_krate(DepNode::CheckConst, + &mut CheckCrateVisitor { + tcx: tcx, + mode: Mode::Var, + qualif: ConstQualif::NOT_CONST, + rvalue_borrows: NodeMap(), + }); tcx.sess.abort_if_errors(); } @@ -687,7 +686,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> { Categorization::Rvalue(..) | Categorization::Upvar(..) | - Categorization::Local(..) => break + Categorization::Local(..) => break, } } } @@ -697,8 +696,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> { cmt: mc::cmt<'tcx>, _loan_region: ty::Region, bk: ty::BorrowKind, - loan_cause: euv::LoanCause) - { + loan_cause: euv::LoanCause) { // Kind of hacky, but we allow Unsafe coercions in constants. // These occur when we convert a &T or *T to a *U, as well as // when making a thin pointer (e.g., `*T`) into a fat pointer @@ -707,7 +705,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> { euv::LoanCause::AutoUnsafe => { return; } - _ => { } + _ => {} } let mut cur = &cmt; @@ -744,27 +742,20 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> { } Categorization::Upvar(..) | - Categorization::Local(..) => break + Categorization::Local(..) => break, } } } - fn decl_without_init(&mut self, - _id: ast::NodeId, - _span: Span) {} + fn decl_without_init(&mut self, _id: ast::NodeId, _span: Span) {} fn mutate(&mut self, _assignment_id: ast::NodeId, _assignment_span: Span, _assignee_cmt: mc::cmt, - _mode: euv::MutateMode) {} + _mode: euv::MutateMode) { + } - fn matched_pat(&mut self, - _: &hir::Pat, - _: mc::cmt, - _: euv::MatchMode) {} + fn matched_pat(&mut self, _: &hir::Pat, _: mc::cmt, _: euv::MatchMode) {} - fn consume_pat(&mut self, - _consume_pat: &hir::Pat, - _cmt: mc::cmt, - _mode: euv::ConsumeMode) {} + fn consume_pat(&mut self, _consume_pat: &hir::Pat, _cmt: mc::cmt, _mode: euv::ConsumeMode) {} } diff --git a/src/librustc_passes/lib.rs b/src/librustc_passes/lib.rs index 1576ca6bdeaa4..6c1a8fec8a9e4 100644 --- a/src/librustc_passes/lib.rs +++ b/src/librustc_passes/lib.rs @@ -28,12 +28,15 @@ #![feature(rustc_private)] extern crate core; -#[macro_use] extern crate rustc; +#[macro_use] +extern crate rustc; extern crate rustc_const_eval; extern crate rustc_const_math; -#[macro_use] extern crate log; -#[macro_use] extern crate syntax; +#[macro_use] +extern crate log; +#[macro_use] +extern crate syntax; pub mod diagnostics; diff --git a/src/librustc_passes/loops.rs b/src/librustc_passes/loops.rs index 2174d1cf9b82a..dcd9f9cbe06d8 100644 --- a/src/librustc_passes/loops.rs +++ b/src/librustc_passes/loops.rs @@ -19,19 +19,24 @@ use syntax::codemap::Span; #[derive(Clone, Copy, PartialEq)] enum Context { - Normal, Loop, Closure + Normal, + Loop, + Closure, } #[derive(Copy, Clone)] struct CheckLoopVisitor<'a> { sess: &'a Session, - cx: Context + cx: Context, } pub fn check_crate(sess: &Session, map: &Map) { let _task = map.dep_graph.in_task(DepNode::CheckLoops); let krate = map.krate(); - krate.visit_all_items(&mut CheckLoopVisitor { sess: sess, cx: Normal }); + krate.visit_all_items(&mut CheckLoopVisitor { + sess: sess, + cx: Normal, + }); } impl<'a, 'v> Visitor<'v> for CheckLoopVisitor<'a> { @@ -53,14 +58,14 @@ impl<'a, 'v> Visitor<'v> for CheckLoopVisitor<'a> { } hir::ExprBreak(_) => self.require_loop("break", e.span), hir::ExprAgain(_) => self.require_loop("continue", e.span), - _ => intravisit::walk_expr(self, e) + _ => intravisit::walk_expr(self, e), } } } impl<'a> CheckLoopVisitor<'a> { - fn with_context(&mut self, cx: Context, f: F) where - F: FnOnce(&mut CheckLoopVisitor<'a>), + fn with_context(&mut self, cx: Context, f: F) + where F: FnOnce(&mut CheckLoopVisitor<'a>) { let old_cx = self.cx; self.cx = cx; @@ -72,12 +77,10 @@ impl<'a> CheckLoopVisitor<'a> { match self.cx { Loop => {} Closure => { - span_err!(self.sess, span, E0267, - "`{}` inside of a closure", name); + span_err!(self.sess, span, E0267, "`{}` inside of a closure", name); } Normal => { - span_err!(self.sess, span, E0268, - "`{}` outside of loop", name); + span_err!(self.sess, span, E0268, "`{}` outside of loop", name); } } } diff --git a/src/librustc_passes/no_asm.rs b/src/librustc_passes/no_asm.rs index 90f92c25b05ea..1c0e3a09cd895 100644 --- a/src/librustc_passes/no_asm.rs +++ b/src/librustc_passes/no_asm.rs @@ -19,9 +19,11 @@ use syntax::visit::Visitor; use syntax::visit; pub fn check_crate(sess: &Session, krate: &ast::Crate) { - if sess.target.target.options.allow_asm { return; } + if sess.target.target.options.allow_asm { + return; + } - visit::walk_crate(&mut CheckNoAsm { sess: sess, }, krate); + visit::walk_crate(&mut CheckNoAsm { sess: sess }, krate); } #[derive(Copy, Clone)] @@ -32,9 +34,13 @@ struct CheckNoAsm<'a> { impl<'a, 'v> Visitor<'v> for CheckNoAsm<'a> { fn visit_expr(&mut self, e: &ast::Expr) { match e.node { - ast::ExprKind::InlineAsm(_) => span_err!(self.sess, e.span, E0472, - "asm! is unsupported on this target"), - _ => {}, + ast::ExprKind::InlineAsm(_) => { + span_err!(self.sess, + e.span, + E0472, + "asm! is unsupported on this target") + } + _ => {} } visit::walk_expr(self, e) } diff --git a/src/librustc_passes/rvalues.rs b/src/librustc_passes/rvalues.rs index 137a50642fcf4..3850adfbfaf6c 100644 --- a/src/librustc_passes/rvalues.rs +++ b/src/librustc_passes/rvalues.rs @@ -14,7 +14,7 @@ use rustc::dep_graph::DepNode; use rustc::middle::expr_use_visitor as euv; use rustc::middle::mem_categorization as mc; -use rustc::ty::{self, TyCtxt, ParameterEnvironment}; +use rustc::ty::{self, ParameterEnvironment, TyCtxt}; use rustc::traits::ProjectionMode; use rustc::hir; @@ -40,30 +40,27 @@ impl<'a, 'tcx, 'v> intravisit::Visitor<'v> for RvalueContext<'a, 'tcx> { fn_id: ast::NodeId) { // FIXME (@jroesch) change this to be an inference context let param_env = ParameterEnvironment::for_item(self.tcx, fn_id); - self.tcx.infer_ctxt(None, Some(param_env.clone()), - ProjectionMode::AnyFinal).enter(|infcx| { - let mut delegate = RvalueContextDelegate { - tcx: infcx.tcx, - param_env: ¶m_env - }; - let mut euv = euv::ExprUseVisitor::new(&mut delegate, &infcx); - euv.walk_fn(fd, b); - }); + self.tcx + .infer_ctxt(None, Some(param_env.clone()), ProjectionMode::AnyFinal) + .enter(|infcx| { + let mut delegate = RvalueContextDelegate { + tcx: infcx.tcx, + param_env: ¶m_env, + }; + let mut euv = euv::ExprUseVisitor::new(&mut delegate, &infcx); + euv.walk_fn(fd, b); + }); intravisit::walk_fn(self, fk, fd, b, s) } } -struct RvalueContextDelegate<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { +struct RvalueContextDelegate<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> { tcx: TyCtxt<'a, 'gcx, 'tcx>, param_env: &'a ty::ParameterEnvironment<'gcx>, } impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for RvalueContextDelegate<'a, 'gcx, 'tcx> { - fn consume(&mut self, - _: ast::NodeId, - span: Span, - cmt: mc::cmt<'tcx>, - _: euv::ConsumeMode) { + fn consume(&mut self, _: ast::NodeId, span: Span, cmt: mc::cmt<'tcx>, _: euv::ConsumeMode) { debug!("consume; cmt: {:?}; type: {:?}", *cmt, cmt.ty); let ty = self.tcx.lift_to_global(&cmt.ty).unwrap(); if !ty.is_sized(self.tcx.global_tcx(), self.param_env, span) { @@ -73,16 +70,9 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for RvalueContextDelegate<'a, 'gcx, 'tc } } - fn matched_pat(&mut self, - _matched_pat: &hir::Pat, - _cmt: mc::cmt, - _mode: euv::MatchMode) {} + fn matched_pat(&mut self, _matched_pat: &hir::Pat, _cmt: mc::cmt, _mode: euv::MatchMode) {} - fn consume_pat(&mut self, - _consume_pat: &hir::Pat, - _cmt: mc::cmt, - _mode: euv::ConsumeMode) { - } + fn consume_pat(&mut self, _consume_pat: &hir::Pat, _cmt: mc::cmt, _mode: euv::ConsumeMode) {} fn borrow(&mut self, _borrow_id: ast::NodeId, @@ -93,10 +83,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for RvalueContextDelegate<'a, 'gcx, 'tc _loan_cause: euv::LoanCause) { } - fn decl_without_init(&mut self, - _id: ast::NodeId, - _span: Span) { - } + fn decl_without_init(&mut self, _id: ast::NodeId, _span: Span) {} fn mutate(&mut self, _assignment_id: ast::NodeId, diff --git a/src/librustc_passes/static_recursion.rs b/src/librustc_passes/static_recursion.rs index 245960a04f030..bcfd0159429e1 100644 --- a/src/librustc_passes/static_recursion.rs +++ b/src/librustc_passes/static_recursion.rs @@ -13,11 +13,11 @@ use rustc::dep_graph::DepNode; use rustc::hir::map as ast_map; -use rustc::session::{Session, CompileResult}; +use rustc::session::{CompileResult, Session}; use rustc::hir::def::{Def, DefMap}; use rustc::util::nodemap::NodeMap; -use syntax::{ast}; +use syntax::ast; use syntax::codemap::Span; use syntax::feature_gate::{GateIssue, emit_feature_err}; use rustc::hir::intravisit::{self, Visitor}; @@ -41,18 +41,17 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckCrateVisitor<'a, 'ast> { match it.node { hir::ItemStatic(..) | hir::ItemConst(..) => { - let mut recursion_visitor = - CheckItemRecursionVisitor::new(self, &it.span); + let mut recursion_visitor = CheckItemRecursionVisitor::new(self, &it.span); recursion_visitor.visit_item(it); - }, + } hir::ItemEnum(ref enum_def, ref generics) => { // We could process the whole enum, but handling the variants // with discriminant expressions one by one gives more specific, // less redundant output. for variant in &enum_def.variants { if let Some(_) = variant.node.disr_expr { - let mut recursion_visitor = - CheckItemRecursionVisitor::new(self, &variant.span); + let mut recursion_visitor = CheckItemRecursionVisitor::new(self, + &variant.span); recursion_visitor.populate_enum_discriminants(enum_def); recursion_visitor.visit_variant(variant, generics, it.id); } @@ -67,8 +66,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckCrateVisitor<'a, 'ast> { match ti.node { hir::ConstTraitItem(_, ref default) => { if let Some(_) = *default { - let mut recursion_visitor = - CheckItemRecursionVisitor::new(self, &ti.span); + let mut recursion_visitor = CheckItemRecursionVisitor::new(self, &ti.span); recursion_visitor.visit_trait_item(ti); } } @@ -80,8 +78,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckCrateVisitor<'a, 'ast> { fn visit_impl_item(&mut self, ii: &'ast hir::ImplItem) { match ii.node { hir::ImplItemKind::Const(..) => { - let mut recursion_visitor = - CheckItemRecursionVisitor::new(self, &ii.span); + let mut recursion_visitor = CheckItemRecursionVisitor::new(self, &ii.span); recursion_visitor.visit_impl_item(ii); } _ => {} @@ -117,7 +114,8 @@ struct CheckItemRecursionVisitor<'a, 'ast: 'a> { } impl<'a, 'ast: 'a> CheckItemRecursionVisitor<'a, 'ast> { - fn new(v: &'a CheckCrateVisitor<'a, 'ast>, span: &'a Span) + fn new(v: &'a CheckCrateVisitor<'a, 'ast>, + span: &'a Span) -> CheckItemRecursionVisitor<'a, 'ast> { CheckItemRecursionVisitor { root_span: span, @@ -129,7 +127,8 @@ impl<'a, 'ast: 'a> CheckItemRecursionVisitor<'a, 'ast> { } } fn with_item_id_pushed(&mut self, id: ast::NodeId, f: F) - where F: Fn(&mut Self) { + where F: Fn(&mut Self) + { if self.idstack.iter().any(|&x| x == id) { let any_static = self.idstack.iter().any(|&x| { if let ast_map::NodeItem(item) = self.ast_map.get(x) { @@ -146,7 +145,9 @@ impl<'a, 'ast: 'a> CheckItemRecursionVisitor<'a, 'ast> { if !self.sess.features.borrow().static_recursion { emit_feature_err(&self.sess.parse_sess.span_diagnostic, "static_recursion", - *self.root_span, GateIssue::Language, "recursive static"); + *self.root_span, + GateIssue::Language, + "recursive static"); } } else { span_err!(self.sess, *self.root_span, E0265, "recursive constant"); @@ -170,7 +171,9 @@ impl<'a, 'ast: 'a> CheckItemRecursionVisitor<'a, 'ast> { // has no variants. let mut discriminant_map = self.discriminant_map.borrow_mut(); match enum_definition.variants.first() { - None => { return; } + None => { + return; + } Some(variant) if discriminant_map.contains_key(&variant.node.data.id()) => { return; } @@ -203,14 +206,19 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> { self.with_item_id_pushed(it.id, |v| intravisit::walk_item(v, it)); } - fn visit_enum_def(&mut self, enum_definition: &'ast hir::EnumDef, - generics: &'ast hir::Generics, item_id: ast::NodeId, _: Span) { + fn visit_enum_def(&mut self, + enum_definition: &'ast hir::EnumDef, + generics: &'ast hir::Generics, + item_id: ast::NodeId, + _: Span) { self.populate_enum_discriminants(enum_definition); intravisit::walk_enum_def(self, enum_definition, generics, item_id); } - fn visit_variant(&mut self, variant: &'ast hir::Variant, - _: &'ast hir::Generics, _: ast::NodeId) { + fn visit_variant(&mut self, + variant: &'ast hir::Variant, + _: &'ast hir::Generics, + _: ast::NodeId) { let variant_id = variant.node.data.id(); let maybe_expr; if let Some(get_expr) = self.discriminant_map.borrow().get(&variant_id) { @@ -246,18 +254,14 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> { Some(Def::Const(def_id)) => { if let Some(node_id) = self.ast_map.as_local_node_id(def_id) { match self.ast_map.get(node_id) { - ast_map::NodeItem(item) => - self.visit_item(item), - ast_map::NodeTraitItem(item) => - self.visit_trait_item(item), - ast_map::NodeImplItem(item) => - self.visit_impl_item(item), - ast_map::NodeForeignItem(_) => {}, + ast_map::NodeItem(item) => self.visit_item(item), + ast_map::NodeTraitItem(item) => self.visit_trait_item(item), + ast_map::NodeImplItem(item) => self.visit_impl_item(item), + ast_map::NodeForeignItem(_) => {} _ => { - span_bug!( - e.span, - "expected item, found {}", - self.ast_map.node_to_string(node_id)); + span_bug!(e.span, + "expected item, found {}", + self.ast_map.node_to_string(node_id)); } } } @@ -269,8 +273,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> { Some(Def::Variant(enum_id, variant_id)) => { if let Some(enum_node_id) = self.ast_map.as_local_node_id(enum_id) { if let hir::ItemEnum(ref enum_def, ref generics) = - self.ast_map.expect_item(enum_node_id).node - { + self.ast_map.expect_item(enum_node_id).node { self.populate_enum_discriminants(enum_def); let enum_id = self.ast_map.as_local_node_id(enum_id).unwrap(); let variant_id = self.ast_map.as_local_node_id(variant_id).unwrap(); @@ -283,10 +286,10 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> { } } } - _ => () + _ => (), } - }, - _ => () + } + _ => (), } intravisit::walk_expr(self, e); } diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index b0b55a76e266e..020d6f80c595d 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -189,8 +189,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } hir::ViewPathList(p, paths) => { let mine = paths.into_iter().filter(|path| { - !self.maybe_inline_local(path.node.id(), None, false, om, - please_inline) + !self.maybe_inline_local(path.node.id(), path.node.rename(), + false, om, please_inline) }).collect::>(); if mine.is_empty() { diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index ff80a4e305359..17d412411c048 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -217,7 +217,7 @@ impl f32 { /// // Values between `0` and `min` are Subnormal. /// assert!(!lower_than_min.is_normal()); /// ``` - /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number + /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn is_normal(self) -> bool { num::Float::is_normal(self) } @@ -923,12 +923,12 @@ impl f32 { /// Computes the tangent of a number (in radians). /// /// ``` - /// use std::f64; + /// use std::f32; /// - /// let x = f64::consts::PI/4.0; + /// let x = f32::consts::PI / 4.0; /// let abs_difference = (x.tan() - 1.0).abs(); /// - /// assert!(abs_difference < 1e-10); + /// assert!(abs_difference <= f32::EPSILON); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] @@ -1052,12 +1052,14 @@ impl f32 { /// number is close to zero. /// /// ``` - /// let x = 7.0f64; + /// use std::f32; /// - /// // e^(ln(7)) - 1 - /// let abs_difference = (x.ln().exp_m1() - 6.0).abs(); + /// let x = 6.0f32; /// - /// assert!(abs_difference < 1e-10); + /// // e^(ln(6)) - 1 + /// let abs_difference = (x.ln().exp_m1() - 5.0).abs(); + /// + /// assert!(abs_difference <= f32::EPSILON); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index b775031787084..70b7706535ca1 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -147,23 +147,23 @@ impl f64 { /// [subnormal][subnormal], or `NaN`. /// /// ``` - /// use std::f32; + /// use std::f64; /// - /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f64 - /// let max = f32::MAX; - /// let lower_than_min = 1.0e-40_f32; - /// let zero = 0.0f32; + /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64 + /// let max = f64::MAX; + /// let lower_than_min = 1.0e-308_f64; + /// let zero = 0.0f64; /// /// assert!(min.is_normal()); /// assert!(max.is_normal()); /// /// assert!(!zero.is_normal()); - /// assert!(!f32::NAN.is_normal()); - /// assert!(!f32::INFINITY.is_normal()); + /// assert!(!f64::NAN.is_normal()); + /// assert!(!f64::INFINITY.is_normal()); /// // Values between `0` and `min` are Subnormal. /// assert!(!lower_than_min.is_normal()); /// ``` - /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number + /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn is_normal(self) -> bool { num::Float::is_normal(self) } @@ -655,9 +655,9 @@ impl f64 { /// ``` /// #![feature(float_extras)] /// - /// let x = 1.0f32; + /// let x = 1.0f64; /// - /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs(); + /// let abs_diff = (x.next_after(2.0) - 1.0000000000000002220446049250313_f64).abs(); /// /// assert!(abs_diff < 1e-10); /// ``` diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 8dc46239f3d03..c103ff7f4b025 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -525,6 +525,26 @@ impl<'a> Hash for PrefixComponent<'a> { /// /// See the module documentation for an in-depth explanation of components and /// their role in the API. +/// +/// This `enum` is created from iterating over the [`path::Components`] +/// `struct`. +/// +/// # Examples +/// +/// ```rust +/// use std::path::{Component, Path}; +/// +/// let path = Path::new("/tmp/foo/bar.txt"); +/// let components = path.components().collect::>(); +/// assert_eq!(&components, &[ +/// Component::RootDir, +/// Component::Normal("tmp".as_ref()), +/// Component::Normal("foo".as_ref()), +/// Component::Normal("bar.txt".as_ref()), +/// ]); +/// ``` +/// +/// [`path::Components`]: struct.Components.html #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub enum Component<'a> { diff --git a/src/libstd/primitive_docs.rs b/src/libstd/primitive_docs.rs index 11af768c5b9b0..be9cd6a688858 100644 --- a/src/libstd/primitive_docs.rs +++ b/src/libstd/primitive_docs.rs @@ -490,9 +490,6 @@ mod prim_tuple { } /// /// *[See also the `std::f32` module](f32/index.html).* /// -/// However, please note that examples are shared between the `f64` and `f32` -/// primitive types. So it's normal if you see usage of `f64` in there. -/// mod prim_f32 { } #[doc(primitive = "f64")] @@ -501,9 +498,6 @@ mod prim_f32 { } /// /// *[See also the `std::f64` module](f64/index.html).* /// -/// However, please note that examples are shared between the `f64` and `f32` -/// primitive types. So it's normal if you see usage of `f32` in there. -/// mod prim_f64 { } #[doc(primitive = "i8")] diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 3bee878de3585..e9736fea7b37f 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -394,6 +394,19 @@ pub fn sleep_ms(ms: u32) { /// signal being received or a spurious wakeup. Platforms which do not support /// nanosecond precision for sleeping will have `dur` rounded up to the nearest /// granularity of time they can sleep for. +/// +/// # Examples +/// +/// ```rust,no_run +/// use std::{thread, time}; +/// +/// let ten_millis = time::Duration::from_millis(10); +/// let now = time::Instant::now(); +/// +/// thread::sleep(ten_millis); +/// +/// assert!(now.elapsed() >= ten_millis); +/// ``` #[stable(feature = "thread_sleep", since = "1.4.0")] pub fn sleep(dur: Duration) { imp::Thread::sleep(dur) diff --git a/src/test/rustdoc/issue-34473.rs b/src/test/rustdoc/issue-34473.rs new file mode 100644 index 0000000000000..a6de638854f65 --- /dev/null +++ b/src/test/rustdoc/issue-34473.rs @@ -0,0 +1,22 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_name = "foo"] + +mod second { + pub struct SomeTypeWithLongName; +} + +// @has foo/index.html +// @!has - SomeTypeWithLongName +// @has foo/struct.SomeType.html +// @!has - SomeTypeWithLongName +// @!has foo/struct.SomeTypeWithLongName.html +pub use second::{SomeTypeWithLongName as SomeType};