From ca8708273b6d7ee506e7d03051778d520cfaa50f Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 1 Mar 2017 01:30:41 +0200 Subject: [PATCH 01/29] more through normalization in typeck & trans Fixes #27901. Fixes #28828. Fixes #38135. Fixes #39363. --- src/librustc_trans/base.rs | 9 +---- src/librustc_trans/callee.rs | 18 ++------- src/librustc_trans/collector.rs | 11 ++---- src/librustc_trans/common.rs | 12 +++++- src/librustc_trans/consts.rs | 9 +++-- src/librustc_trans/debuginfo/metadata.rs | 9 ++--- src/librustc_trans/debuginfo/mod.rs | 11 ++---- src/librustc_trans/partitioning.rs | 9 +---- src/librustc_trans/trans_item.rs | 9 ++--- src/librustc_typeck/astconv.rs | 24 ++++++------ src/librustc_typeck/check/mod.rs | 50 ++++++++---------------- src/librustc_typeck/collect.rs | 13 +++--- src/test/run-pass/issue-27901.rs | 20 ++++++++++ src/test/run-pass/issue-28828.rs | 27 +++++++++++++ 14 files changed, 121 insertions(+), 110 deletions(-) create mode 100644 src/test/run-pass/issue-27901.rs create mode 100644 src/test/run-pass/issue-28828.rs diff --git a/src/librustc_trans/base.rs b/src/librustc_trans/base.rs index 8125f432ff5ae..36f6fa7643909 100644 --- a/src/librustc_trans/base.rs +++ b/src/librustc_trans/base.rs @@ -596,10 +596,7 @@ pub fn trans_instance<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, instance: Instance // release builds. info!("trans_instance({})", instance); - let fn_ty = ccx.tcx().item_type(instance.def); - let fn_ty = ccx.tcx().erase_regions(&fn_ty); - let fn_ty = monomorphize::apply_param_substs(ccx.shared(), instance.substs, &fn_ty); - + let fn_ty = common::def_ty(ccx.shared(), instance.def, instance.substs); let sig = common::ty_fn_sig(ccx, fn_ty); let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&sig); @@ -626,9 +623,7 @@ pub fn trans_ctor_shim<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, attributes::inline(llfn, attributes::InlineAttr::Hint); attributes::set_frame_pointer_elimination(ccx, llfn); - let ctor_ty = ccx.tcx().item_type(def_id); - let ctor_ty = monomorphize::apply_param_substs(ccx.shared(), substs, &ctor_ty); - + let ctor_ty = common::def_ty(ccx.shared(), def_id, substs); let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&ctor_ty.fn_sig()); let fn_ty = FnType::new(ccx, sig, &[]); diff --git a/src/librustc_trans/callee.rs b/src/librustc_trans/callee.rs index 4925c9d547e9d..762aaf1ce1d1b 100644 --- a/src/librustc_trans/callee.rs +++ b/src/librustc_trans/callee.rs @@ -24,14 +24,15 @@ use abi::{Abi, FnType}; use attributes; use base; use builder::Builder; -use common::{self, CrateContext, SharedCrateContext}; +use common::{self, CrateContext}; use cleanup::CleanupScope; use mir::lvalue::LvalueRef; use consts; +use common::def_ty; use declare; use value::Value; use meth; -use monomorphize::{self, Instance}; +use monomorphize::Instance; use trans_item::TransItem; use type_of; use Disr; @@ -207,16 +208,6 @@ impl<'tcx> Callee<'tcx> { } } -/// Given a DefId and some Substs, produces the monomorphic item type. -fn def_ty<'a, 'tcx>(shared: &SharedCrateContext<'a, 'tcx>, - def_id: DefId, - substs: &'tcx Substs<'tcx>) - -> Ty<'tcx> { - let ty = shared.tcx().item_type(def_id); - monomorphize::apply_param_substs(shared, substs, &ty) -} - - fn trans_closure_method<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>, def_id: DefId, substs: ty::ClosureSubsts<'tcx>, @@ -544,8 +535,7 @@ fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let substs = tcx.normalize_associated_type(&substs); let instance = Instance::new(def_id, substs); - let item_ty = ccx.tcx().item_type(def_id); - let fn_ty = monomorphize::apply_param_substs(ccx.shared(), substs, &item_ty); + let fn_ty = common::def_ty(ccx.shared(), def_id, substs); if let Some(&llfn) = ccx.instances().borrow().get(&instance) { return (llfn, fn_ty); diff --git a/src/librustc_trans/collector.rs b/src/librustc_trans/collector.rs index b12c1220b2b4d..7e349c6d0cb1d 100644 --- a/src/librustc_trans/collector.rs +++ b/src/librustc_trans/collector.rs @@ -207,7 +207,7 @@ use syntax_pos::DUMMY_SP; use base::custom_coerce_unsize_info; use callee::needs_fn_once_adapter_shim; use context::SharedCrateContext; -use common::fulfill_obligation; +use common::{def_ty, fulfill_obligation}; use glue::{self, DropGlueKind}; use monomorphize::{self, Instance}; use util::nodemap::{FxHashSet, FxHashMap, DefIdMap}; @@ -341,7 +341,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(scx: &SharedCrateContext<'a, 'tcx>, // Sanity check whether this ended up being collected accidentally debug_assert!(should_trans_locally(scx.tcx(), def_id)); - let ty = scx.tcx().item_type(def_id); + let ty = def_ty(scx, def_id, Substs::empty()); let ty = glue::get_drop_glue_type(scx, ty); neighbors.push(TransItem::DropGlue(DropGlueKind::Ty(ty))); @@ -815,10 +815,7 @@ fn find_drop_glue_neighbors<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>, } ty::TyAdt(def, substs) => { for field in def.all_fields() { - let field_type = scx.tcx().item_type(field.did); - let field_type = monomorphize::apply_param_substs(scx, - substs, - &field_type); + let field_type = def_ty(scx, field.did, substs); let field_type = glue::get_drop_glue_type(scx, field_type); if scx.type_needs_drop(field_type) { @@ -1184,7 +1181,7 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> { debug!("RootCollector: ADT drop-glue for {}", def_id_to_string(self.scx.tcx(), def_id)); - let ty = self.scx.tcx().item_type(def_id); + let ty = def_ty(self.scx, def_id, Substs::empty()); let ty = glue::get_drop_glue_type(self.scx, ty); self.output.push(TransItem::DropGlue(DropGlueKind::Ty(ty))); } diff --git a/src/librustc_trans/common.rs b/src/librustc_trans/common.rs index 1032da7ef75bb..a509587f80fd0 100644 --- a/src/librustc_trans/common.rs +++ b/src/librustc_trans/common.rs @@ -29,7 +29,7 @@ use type_::Type; use value::Value; use rustc::ty::{self, Ty, TyCtxt}; use rustc::ty::layout::Layout; -use rustc::ty::subst::Subst; +use rustc::ty::subst::{Subst, Substs}; use rustc::traits::{self, SelectionContext, Reveal}; use rustc::hir; @@ -604,3 +604,13 @@ pub fn ty_fn_sig<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, pub fn is_closure(tcx: TyCtxt, def_id: DefId) -> bool { tcx.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr } + +/// Given a DefId and some Substs, produces the monomorphic item type. +pub fn def_ty<'a, 'tcx>(shared: &SharedCrateContext<'a, 'tcx>, + def_id: DefId, + substs: &'tcx Substs<'tcx>) + -> Ty<'tcx> +{ + let ty = shared.tcx().item_type(def_id); + monomorphize::apply_param_substs(shared, substs, &ty) +} diff --git a/src/librustc_trans/consts.rs b/src/librustc_trans/consts.rs index 011f7748f2c98..bf1d9886ae7f0 100644 --- a/src/librustc_trans/consts.rs +++ b/src/librustc_trans/consts.rs @@ -18,12 +18,13 @@ use rustc::hir::map as hir_map; use {debuginfo, machine}; use base; use trans_item::TransItem; -use common::{CrateContext, val_ty}; +use common::{self, CrateContext, val_ty}; use declare; -use monomorphize::{Instance}; +use monomorphize::Instance; use type_::Type; use type_of; use rustc::ty; +use rustc::ty::subst::Substs; use rustc::hir; @@ -84,7 +85,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef { return g; } - let ty = ccx.tcx().item_type(def_id); + let ty = common::def_ty(ccx.shared(), def_id, Substs::empty()); let g = if let Some(id) = ccx.tcx().hir.as_local_node_id(def_id) { let llty = type_of::type_of(ccx, ty); @@ -234,7 +235,7 @@ pub fn trans_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, v }; - let ty = ccx.tcx().item_type(def_id); + let ty = common::def_ty(ccx.shared(), def_id, Substs::empty()); let llty = type_of::type_of(ccx, ty); let g = if val_llty == llty { g diff --git a/src/librustc_trans/debuginfo/metadata.rs b/src/librustc_trans/debuginfo/metadata.rs index f6cdd883850cc..049178a2575f3 100644 --- a/src/librustc_trans/debuginfo/metadata.rs +++ b/src/librustc_trans/debuginfo/metadata.rs @@ -33,7 +33,7 @@ use rustc::ty::util::TypeIdHasher; use rustc::hir; use rustc_data_structures::ToHex; use {type_of, machine, monomorphize}; -use common::CrateContext; +use common::{self, CrateContext}; use type_::Type; use rustc::ty::{self, AdtKind, Ty, layout}; use session::config; @@ -377,7 +377,7 @@ fn subroutine_type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, span: Span) -> MetadataCreationResult { - let signature = cx.tcx().erase_late_bound_regions(&signature); + let signature = cx.tcx().erase_late_bound_regions_and_normalize(&signature); let mut signature_metadata: Vec = Vec::with_capacity(signature.inputs().len() + 1); @@ -1764,7 +1764,7 @@ pub fn create_global_var_metadata(cx: &CrateContext, }; let is_local_to_unit = is_node_local_to_unit(cx, node_id); - let variable_type = tcx.erase_regions(&tcx.item_type(node_def_id)); + let variable_type = common::def_ty(cx.shared(), node_def_id, Substs::empty()); let type_metadata = type_metadata(cx, variable_type, span); let var_name = tcx.item_name(node_def_id).to_string(); let linkage_name = mangled_name_of_item(cx, node_def_id, ""); @@ -1772,8 +1772,7 @@ pub fn create_global_var_metadata(cx: &CrateContext, let var_name = CString::new(var_name).unwrap(); let linkage_name = CString::new(linkage_name).unwrap(); - let ty = cx.tcx().item_type(node_def_id); - let global_align = type_of::align_of(cx, ty); + let global_align = type_of::align_of(cx, variable_type); unsafe { llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx), diff --git a/src/librustc_trans/debuginfo/mod.rs b/src/librustc_trans/debuginfo/mod.rs index d5f04542d0255..6933f15825620 100644 --- a/src/librustc_trans/debuginfo/mod.rs +++ b/src/librustc_trans/debuginfo/mod.rs @@ -27,9 +27,9 @@ use rustc::hir::def_id::DefId; use rustc::ty::subst::Substs; use abi::Abi; -use common::CrateContext; +use common::{self, CrateContext}; use builder::Builder; -use monomorphize::{self, Instance}; +use monomorphize::Instance; use rustc::ty::{self, Ty}; use rustc::mir; use session::config::{self, FullDebugInfo, LimitedDebugInfo, NoDebugInfo}; @@ -397,11 +397,8 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let self_type = cx.tcx().impl_of_method(instance.def).and_then(|impl_def_id| { // If the method does *not* belong to a trait, proceed if cx.tcx().trait_id_of_impl(impl_def_id).is_none() { - let impl_self_ty = cx.tcx().item_type(impl_def_id); - let impl_self_ty = cx.tcx().erase_regions(&impl_self_ty); - let impl_self_ty = monomorphize::apply_param_substs(cx.shared(), - instance.substs, - &impl_self_ty); + let impl_self_ty = + common::def_ty(cx.shared(), impl_def_id, instance.substs); // Only "class" methods are generally understood by LLVM, // so avoid methods on other types (e.g. `<*mut T>::null`). diff --git a/src/librustc_trans/partitioning.rs b/src/librustc_trans/partitioning.rs index 706f131ed627f..cc9fd8f46f6f0 100644 --- a/src/librustc_trans/partitioning.rs +++ b/src/librustc_trans/partitioning.rs @@ -103,9 +103,9 @@ //! inlining, even when they are not marked #[inline]. use collector::InliningMap; +use common; use context::SharedCrateContext; use llvm; -use monomorphize; use rustc::dep_graph::{DepNode, WorkProductId}; use rustc::hir::def_id::DefId; use rustc::hir::map::DefPathData; @@ -468,12 +468,7 @@ fn characteristic_def_id_of_trans_item<'a, 'tcx>(scx: &SharedCrateContext<'a, 't if let Some(impl_def_id) = tcx.impl_of_method(instance.def) { // This is a method within an inherent impl, find out what the // self-type is: - let impl_self_ty = tcx.item_type(impl_def_id); - let impl_self_ty = tcx.erase_regions(&impl_self_ty); - let impl_self_ty = monomorphize::apply_param_substs(scx, - instance.substs, - &impl_self_ty); - + let impl_self_ty = common::def_ty(scx, impl_def_id, instance.substs); if let Some(def_id) = characteristic_def_id_of_type(impl_self_ty) { return Some(def_id); } diff --git a/src/librustc_trans/trans_item.rs b/src/librustc_trans/trans_item.rs index d691fa6aadf2e..d19f04b9554fb 100644 --- a/src/librustc_trans/trans_item.rs +++ b/src/librustc_trans/trans_item.rs @@ -22,7 +22,7 @@ use common; use declare; use glue::DropGlueKind; use llvm; -use monomorphize::{self, Instance}; +use monomorphize::Instance; use rustc::dep_graph::DepNode; use rustc::hir; use rustc::hir::def_id::DefId; @@ -146,7 +146,7 @@ impl<'a, 'tcx> TransItem<'tcx> { linkage: llvm::Linkage, symbol_name: &str) { let def_id = ccx.tcx().hir.local_def_id(node_id); - let ty = ccx.tcx().item_type(def_id); + let ty = common::def_ty(ccx.shared(), def_id, Substs::empty()); let llty = type_of::type_of(ccx, ty); let g = declare::define_global(ccx, symbol_name, llty).unwrap_or_else(|| { @@ -168,10 +168,7 @@ impl<'a, 'tcx> TransItem<'tcx> { assert!(!instance.substs.needs_infer() && !instance.substs.has_param_types()); - let item_ty = ccx.tcx().item_type(instance.def); - let item_ty = ccx.tcx().erase_regions(&item_ty); - let mono_ty = monomorphize::apply_param_substs(ccx.shared(), instance.substs, &item_ty); - + let mono_ty = common::def_ty(ccx.shared(), instance.def, instance.substs); let attrs = ccx.tcx().get_attrs(instance.def); let lldecl = declare::declare_fn(ccx, symbol_name, mono_ty); unsafe { llvm::LLVMRustSetLinkage(lldecl, linkage) }; diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 577fe31eab02a..923ec05c22b77 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -79,13 +79,8 @@ pub trait AstConv<'gcx, 'tcx> { item_name: ast::Name) -> Ty<'tcx>; - /// Project an associated type from a non-higher-ranked trait reference. - /// This is fairly straightforward and can be accommodated in any context. - fn projected_ty(&self, - span: Span, - _trait_ref: ty::TraitRef<'tcx>, - _item_name: ast::Name) - -> Ty<'tcx>; + /// Normalize an associated type coming from the user. + fn normalize_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx>; /// Invoked when we encounter an error from some prior pass /// (e.g. resolve) that is translated into a ty-error. This is @@ -310,8 +305,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o { tcx.types.err } else { // This is a default type parameter. - ty::queries::ty::get(tcx, span, def.def_id) - .subst_spanned(tcx, substs, Some(span)) + self.normalize_ty( + span, + ty::queries::ty::get(tcx, span, def.def_id) + .subst_spanned(tcx, substs, Some(span)) + ) } } else { // We've already errored above about the mismatch. @@ -600,7 +598,10 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o { -> Ty<'tcx> { let substs = self.ast_path_substs_for_ty(span, did, item_segment); - ty::queries::ty::get(self.tcx(), span, did).subst(self.tcx(), substs) + self.normalize_ty( + span, + ty::queries::ty::get(self.tcx(), span, did).subst(self.tcx(), substs) + ) } /// Transform a PolyTraitRef into a PolyExistentialTraitRef by @@ -900,6 +901,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o { let trait_did = bound.0.def_id; let ty = self.projected_ty_from_poly_trait_ref(span, bound, assoc_name); + let ty = self.normalize_ty(span, ty); let item = tcx.associated_items(trait_did).find(|i| i.name == assoc_name); let def_id = item.expect("missing associated type").def_id; @@ -939,7 +941,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o { debug!("qpath_to_ty: trait_ref={:?}", trait_ref); - self.projected_ty(span, trait_ref, item_segment.name) + self.normalize_ty(span, tcx.mk_projection(trait_ref, item_segment.name)) } pub fn prohibit_type_params(&self, segments: &[hir::PathSegment]) { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 0337727dcba9a..c3271914768dd 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -739,8 +739,9 @@ fn typeck_tables<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, check_fn(&inh, fn_sig, decl, id, body) } else { - let expected_type = tcx.item_type(def_id); let fcx = FnCtxt::new(&inh, None, body.value.id); + let expected_type = tcx.item_type(def_id); + let expected_type = fcx.normalize_associated_types_in(body.value.span, &expected_type); fcx.require_type_is_sized(expected_type, body.value.span, traits::ConstSized); // Gather locals in statics (because of block expressions). @@ -1442,16 +1443,15 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> { infer::LateBoundRegionConversionTime::AssocTypeProjection(item_name), &poly_trait_ref); - self.normalize_associated_type(span, trait_ref, item_name) + self.tcx().mk_projection(trait_ref, item_name) } - fn projected_ty(&self, - span: Span, - trait_ref: ty::TraitRef<'tcx>, - item_name: ast::Name) - -> Ty<'tcx> - { - self.normalize_associated_type(span, trait_ref, item_name) + fn normalize_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx> { + if ty.has_escaping_regions() { + ty // FIXME: normalization and escaping regions + } else { + self.normalize_associated_types_in(span, &ty) + } } fn set_tainted_by_errors(&self) { @@ -1728,25 +1728,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { self.inh.normalize_associated_types_in(span, self.body_id, value) } - fn normalize_associated_type(&self, - span: Span, - trait_ref: ty::TraitRef<'tcx>, - item_name: ast::Name) - -> Ty<'tcx> - { - let cause = traits::ObligationCause::new(span, - self.body_id, - traits::ObligationCauseCode::MiscObligation); - self.fulfillment_cx - .borrow_mut() - .normalize_projection_type(self, - ty::ProjectionTy { - trait_ref: trait_ref, - item_name: item_name, - }, - cause) - } - pub fn write_nil(&self, node_id: ast::NodeId) { self.write_ty(node_id, self.tcx.mk_nil()); } @@ -1777,9 +1758,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } pub fn register_bound(&self, - ty: Ty<'tcx>, - def_id: DefId, - cause: traits::ObligationCause<'tcx>) + ty: Ty<'tcx>, + def_id: DefId, + cause: traits::ObligationCause<'tcx>) { self.fulfillment_cx.borrow_mut() .register_bound(self, ty, def_id, cause); @@ -1788,8 +1769,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { pub fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) { - debug!("register_predicate({:?})", - obligation); + debug!("register_predicate({:?})", obligation); + if obligation.has_escaping_regions() { + span_bug!(obligation.cause.span, "escaping regions in predicate {:?}", + obligation); + } self.fulfillment_cx .borrow_mut() .register_predicate_obligation(self, obligation); diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 7f413a0dfc3ab..cf4dfd9e0aeb1 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -291,7 +291,7 @@ impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> { -> Ty<'tcx> { if let Some(trait_ref) = self.tcx().no_late_bound_regions(&poly_trait_ref) { - self.projected_ty(span, trait_ref, item_name) + self.tcx().mk_projection(trait_ref, item_name) } else { // no late-bound regions, we can just ignore the binder span_err!(self.tcx().sess, span, E0212, @@ -301,13 +301,10 @@ impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> { } } - fn projected_ty(&self, - _span: Span, - trait_ref: ty::TraitRef<'tcx>, - item_name: ast::Name) - -> Ty<'tcx> - { - self.tcx().mk_projection(trait_ref, item_name) + fn normalize_ty(&self, _span: Span, ty: Ty<'tcx>) -> Ty<'tcx> { + // types in item signatures are not normalized, to avoid undue + // dependencies. + ty } fn set_tainted_by_errors(&self) { diff --git a/src/test/run-pass/issue-27901.rs b/src/test/run-pass/issue-27901.rs new file mode 100644 index 0000000000000..b7a9daaf8abd4 --- /dev/null +++ b/src/test/run-pass/issue-27901.rs @@ -0,0 +1,20 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +trait Stream { type Item; } +impl<'a> Stream for &'a str { type Item = u8; } +fn f<'s>(s: &'s str) -> (&'s str, <&'s str as Stream>::Item) { + (s, 42) +} + +fn main() { + let fx = f as for<'t> fn(&'t str) -> (&'t str, <&'t str as Stream>::Item); + assert_eq!(fx("hi"), ("hi", 42)); +} diff --git a/src/test/run-pass/issue-28828.rs b/src/test/run-pass/issue-28828.rs new file mode 100644 index 0000000000000..24e4b83e8a675 --- /dev/null +++ b/src/test/run-pass/issue-28828.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub trait Foo { + type Out; +} + +impl Foo for () { + type Out = bool; +} + +fn main() { + type Bool = <() as Foo>::Out; + + let x: Bool = true; + assert!(x); + + let y: Option = None; + assert_eq!(y, None); +} From 34ff9aa83fa014f77ffe8fa5a12268a033c95694 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 1 Mar 2017 02:29:57 +0200 Subject: [PATCH 02/29] store the normalized types of statics in MIR Lvalues The types of statics, like all other items, are stored in the tcx unnormalized. This is necessarily so, because a) Item types other than statics have generics, which can't be normalized. b) Eager normalization causes undesirable on-demand dependencies. Keeping with the principle that MIR lvalues require no normalization in order to interpret, this patch stores the normalized type of the statics in the Lvalue and reads it to get the lvalue type. Fixes #39367. --- src/librustc/mir/mod.rs | 14 +++++-- src/librustc/mir/tcx.rs | 4 +- src/librustc/mir/visit.rs | 24 ++++++++++-- src/librustc_mir/build/expr/as_lvalue.rs | 2 +- src/librustc_mir/transform/type_check.rs | 14 ++++++- src/librustc_trans/mir/constant.rs | 4 +- src/librustc_trans/mir/lvalue.rs | 5 +-- src/test/run-pass/issue-39367.rs | 49 ++++++++++++++++++++++++ 8 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 src/test/run-pass/issue-39367.rs diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 40ebc97a78a6c..a7e89e77f3401 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -816,12 +816,20 @@ pub enum Lvalue<'tcx> { Local(Local), /// static or static mut variable - Static(DefId), + Static(Box>), /// projection out of an lvalue (access a field, deref a pointer, etc) Projection(Box>), } +/// The def-id of a static, along with its normalized type (which is +/// stored to avoid requiring normalization when reading MIR). +#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable)] +pub struct Static<'tcx> { + pub def_id: DefId, + pub ty: Ty<'tcx>, +} + /// The `Projection` data structure defines things of the form `B.x` /// or `*B` or `B[index]`. Note that it is parameterized because it is /// shared between `Constant` and `Lvalue`. See the aliases @@ -911,8 +919,8 @@ impl<'tcx> Debug for Lvalue<'tcx> { match *self { Local(id) => write!(fmt, "{:?}", id), - Static(def_id) => - write!(fmt, "{}", ty::tls::with(|tcx| tcx.item_path_str(def_id))), + Static(box self::Static { def_id, ty }) => + write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.item_path_str(def_id)), ty), Projection(ref data) => match data.elem { ProjectionElem::Downcast(ref adt_def, index) => diff --git a/src/librustc/mir/tcx.rs b/src/librustc/mir/tcx.rs index 50a80305bee27..638655aee15aa 100644 --- a/src/librustc/mir/tcx.rs +++ b/src/librustc/mir/tcx.rs @@ -125,8 +125,8 @@ impl<'tcx> Lvalue<'tcx> { match *self { Lvalue::Local(index) => LvalueTy::Ty { ty: mir.local_decls[index].ty }, - Lvalue::Static(def_id) => - LvalueTy::Ty { ty: tcx.item_type(def_id) }, + Lvalue::Static(ref data) => + LvalueTy::Ty { ty: data.ty }, Lvalue::Projection(ref proj) => proj.base.ty(mir, tcx).projection_ty(tcx, &proj.elem), } diff --git a/src/librustc/mir/visit.rs b/src/librustc/mir/visit.rs index 7cdbd5cae061f..1172172a845c2 100644 --- a/src/librustc/mir/visit.rs +++ b/src/librustc/mir/visit.rs @@ -154,6 +154,13 @@ macro_rules! make_mir_visitor { self.super_lvalue(lvalue, context, location); } + fn visit_static(&mut self, + static_: & $($mutability)* Static<'tcx>, + context: LvalueContext<'tcx>, + location: Location) { + self.super_static(static_, context, location); + } + fn visit_projection(&mut self, lvalue: & $($mutability)* LvalueProjection<'tcx>, context: LvalueContext<'tcx>, @@ -559,8 +566,8 @@ macro_rules! make_mir_visitor { match *lvalue { Lvalue::Local(_) => { } - Lvalue::Static(ref $($mutability)* def_id) => { - self.visit_def_id(def_id, location); + Lvalue::Static(ref $($mutability)* static_) => { + self.visit_static(static_, context, location); } Lvalue::Projection(ref $($mutability)* proj) => { self.visit_projection(proj, context, location); @@ -568,6 +575,18 @@ macro_rules! make_mir_visitor { } } + fn super_static(&mut self, + static_: & $($mutability)* Static<'tcx>, + _context: LvalueContext<'tcx>, + location: Location) { + let Static { + ref $($mutability)* def_id, + ref $($mutability)* ty, + } = *static_; + self.visit_def_id(def_id, location); + self.visit_ty(ty); + } + fn super_projection(&mut self, proj: & $($mutability)* LvalueProjection<'tcx>, context: LvalueContext<'tcx>, @@ -837,4 +856,3 @@ impl<'tcx> LvalueContext<'tcx> { self.is_mutating_use() || self.is_nonmutating_use() } } - diff --git a/src/librustc_mir/build/expr/as_lvalue.rs b/src/librustc_mir/build/expr/as_lvalue.rs index 5abfe084f2258..ec412d4e9c62e 100644 --- a/src/librustc_mir/build/expr/as_lvalue.rs +++ b/src/librustc_mir/build/expr/as_lvalue.rs @@ -84,7 +84,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { block.and(Lvalue::Local(index)) } ExprKind::StaticRef { id } => { - block.and(Lvalue::Static(id)) + block.and(Lvalue::Static(Box::new(Static { def_id: id, ty: expr.ty }))) } ExprKind::Array { .. } | diff --git a/src/librustc_mir/transform/type_check.rs b/src/librustc_mir/transform/type_check.rs index af4a4a53905eb..c2faf27412c69 100644 --- a/src/librustc_mir/transform/type_check.rs +++ b/src/librustc_mir/transform/type_check.rs @@ -126,8 +126,18 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> { debug!("sanitize_lvalue: {:?}", lvalue); match *lvalue { Lvalue::Local(index) => LvalueTy::Ty { ty: self.mir.local_decls[index].ty }, - Lvalue::Static(def_id) => - LvalueTy::Ty { ty: self.tcx().item_type(def_id) }, + Lvalue::Static(box Static { def_id, ty: sty }) => { + let sty = self.sanitize_type(lvalue, sty); + let ty = self.tcx().item_type(def_id); + let ty = self.cx.normalize(&ty); + if let Err(terr) = self.cx.eq_types(self.last_span, ty, sty) { + span_mirbug!( + self, lvalue, "bad static type ({:?}: {:?}): {:?}", + ty, sty, terr); + } + LvalueTy::Ty { ty: sty } + + }, Lvalue::Projection(ref proj) => { let base_ty = self.sanitize_lvalue(&proj.base, location); if let LvalueTy::Ty { ty } = base_ty { diff --git a/src/librustc_trans/mir/constant.rs b/src/librustc_trans/mir/constant.rs index 771a5b7f366a1..fb2ef8f60c449 100644 --- a/src/librustc_trans/mir/constant.rs +++ b/src/librustc_trans/mir/constant.rs @@ -382,11 +382,11 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> { let lvalue = match *lvalue { mir::Lvalue::Local(_) => bug!(), // handled above - mir::Lvalue::Static(def_id) => { + mir::Lvalue::Static(box mir::Static { def_id, ty }) => { ConstLvalue { base: Base::Static(consts::get_static(self.ccx, def_id)), llextra: ptr::null_mut(), - ty: lvalue.ty(self.mir, tcx).to_ty(tcx) + ty: self.monomorphize(&ty), } } mir::Lvalue::Projection(ref projection) => { diff --git a/src/librustc_trans/mir/lvalue.rs b/src/librustc_trans/mir/lvalue.rs index 2538f32031fdb..49e1e3855571b 100644 --- a/src/librustc_trans/mir/lvalue.rs +++ b/src/librustc_trans/mir/lvalue.rs @@ -304,10 +304,9 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> { let result = match *lvalue { mir::Lvalue::Local(_) => bug!(), // handled above - mir::Lvalue::Static(def_id) => { - let const_ty = self.monomorphized_lvalue_ty(lvalue); + mir::Lvalue::Static(box mir::Static { def_id, ty }) => { LvalueRef::new_sized(consts::get_static(ccx, def_id), - LvalueTy::from_ty(const_ty), + LvalueTy::from_ty(self.monomorphize(&ty)), Alignment::AbiAligned) }, mir::Lvalue::Projection(box mir::Projection { diff --git a/src/test/run-pass/issue-39367.rs b/src/test/run-pass/issue-39367.rs new file mode 100644 index 0000000000000..3e72efada84e6 --- /dev/null +++ b/src/test/run-pass/issue-39367.rs @@ -0,0 +1,49 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::ops::Deref; + +struct ArenaSet::Target>(U, &'static V) + where V: 'static + ?Sized; + +static Z: [u8; 4] = [1,2,3,4]; + +fn arena() -> &'static ArenaSet> { + fn __static_ref_initialize() -> ArenaSet> { + ArenaSet(vec![], &Z) + } + unsafe { + use std::sync::{Once, ONCE_INIT}; + fn require_sync(_: &T) { } + unsafe fn __stability() -> &'static ArenaSet> { + use std::mem::transmute; + use std::boxed::Box; + static mut DATA: *const ArenaSet> = 0 as *const ArenaSet>; + + static mut ONCE: Once = ONCE_INIT; + ONCE.call_once(|| { + DATA = transmute + ::>>, *const ArenaSet>> + (Box::new(__static_ref_initialize())); + }); + + &*DATA + } + let static_ref = __stability(); + require_sync(static_ref); + static_ref + } +} + +fn main() { + let &ArenaSet(ref u, v) = arena(); + assert!(u.is_empty()); + assert_eq!(v, Z); +} From 2ecbc22856f7ac6acf2de9af65af7bbc81b35250 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 1 Mar 2017 03:39:43 +0200 Subject: [PATCH 03/29] fix a few more typeck normalization cases I'll like @nikomatsakis or someone to look at the unsolved variable case. --- src/librustc_typeck/check/mod.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index c3271914768dd..c42ef05bc5a77 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2092,10 +2092,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { Neither => { if let Some(default) = default_map.get(ty) { let default = default.clone(); + let default_ty = self.normalize_associated_types_in( + default.origin_span, &default.ty); match self.eq_types(false, &self.misc(default.origin_span), ty, - default.ty) { + default_ty) { Ok(ok) => self.register_infer_ok_obligations(ok), Err(_) => conflicts.push((*ty, default)), } @@ -4396,7 +4398,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } else if !infer_types && def.has_default { // No type parameter provided, but a default exists. let default = self.tcx.item_type(def.def_id); - default.subst_spanned(self.tcx, substs, Some(span)) + self.normalize_ty( + span, + default.subst_spanned(self.tcx, substs, Some(span)) + ) } else { // No type parameters were provided, we can infer all. // This can also be reached in some error cases: From 4aede759146260cddd194de71375d3f1b8183135 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Wed, 1 Mar 2017 02:44:01 +0200 Subject: [PATCH 04/29] transform broken MIR warnings to hard ICEs We ought to do that sometime, and this PR fixes all broken MIR errors I could find. --- src/librustc_mir/transform/type_check.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/librustc_mir/transform/type_check.rs b/src/librustc_mir/transform/type_check.rs index c2faf27412c69..fb7d708778ef6 100644 --- a/src/librustc_mir/transform/type_check.rs +++ b/src/librustc_mir/transform/type_check.rs @@ -25,22 +25,21 @@ use syntax_pos::{Span, DUMMY_SP}; use rustc_data_structures::indexed_vec::Idx; +fn mirbug(tcx: TyCtxt, span: Span, msg: &str) { + tcx.sess.diagnostic().span_bug(span, msg); +} + macro_rules! span_mirbug { ($context:expr, $elem:expr, $($message:tt)*) => ({ - $context.tcx().sess.span_warn( - $context.last_span, - &format!("broken MIR ({:?}): {}", $elem, format!($($message)*)) - ) + mirbug($context.tcx(), $context.last_span, + &format!("broken MIR ({:?}): {}", $elem, format!($($message)*))) }) } macro_rules! span_mirbug_and_err { ($context:expr, $elem:expr, $($message:tt)*) => ({ { - $context.tcx().sess.span_warn( - $context.last_span, - &format!("broken MIR ({:?}): {:?}", $elem, format!($($message)*)) - ); + span_mirbug!($context, $elem, $($message)*); $context.error() } }) From f6304e12281ed03f4123de6352d4bd7bbff6e0dd Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 15 Feb 2017 14:55:26 -0800 Subject: [PATCH 05/29] Add Cargo as a submodule --- .gitmodules | 3 + src/Cargo.lock | 632 ++++++++++++++++++++++++++++++++- src/Cargo.toml | 1 + src/tools/cargo | 1 + src/tools/cargotest/Cargo.toml | 2 +- 5 files changed, 625 insertions(+), 14 deletions(-) create mode 160000 src/tools/cargo diff --git a/.gitmodules b/.gitmodules index 86c5c780c5eb2..2beff77267efa 100644 --- a/.gitmodules +++ b/.gitmodules @@ -18,3 +18,6 @@ [submodule "src/liblibc"] path = src/liblibc url = https://github.com/rust-lang/libc.git +[submodule "src/tools/cargo"] + path = src/tools/cargo + url = https://github.com/rust-lang/cargo diff --git a/src/Cargo.lock b/src/Cargo.lock index 1fb5d34d13f5d..9a4fd3a1d5594 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -6,6 +6,23 @@ dependencies = [ "libc 0.0.0", ] +[[package]] +name = "advapi32-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "aho-corasick" version = "0.6.2" @@ -73,6 +90,11 @@ dependencies = [ "toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bufstream" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "build-manifest" version = "0.1.0" @@ -88,9 +110,74 @@ dependencies = [ "filetime 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cargo" +version = "0.18.0" +dependencies = [ + "advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "cargotest 0.1.0", + "crates-io 0.7.0", + "crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "curl 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "docopt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "filetime 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "flate2 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", + "fs2 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "git2 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "git2-curl 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "hamcrest 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "handlebars 0.25.1 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "libgit2-sys 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "psapi-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "shell-escape 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tar 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cargotest" version = "0.1.0" +dependencies = [ + "bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "cargo 0.18.0", + "filetime 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "flate2 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", + "git2 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "hamcrest 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "tar 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cargotest2" +version = "0.1.0" + +[[package]] +name = "cfg-if" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "clap" @@ -101,7 +188,7 @@ dependencies = [ "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "term_size 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "term_size 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -146,6 +233,56 @@ dependencies = [ name = "core" version = "0.0.0" +[[package]] +name = "crates-io" +version = "0.7.0" +dependencies = [ + "curl 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "crossbeam" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "curl" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "curl-sys 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-probe 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "curl-sys" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "libz-sys 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "docopt" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dtoa" version = "0.4.1" @@ -188,15 +325,48 @@ dependencies = [ "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "flate2" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "miniz-sys 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "fmt_macros" version = "0.0.0" +[[package]] +name = "foreign-types" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fs2" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gcc" version = "0.3.43" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "gdi32-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "getopts" version = "0.0.0" @@ -206,13 +376,51 @@ name = "getopts" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "git2" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "libgit2-sys 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-probe 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "git2-curl" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "curl 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "git2 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "glob" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "graphviz" version = "0.0.0" +[[package]] +name = "hamcrest" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "handlebars" -version = "0.25.0" +version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -221,7 +429,17 @@ dependencies = [ "quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "idna" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -255,6 +473,43 @@ name = "libc" version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "libgit2-sys" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "curl-sys 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "libssh2-sys 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libz-sys 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libssh2-sys" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "libz-sys 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libz-sys" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "linkchecker" version = "0.1.0" @@ -268,6 +523,11 @@ name = "log" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "matches" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "mdbook" version = "0.0.17" @@ -275,16 +535,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "clap 2.20.5 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "handlebars 0.25.0 (registry+https://github.com/rust-lang/crates.io-index)", + "handlebars 0.25.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "open 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "memchr" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "memchr" version = "1.0.1" @@ -293,6 +561,99 @@ dependencies = [ "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "miniz-sys" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "miow" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "net2" +version = "0.2.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-bigint 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-complex 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-rational 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-bigint" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-complex" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-integer" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-iter" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-rational" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-bigint 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "num-traits" version = "0.1.36" @@ -306,11 +667,48 @@ dependencies = [ "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num_cpus" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "open" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "openssl" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "foreign-types 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "openssl-probe" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "openssl-sys" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", + "gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "panic_abort" version = "0.0.0" @@ -334,6 +732,11 @@ name = "pest" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "pkg-config" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc_macro" version = "0.0.0" @@ -350,6 +753,15 @@ dependencies = [ "syntax_pos 0.0.0", ] +[[package]] +name = "psapi-sys" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "pulldown-cmark" version = "0.0.8" @@ -379,6 +791,26 @@ dependencies = [ "core 0.0.0", ] +[[package]] +name = "rand" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.1.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "0.2.1" @@ -387,10 +819,15 @@ dependencies = [ "aho-corasick 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "thread_local 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "regex-syntax" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "regex-syntax" version = "0.4.0" @@ -767,6 +1204,19 @@ dependencies = [ "syntax_pos 0.0.0", ] +[[package]] +name = "semver" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "serde" version = "0.9.7" @@ -774,7 +1224,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde_json" -version = "0.9.6" +version = "0.9.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -787,6 +1237,11 @@ dependencies = [ name = "serialize" version = "0.0.0" +[[package]] +name = "shell-escape" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "std" version = "0.0.0" @@ -854,13 +1309,39 @@ dependencies = [ "serialize 0.0.0", ] +[[package]] +name = "tar" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "filetime 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tempdir" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "term" version = "0.0.0" +[[package]] +name = "term" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "term_size" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -876,6 +1357,15 @@ dependencies = [ "term 0.0.0", ] +[[package]] +name = "thread-id" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread-id" version = "3.0.0" @@ -887,7 +1377,15 @@ dependencies = [ [[package]] name = "thread_local" -version = "0.3.2" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -906,6 +1404,14 @@ dependencies = [ "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "toml" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "toml" version = "0.3.0" @@ -914,6 +1420,19 @@ dependencies = [ "serde 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-bidi" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-segmentation" version = "1.1.0" @@ -932,6 +1451,29 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "url" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "user32-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "utf8-ranges" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "utf8-ranges" version = "1.0.0" @@ -957,49 +1499,113 @@ name = "winapi-build" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "ws2_32-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [metadata] +"checksum advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e06588080cb19d0acb6739808aafa5f26bfb2ca015b2b6370028b44cf7cb8a9a" +"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" "checksum aho-corasick 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0638fd549427caa90c499814196d1b9e3725eb4d15d7339d6de073a680ed0ca2" "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" "checksum bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4f67931368edf3a9a51d29886d245f1c3db2f1ef0dcc9e35ff70341b78c10d23" "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" +"checksum bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b48dbe2ff0e98fa2f03377d204a9637d3c9816cd431bfe05a8abbd0ea11d074" +"checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" "checksum clap 2.20.5 (registry+https://github.com/rust-lang/crates.io-index)" = "7db281b0520e97fbd15cd615dcd8f8bcad0c26f5f7d5effe705f090f39e9a758" "checksum cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)" = "a3a6805df695087e7c1bcd9a82e03ad6fb864c8e67ac41b1348229ce5b7f0407" +"checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97" +"checksum curl 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c90e1240ef340dd4027ade439e5c7c2064dd9dc652682117bd50d1486a3add7b" +"checksum curl-sys 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)" = "c0d909dc402ae80b6f7b0118c039203436061b9d9a3ca5d2c2546d93e0a61aaa" +"checksum docopt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab32ea6e284d87987066f21a9e809a73c14720571ef34516f0890b3d355ccfd8" "checksum dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "80c8b71fd71146990a9742fc06dcbbde19161a267e0ad4e572c35162f4578c90" "checksum env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f" "checksum env_logger 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "99971fb1b635fe7a0ee3c4d065845bb93cca80a23b5613b5613391ece5de4144" "checksum filetime 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "5363ab8e4139b8568a6237db5248646e5a8a2f89bd5ccb02092182b11fd3e922" +"checksum flate2 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)" = "d4e4d0c15ef829cbc1b7cda651746be19cceeb238be7b1049227b14891df9e25" +"checksum foreign-types 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e4056b9bd47f8ac5ba12be771f77a0dae796d1bbaaf5fd0b9c2d38b69b8a29d" +"checksum fs2 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "34edaee07555859dc13ca387e6ae05686bb4d0364c95d649b6dab959511f4baf" "checksum gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)" = "c07c758b972368e703a562686adb39125707cc1ef3399da8c019fc6c2498a75d" +"checksum gdi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0912515a8ff24ba900422ecda800b52f4016a56251922d397c576bf92c690518" "checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685" -"checksum handlebars 0.25.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b930077f1422bf853008047b55896efc1409744bfc9903f1eec1a58fcc7edeff" +"checksum git2 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "046ae03385257040b2a35e56d9669d950dd911ba2bf48202fbef73ee6aab27b2" +"checksum git2-curl 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "68676bc784bf0bef83278898929bf64a251e87c0340723d0b93fa096c9c5bf8e" +"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +"checksum hamcrest 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bf088f042a467089e9baa4972f57f9247e42a0cc549ba264c7a04fbb8ecb89d4" +"checksum handlebars 0.25.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b2249f6f0dc5a3bb2b3b1a8f797dfccbc4b053344d773d654ad565e51427d335" +"checksum idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1053236e00ce4f668aeca4a769a09b3bf5a682d802abd6f3cb39374f6b162c11" "checksum itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eb2f404fbc66fd9aac13e998248505e7ecb2ad8e44ab6388684c5fb11c6c251c" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6abe0ee2e758cd6bc8a2cd56726359007748fbf4128da998b65d0b70f881e19b" "checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5" +"checksum libgit2-sys 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "d951fd5eccae07c74e8c2c1075b05ea1e43be7f8952245af8c2840d1480b1d95" +"checksum libssh2-sys 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "91e135645c2e198a39552c8c7686bb5b83b1b99f64831c040a6c2798a1195934" +"checksum libz-sys 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "e5ee912a45d686d393d5ac87fac15ba0ba18daae14e8e7543c63ebf7fb7e970c" "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" "checksum mdbook 0.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "dbba458ca886cb082d026afd704eeeeb0531f7e4ffd6c619f72dc309c1c18fe4" +"checksum matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efd7622e3022e1a6eaa602c4cea8912254e5582c9c692e9167714182244801b1" +"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" "checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4" +"checksum miniz-sys 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "28eaee17666671fa872e567547e8428e83308ebe5808cdf6a0e28397dbe2c726" +"checksum miow 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3a78d2605eb97302c10cf944b8d96b0a2a890c52957caf92fcd1f24f69049579" +"checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2" +"checksum num 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "bde7c03b09e7c6a301ee81f6ddf66d7a28ec305699e3d3b056d2fc56470e3120" +"checksum num-bigint 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "88b14378471f7c2adc5262f05b4701ef53e8da376453a8d8fee48e51db745e49" +"checksum num-complex 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "f0c78e054dd19c3fd03419ade63fa661e9c49bb890ce3beb4eee5b7baf93f92f" +"checksum num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "fb24d9bfb3f222010df27995441ded1e954f8f69cd35021f6bef02ca9552fb92" +"checksum num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "287a1c9969a847055e1122ec0ea7a5c5d6f72aad97934e131c83d5c08ab4e45c" +"checksum num-rational 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "54ff603b8334a72fbb27fe66948aac0abaaa40231b3cecd189e76162f6f38aaf" "checksum num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "a16a42856a256b39c6d3484f097f6713e14feacd9bfb02290917904fae46c81c" "checksum num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "cee7e88156f3f9e19bdd598f8d6c9db7bf4078f99f8381f43a55b09648d1a6e3" +"checksum num_cpus 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a225d1e2717567599c24f88e49f00856c6e825a12125181ee42c4257e3688d39" "checksum open 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3478ed1686bd1300c8a981a940abc92b06fac9cbef747f4c668d4e032ff7b842" +"checksum openssl 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f9871ecf7629da3760599e3e547d35940cff3cead49159b49f81cd1250f24f1d" +"checksum openssl-probe 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "756d49c8424483a3df3b5d735112b4da22109ced9a8294f1f5cdf80fb3810919" +"checksum openssl-sys 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5dd48381e9e8a6dce9c4c402db143b2e243f5f872354532f7a009c289b3998ca" "checksum pest 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0a6dda33d67c26f0aac90d324ab2eb7239c819fc7b2552fe9faa4fe88441edc8" +"checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" +"checksum psapi-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "abcd5d1a07d360e29727f757a9decb3ce8bc6e0efa8969cfaad669a8317a2478" "checksum pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1058d7bb927ca067656537eec4e02c2b4b70eaaa129664c5b90c111e20326f41" "checksum quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0aad603e8d7fb67da22dbdf1f4b826ce8829e406124109e73cf1b2454b93a71c" +"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" +"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" "checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01" +"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" "checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457" "checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b" +"checksum semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a3186ec9e65071a2095434b1f5bb24838d4e8e130f584c790f6033c79943537" +"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1e0ed773960f90a78567fcfbe935284adf50c5d7cf119aa2cf43bb0b4afa69bb" -"checksum serde_json 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e095e4e94e7382b76f48e93bd845ffddda62df8dfd4c163b1bfa93d40e22e13a" +"checksum serde_json 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "2eb96d30e4e6f9fc52e08f51176d078b6f79b981dc3ed4134f7b850be9f446a8" +"checksum shell-escape 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "dd5cc96481d54583947bfe88bf30c23d53f883c6cd0145368b69989d97b84ef8" "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" -"checksum term_size 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "71662702fe5cd2cf95edd4ad655eea42f24a87a0e44059cbaa4e55260b7bc331" +"checksum tar 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "1eb3bf6ec92843ca93f4fcfb5fc6dfe30534815b147885db4b5759b8e2ff7d52" +"checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" +"checksum term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d168af3930b369cfe245132550579d47dfd873d69470755a19c2c6568dbbd989" +"checksum term_size 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "07b6c1ac5b3fffd75073276bca1ceed01f67a28537097a2a9539e116e50fb21a" +"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" "checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a" -"checksum thread_local 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7793b722f0f77ce716e7f1acf416359ca32ff24d04ffbac4269f44a4a83be05d" +"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" +"checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7" "checksum toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "0590d72182e50e879c4da3b11c6488dae18fccb1ae0c7a3eda18e16795844796" +"checksum toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "736b60249cb25337bc196faa43ee12c705e426f3d55c214d73a4e7be06f92cb4" "checksum toml 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "08272367dd2e766db3fa38f068067d17aa6a9dfd7259af24b3927db92f1e0c2f" +"checksum unicode-bidi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a078ebdd62c0e71a709c3d53d2af693fe09fe93fbff8344aebe289b78f9032" +"checksum unicode-normalization 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e28fa37426fceeb5cf8f41ee273faa7c82c47dc8fba5853402841e665fcd86ff" "checksum unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18127285758f0e2c6cf325bb3f3d138a12fee27de4f23e146cd6a179f26c2cf3" "checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" "checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" +"checksum url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f5ba8a749fb4479b043733416c244fa9d1d3af3d7c23804944651c8a448cb87e" +"checksum user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ef4711d107b21b410a3a974b1204d9accc8b10dad75d8324b5d755de1617d47" +"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" "checksum vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cac5efe5cb0fa14ec2f84f83c701c562ee63f6dcc680861b21d65c682adfb05f" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" +"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" diff --git a/src/Cargo.toml b/src/Cargo.toml index 0dafbb8428e3e..c5ca80accbf02 100644 --- a/src/Cargo.toml +++ b/src/Cargo.toml @@ -13,6 +13,7 @@ members = [ "tools/build-manifest", "tools/qemu-test-client", "tools/qemu-test-server", + "tools/cargo", ] # Curiously, compiletest will segfault if compiled with opt-level=3 on 64-bit diff --git a/src/tools/cargo b/src/tools/cargo new file mode 160000 index 0000000000000..6ed5a43bb566e --- /dev/null +++ b/src/tools/cargo @@ -0,0 +1 @@ +Subproject commit 6ed5a43bb566e0ee3fe7981de5aa5a36e2905ebd diff --git a/src/tools/cargotest/Cargo.toml b/src/tools/cargotest/Cargo.toml index 8108c7f762eeb..f4163a6f1170a 100644 --- a/src/tools/cargotest/Cargo.toml +++ b/src/tools/cargotest/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "cargotest" +name = "cargotest2" version = "0.1.0" authors = ["Brian Anderson "] From accc7f4c6a8af93ce93f7f15d0ce700d8952d50a Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Thu, 2 Mar 2017 11:28:09 +0100 Subject: [PATCH 06/29] LLVM: Update submodule to include x86-interrupt ABI patches --- src/llvm | 2 +- src/rustllvm/llvm-auto-clean-trigger | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/llvm b/src/llvm index ceb177eeefa7d..50ab09fb43f03 160000 --- a/src/llvm +++ b/src/llvm @@ -1 +1 @@ -Subproject commit ceb177eeefa7d67ca29230d2e7e8584f97d4fdad +Subproject commit 50ab09fb43f038e4f824eea6cb278f560d3e8621 diff --git a/src/rustllvm/llvm-auto-clean-trigger b/src/rustllvm/llvm-auto-clean-trigger index ab36e9a2c2b20..57f37ea050c1b 100644 --- a/src/rustllvm/llvm-auto-clean-trigger +++ b/src/rustllvm/llvm-auto-clean-trigger @@ -1,4 +1,4 @@ # If this file is modified, then llvm will be forcibly cleaned and then rebuilt. # The actual contents of this file do not matter, but to trigger a change on the # build bots then the contents should be changed so git updates the mtime. -2017-02-15 +2017-03-02 From 0143774cb5c21b18bf599b1726e122785c0de452 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sat, 18 Feb 2017 06:18:29 +0000 Subject: [PATCH 07/29] Remove lifetime parameter from `syntax::tokenstream::Cursor`. --- src/libproc_macro_plugin/qquote.rs | 12 +++---- src/libsyntax/ext/expand.rs | 2 +- src/libsyntax/parse/mod.rs | 2 +- src/libsyntax/tokenstream.rs | 58 ++++++++++++++++-------------- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/libproc_macro_plugin/qquote.rs b/src/libproc_macro_plugin/qquote.rs index dc7c96a4e2767..b9bf35cff07b4 100644 --- a/src/libproc_macro_plugin/qquote.rs +++ b/src/libproc_macro_plugin/qquote.rs @@ -51,7 +51,7 @@ macro_rules! quote_tree { fn delimit(delim: token::DelimToken, stream: TokenStream) -> TokenStream { TokenTree::Delimited(DUMMY_SP, Rc::new(Delimited { delim: delim, - tts: stream.trees().cloned().collect(), + tts: stream.into_trees().collect(), })).into() } @@ -75,21 +75,21 @@ impl Quote for TokenStream { return quote!(::syntax::tokenstream::TokenStream::empty()); } - struct Quote<'a>(tokenstream::Cursor<'a>); + struct Quote(tokenstream::Cursor); - impl<'a> Iterator for Quote<'a> { + impl Iterator for Quote { type Item = TokenStream; fn next(&mut self) -> Option { let is_unquote = match self.0.peek() { - Some(&TokenTree::Token(_, Token::Ident(ident))) if ident.name == "unquote" => { + Some(TokenTree::Token(_, Token::Ident(ident))) if ident.name == "unquote" => { self.0.next(); true } _ => false, }; - self.0.next().cloned().map(|tree| { + self.0.next().map(|tree| { let quoted_tree = if is_unquote { tree.into() } else { tree.quote() }; quote!(::syntax::tokenstream::TokenStream::from((unquote quoted_tree)),) }) @@ -104,7 +104,7 @@ impl Quote for TokenStream { impl Quote for Vec { fn quote(&self) -> TokenStream { let stream = self.iter().cloned().collect::(); - quote!((quote stream).trees().cloned().collect::<::std::vec::Vec<_> >()) + quote!((quote stream).into_trees().collect::<::std::vec::Vec<_> >()) } } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 38494378f72ad..8107696b8b920 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -647,7 +647,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { fn parse_expansion(&mut self, toks: TokenStream, kind: ExpansionKind, name: Name, span: Span) -> Expansion { - let mut parser = self.cx.new_parser_from_tts(&toks.trees().cloned().collect::>()); + let mut parser = self.cx.new_parser_from_tts(&toks.into_trees().collect::>()); let expansion = match parser.parse_expansion(kind, false) { Ok(expansion) => expansion, Err(mut err) => { diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 6fec49b229abe..f783e32d62104 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -192,7 +192,7 @@ pub fn new_parser_from_tts<'a>(sess: &'a ParseSess, tts: Vec(sess: &'a ParseSess, ts: tokenstream::TokenStream) -> Parser<'a> { - tts_to_parser(sess, ts.trees().cloned().collect()) + tts_to_parser(sess, ts.into_trees().collect()) } diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 6665404672133..0f973540edb5a 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -35,7 +35,7 @@ use serialize::{Decoder, Decodable, Encoder, Encodable}; use symbol::Symbol; use util::RcSlice; -use std::{fmt, iter}; +use std::{fmt, iter, mem}; use std::rc::Rc; /// A delimited sequence of token trees @@ -338,14 +338,18 @@ impl TokenStream { TokenStream { kind: TokenStreamKind::Stream(RcSlice::new(vec)) } } - pub fn trees<'a>(&'a self) -> Cursor { + pub fn trees(&self) -> Cursor { + self.clone().into_trees() + } + + pub fn into_trees(self) -> Cursor { Cursor::new(self) } /// Compares two TokenStreams, checking equality without regarding span information. pub fn eq_unspanned(&self, other: &TokenStream) -> bool { for (t1, t2) in self.trees().zip(other.trees()) { - if !t1.eq_unspanned(t2) { + if !t1.eq_unspanned(&t2) { return false; } } @@ -353,58 +357,60 @@ impl TokenStream { } } -pub struct Cursor<'a> { - current_frame: CursorFrame<'a>, - stack: Vec>, +pub struct Cursor { + current_frame: CursorFrame, + stack: Vec, } -impl<'a> Iterator for Cursor<'a> { - type Item = &'a TokenTree; +impl Iterator for Cursor { + type Item = TokenTree; - fn next(&mut self) -> Option<&'a TokenTree> { + fn next(&mut self) -> Option { let tree = self.peek(); self.current_frame = self.stack.pop().unwrap_or(CursorFrame::Empty); tree } } -enum CursorFrame<'a> { +enum CursorFrame { Empty, - Tree(&'a TokenTree), - Stream(&'a RcSlice, usize), + Tree(TokenTree), + Stream(RcSlice, usize), } -impl<'a> CursorFrame<'a> { - fn new(stream: &'a TokenStream) -> Self { +impl CursorFrame { + fn new(stream: TokenStream) -> Self { match stream.kind { TokenStreamKind::Empty => CursorFrame::Empty, - TokenStreamKind::Tree(ref tree) => CursorFrame::Tree(tree), - TokenStreamKind::Stream(ref stream) => CursorFrame::Stream(stream, 0), + TokenStreamKind::Tree(tree) => CursorFrame::Tree(tree), + TokenStreamKind::Stream(stream) => CursorFrame::Stream(stream, 0), } } } -impl<'a> Cursor<'a> { - fn new(stream: &'a TokenStream) -> Self { +impl Cursor { + fn new(stream: TokenStream) -> Self { Cursor { current_frame: CursorFrame::new(stream), stack: Vec::new(), } } - pub fn peek(&mut self) -> Option<&'a TokenTree> { - while let CursorFrame::Stream(stream, index) = self.current_frame { + pub fn peek(&mut self) -> Option { + while let CursorFrame::Stream(stream, index) = + mem::replace(&mut self.current_frame, CursorFrame::Empty) { self.current_frame = if index == stream.len() { self.stack.pop().unwrap_or(CursorFrame::Empty) } else { + let frame = CursorFrame::new(stream[index].clone()); self.stack.push(CursorFrame::Stream(stream, index + 1)); - CursorFrame::new(&stream[index]) + frame }; } match self.current_frame { CursorFrame::Empty => None, - CursorFrame::Tree(tree) => Some(tree), + CursorFrame::Tree(ref tree) => Some(tree.clone()), CursorFrame::Stream(..) => unreachable!(), } } @@ -412,13 +418,13 @@ impl<'a> Cursor<'a> { impl fmt::Display for TokenStream { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(&pprust::tts_to_string(&self.trees().cloned().collect::>())) + f.write_str(&pprust::tts_to_string(&self.trees().collect::>())) } } impl Encodable for TokenStream { fn encode(&self, encoder: &mut E) -> Result<(), E::Error> { - self.trees().cloned().collect::>().encode(encoder) + self.trees().collect::>().encode(encoder) } } @@ -464,14 +470,14 @@ mod tests { fn test_from_to_bijection() { let test_start = string_to_tts("foo::bar(baz)".to_string()); let ts = test_start.iter().cloned().collect::(); - let test_end: Vec = ts.trees().cloned().collect(); + let test_end: Vec = ts.trees().collect(); assert_eq!(test_start, test_end) } #[test] fn test_to_from_bijection() { let test_start = string_to_ts("foo::bar(baz)"); - let test_end = test_start.trees().cloned().collect(); + let test_end = test_start.trees().collect(); assert_eq!(test_start, test_end) } From 8dca72be9be71fbccae9370b45af8efb946dedc1 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sat, 18 Feb 2017 12:45:32 +0000 Subject: [PATCH 08/29] Optimize `syntax::tokenstream::Cursor`. --- src/libproc_macro_plugin/qquote.rs | 7 +- src/libsyntax/tokenstream.rs | 116 +++++++++++++++-------------- 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/src/libproc_macro_plugin/qquote.rs b/src/libproc_macro_plugin/qquote.rs index b9bf35cff07b4..e3d85bca3e0d2 100644 --- a/src/libproc_macro_plugin/qquote.rs +++ b/src/libproc_macro_plugin/qquote.rs @@ -17,6 +17,7 @@ use syntax::symbol::Symbol; use syntax::tokenstream::{self, Delimited, TokenTree, TokenStream}; use syntax_pos::DUMMY_SP; +use std::iter; use std::rc::Rc; pub fn qquote<'cx>(stream: TokenStream) -> TokenStream { @@ -75,14 +76,14 @@ impl Quote for TokenStream { return quote!(::syntax::tokenstream::TokenStream::empty()); } - struct Quote(tokenstream::Cursor); + struct Quote(iter::Peekable); impl Iterator for Quote { type Item = TokenStream; fn next(&mut self) -> Option { let is_unquote = match self.0.peek() { - Some(TokenTree::Token(_, Token::Ident(ident))) if ident.name == "unquote" => { + Some(&TokenTree::Token(_, Token::Ident(ident))) if ident.name == "unquote" => { self.0.next(); true } @@ -96,7 +97,7 @@ impl Quote for TokenStream { } } - let quoted = Quote(self.trees()).collect::(); + let quoted = Quote(self.trees().peekable()).collect::(); quote!([(unquote quoted)].iter().cloned().collect::<::syntax::tokenstream::TokenStream>()) } } diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 0f973540edb5a..552395945a11a 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -299,7 +299,7 @@ impl From for TokenStream { impl> iter::FromIterator for TokenStream { fn from_iter>(iter: I) -> Self { - TokenStream::concat(iter.into_iter().map(Into::into)) + TokenStream::concat(iter.into_iter().map(Into::into).collect::>()) } } @@ -323,19 +323,16 @@ impl TokenStream { } } - pub fn concat>(streams: I) -> TokenStream { - let mut streams = streams.into_iter().filter(|stream| !stream.is_empty()); - let first_stream = match streams.next() { - Some(stream) => stream, - None => return TokenStream::empty(), - }; - let second_stream = match streams.next() { - Some(stream) => stream, - None => return first_stream, - }; - let mut vec = vec![first_stream, second_stream]; - vec.extend(streams); - TokenStream { kind: TokenStreamKind::Stream(RcSlice::new(vec)) } + pub fn concat(mut streams: Vec) -> TokenStream { + match streams.len() { + 0 => TokenStream::empty(), + 1 => TokenStream::from(streams.pop().unwrap()), + _ => TokenStream::concat_rc_slice(RcSlice::new(streams)), + } + } + + fn concat_rc_slice(streams: RcSlice) -> TokenStream { + TokenStream { kind: TokenStreamKind::Stream(streams) } } pub fn trees(&self) -> Cursor { @@ -357,62 +354,67 @@ impl TokenStream { } } -pub struct Cursor { - current_frame: CursorFrame, - stack: Vec, +pub struct Cursor(CursorKind); + +enum CursorKind { + Empty, + Tree(TokenTree, bool /* consumed? */), + Stream(StreamCursor), +} + +struct StreamCursor { + stream: RcSlice, + index: usize, + stack: Vec<(RcSlice, usize)>, } impl Iterator for Cursor { type Item = TokenTree; fn next(&mut self) -> Option { - let tree = self.peek(); - self.current_frame = self.stack.pop().unwrap_or(CursorFrame::Empty); - tree - } -} - -enum CursorFrame { - Empty, - Tree(TokenTree), - Stream(RcSlice, usize), -} + let cursor = match self.0 { + CursorKind::Stream(ref mut cursor) => cursor, + CursorKind::Tree(ref tree, ref mut consumed @ false) => { + *consumed = true; + return Some(tree.clone()); + } + _ => return None, + }; -impl CursorFrame { - fn new(stream: TokenStream) -> Self { - match stream.kind { - TokenStreamKind::Empty => CursorFrame::Empty, - TokenStreamKind::Tree(tree) => CursorFrame::Tree(tree), - TokenStreamKind::Stream(stream) => CursorFrame::Stream(stream, 0), + loop { + if cursor.index < cursor.stream.len() { + match cursor.stream[cursor.index].kind.clone() { + TokenStreamKind::Tree(tree) => { + cursor.index += 1; + return Some(tree); + } + TokenStreamKind::Stream(stream) => { + cursor.stack.push((mem::replace(&mut cursor.stream, stream), + mem::replace(&mut cursor.index, 0) + 1)); + } + TokenStreamKind::Empty => { + cursor.index += 1; + } + } + } else if let Some((stream, index)) = cursor.stack.pop() { + cursor.stream = stream; + cursor.index = index; + } else { + return None; + } } } } impl Cursor { fn new(stream: TokenStream) -> Self { - Cursor { - current_frame: CursorFrame::new(stream), - stack: Vec::new(), - } - } - - pub fn peek(&mut self) -> Option { - while let CursorFrame::Stream(stream, index) = - mem::replace(&mut self.current_frame, CursorFrame::Empty) { - self.current_frame = if index == stream.len() { - self.stack.pop().unwrap_or(CursorFrame::Empty) - } else { - let frame = CursorFrame::new(stream[index].clone()); - self.stack.push(CursorFrame::Stream(stream, index + 1)); - frame - }; - } - - match self.current_frame { - CursorFrame::Empty => None, - CursorFrame::Tree(ref tree) => Some(tree.clone()), - CursorFrame::Stream(..) => unreachable!(), - } + Cursor(match stream.kind { + TokenStreamKind::Empty => CursorKind::Empty, + TokenStreamKind::Tree(tree) => CursorKind::Tree(tree, false), + TokenStreamKind::Stream(stream) => { + CursorKind::Stream(StreamCursor { stream: stream, index: 0, stack: Vec::new() }) + } + }) } } From 8cd0c0885f841c9bfd0c330e3da21363427010e4 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Mon, 20 Feb 2017 05:44:06 +0000 Subject: [PATCH 09/29] Introduce `syntax::parse::parser::TokenCursor`. --- src/libsyntax/parse/parser.rs | 174 +++++++++++++++++++++++++--------- src/libsyntax/tokenstream.rs | 114 +++++++++------------- 2 files changed, 173 insertions(+), 115 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 71274c4fdaa4e..b12b0c0326701 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -9,7 +9,7 @@ // except according to those terms. use abi::{self, Abi}; -use ast::BareFnTy; +use ast::{AttrStyle, BareFnTy}; use ast::{RegionTyParamBound, TraitTyParamBound, TraitBoundModifier}; use ast::Unsafety; use ast::{Mod, Arg, Arm, Attribute, BindingMode, TraitItemKind}; @@ -46,21 +46,21 @@ use errors::{self, DiagnosticBuilder}; use parse::{self, classify, token}; use parse::common::SeqSep; use parse::lexer::TokenAndSpan; +use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; use parse::obsolete::ObsoleteSyntax; use parse::{new_sub_parser_from_file, ParseSess, Directory, DirectoryOwnership}; use util::parser::{AssocOp, Fixity}; use print::pprust; use ptr::P; use parse::PResult; -use tokenstream::{Delimited, TokenTree}; +use tokenstream::{self, Delimited, TokenTree, TokenStream}; use symbol::{Symbol, keywords}; use util::ThinVec; use std::collections::HashSet; -use std::mem; +use std::{cmp, mem, slice}; use std::path::{Path, PathBuf}; use std::rc::Rc; -use std::slice; bitflags! { flags Restrictions: u8 { @@ -175,12 +175,108 @@ pub struct Parser<'a> { /// into modules, and sub-parsers have new values for this name. pub root_module_name: Option, pub expected_tokens: Vec, - pub tts: Vec<(TokenTree, usize)>, + token_cursor: TokenCursor, pub desugar_doc_comments: bool, /// Whether we should configure out of line modules as we parse. pub cfg_mods: bool, } +struct TokenCursor { + frame: TokenCursorFrame, + stack: Vec, +} + +struct TokenCursorFrame { + delim: token::DelimToken, + span: Span, + open_delim: bool, + tree_cursor: tokenstream::Cursor, + close_delim: bool, +} + +impl TokenCursorFrame { + fn new(sp: Span, delimited: &Delimited) -> Self { + TokenCursorFrame { + delim: delimited.delim, + span: sp, + open_delim: delimited.delim == token::NoDelim, + tree_cursor: delimited.tts.iter().cloned().collect::().into_trees(), + close_delim: delimited.delim == token::NoDelim, + } + } +} + +impl TokenCursor { + fn next(&mut self) -> TokenAndSpan { + loop { + let tree = if !self.frame.open_delim { + self.frame.open_delim = true; + Delimited { delim: self.frame.delim, tts: Vec::new() }.open_tt(self.frame.span) + } else if let Some(tree) = self.frame.tree_cursor.next() { + tree + } else if !self.frame.close_delim { + self.frame.close_delim = true; + Delimited { delim: self.frame.delim, tts: Vec::new() }.close_tt(self.frame.span) + } else if let Some(frame) = self.stack.pop() { + self.frame = frame; + continue + } else { + return TokenAndSpan { tok: token::Eof, sp: self.frame.span } + }; + + match tree { + TokenTree::Token(sp, tok) => return TokenAndSpan { tok: tok, sp: sp }, + TokenTree::Delimited(sp, ref delimited) => { + let frame = TokenCursorFrame::new(sp, delimited); + self.stack.push(mem::replace(&mut self.frame, frame)); + } + } + } + } + + fn next_desugared(&mut self) -> TokenAndSpan { + let (sp, name) = match self.next() { + TokenAndSpan { sp, tok: token::DocComment(name) } => (sp, name), + tok @ _ => return tok, + }; + + let stripped = strip_doc_comment_decoration(&name.as_str()); + + // Searches for the occurrences of `"#*` and returns the minimum number of `#`s + // required to wrap the text. + let mut num_of_hashes = 0; + let mut count = 0; + for ch in stripped.chars() { + count = match ch { + '"' => 1, + '#' if count > 0 => count + 1, + _ => 0, + }; + num_of_hashes = cmp::max(num_of_hashes, count); + } + + let body = TokenTree::Delimited(sp, Rc::new(Delimited { + delim: token::Bracket, + tts: vec![TokenTree::Token(sp, token::Ident(ast::Ident::from_str("doc"))), + TokenTree::Token(sp, token::Eq), + TokenTree::Token(sp, token::Literal( + token::StrRaw(Symbol::intern(&stripped), num_of_hashes), None))], + })); + + self.stack.push(mem::replace(&mut self.frame, TokenCursorFrame::new(sp, &Delimited { + delim: token::NoDelim, + tts: if doc_comment_style(&name.as_str()) == AttrStyle::Inner { + [TokenTree::Token(sp, token::Pound), TokenTree::Token(sp, token::Not), body] + .iter().cloned().collect() + } else { + [TokenTree::Token(sp, token::Pound), body].iter().cloned().collect() + }, + }))); + + self.next() + } +} + #[derive(PartialEq, Eq, Clone)] pub enum TokenType { Token(token::Token), @@ -313,10 +409,6 @@ impl<'a> Parser<'a> { directory: Option, desugar_doc_comments: bool) -> Self { - let tt = TokenTree::Delimited(syntax_pos::DUMMY_SP, Rc::new(Delimited { - delim: token::NoDelim, - tts: tokens, - })); let mut parser = Parser { sess: sess, token: token::Underscore, @@ -328,7 +420,13 @@ impl<'a> Parser<'a> { directory: Directory { path: PathBuf::new(), ownership: DirectoryOwnership::Owned }, root_module_name: None, expected_tokens: Vec::new(), - tts: if tt.len() > 0 { vec![(tt, 0)] } else { Vec::new() }, + token_cursor: TokenCursor { + frame: TokenCursorFrame::new(syntax_pos::DUMMY_SP, &Delimited { + delim: token::NoDelim, + tts: tokens, + }), + stack: Vec::new(), + }, desugar_doc_comments: desugar_doc_comments, cfg_mods: true, }; @@ -346,28 +444,9 @@ impl<'a> Parser<'a> { } fn next_tok(&mut self) -> TokenAndSpan { - loop { - let tok = if let Some((tts, i)) = self.tts.pop() { - let tt = tts.get_tt(i); - if i + 1 < tts.len() { - self.tts.push((tts, i + 1)); - } - if let TokenTree::Token(sp, tok) = tt { - TokenAndSpan { tok: tok, sp: sp } - } else { - self.tts.push((tt, 0)); - continue - } - } else { - TokenAndSpan { tok: token::Eof, sp: self.span } - }; - - match tok.tok { - token::DocComment(name) if self.desugar_doc_comments => { - self.tts.push((TokenTree::Token(tok.sp, token::DocComment(name)), 0)); - } - _ => return tok, - } + match self.desugar_doc_comments { + true => self.token_cursor.next_desugared(), + false => self.token_cursor.next(), } } @@ -972,19 +1051,16 @@ impl<'a> Parser<'a> { F: FnOnce(&token::Token) -> R, { if dist == 0 { - return f(&self.token); - } - let mut tok = token::Eof; - if let Some(&(ref tts, mut i)) = self.tts.last() { - i += dist - 1; - if i < tts.len() { - tok = match tts.get_tt(i) { - TokenTree::Token(_, tok) => tok, - TokenTree::Delimited(_, delimited) => token::OpenDelim(delimited.delim), - }; - } + return f(&self.token) } - f(&tok) + + f(&match self.token_cursor.frame.tree_cursor.look_ahead(dist - 1) { + Some(tree) => match tree { + TokenTree::Token(_, tok) => tok, + TokenTree::Delimited(_, delimited) => token::OpenDelim(delimited.delim), + }, + None => token::CloseDelim(self.token_cursor.frame.delim), + }) } pub fn fatal(&self, m: &str) -> DiagnosticBuilder<'a> { self.sess.span_diagnostic.struct_span_fatal(self.span, m) @@ -2569,10 +2645,14 @@ impl<'a> Parser<'a> { pub fn parse_token_tree(&mut self) -> PResult<'a, TokenTree> { match self.token { token::OpenDelim(..) => { - let tt = self.tts.pop().unwrap().0; - self.span = tt.span(); + let frame = mem::replace(&mut self.token_cursor.frame, + self.token_cursor.stack.pop().unwrap()); + self.span = frame.span; self.bump(); - return Ok(tt); + return Ok(TokenTree::Delimited(frame.span, Rc::new(Delimited { + delim: frame.delim, + tts: frame.tree_cursor.original_stream().trees().collect(), + }))); }, token::CloseDelim(_) | token::Eof => unreachable!(), _ => Ok(TokenTree::Token(self.span, self.bump_and_get())), diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 552395945a11a..083435a043362 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -22,12 +22,11 @@ //! and a borrowed TokenStream is sufficient to build an owned TokenStream without taking //! ownership of the original. -use ast::{self, AttrStyle, LitKind}; +use ast::{self, LitKind}; use syntax_pos::{BytePos, Span, DUMMY_SP}; use codemap::Spanned; use ext::base; use ext::tt::{macro_parser, quoted}; -use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; use parse::{self, Directory}; use parse::token::{self, Token, Lit}; use print::pprust; @@ -103,72 +102,6 @@ pub enum TokenTree { } impl TokenTree { - pub fn len(&self) -> usize { - match *self { - TokenTree::Token(_, token::DocComment(name)) => { - match doc_comment_style(&name.as_str()) { - AttrStyle::Outer => 2, - AttrStyle::Inner => 3, - } - } - TokenTree::Delimited(_, ref delimed) => match delimed.delim { - token::NoDelim => delimed.tts.len(), - _ => delimed.tts.len() + 2, - }, - TokenTree::Token(..) => 0, - } - } - - pub fn get_tt(&self, index: usize) -> TokenTree { - match (self, index) { - (&TokenTree::Token(sp, token::DocComment(_)), 0) => TokenTree::Token(sp, token::Pound), - (&TokenTree::Token(sp, token::DocComment(name)), 1) - if doc_comment_style(&name.as_str()) == AttrStyle::Inner => { - TokenTree::Token(sp, token::Not) - } - (&TokenTree::Token(sp, token::DocComment(name)), _) => { - let stripped = strip_doc_comment_decoration(&name.as_str()); - - // Searches for the occurrences of `"#*` and returns the minimum number of `#`s - // required to wrap the text. - let num_of_hashes = stripped.chars() - .scan(0, |cnt, x| { - *cnt = if x == '"' { - 1 - } else if *cnt != 0 && x == '#' { - *cnt + 1 - } else { - 0 - }; - Some(*cnt) - }) - .max() - .unwrap_or(0); - - TokenTree::Delimited(sp, Rc::new(Delimited { - delim: token::Bracket, - tts: vec![TokenTree::Token(sp, token::Ident(ast::Ident::from_str("doc"))), - TokenTree::Token(sp, token::Eq), - TokenTree::Token(sp, token::Literal( - token::StrRaw(Symbol::intern(&stripped), num_of_hashes), None))], - })) - } - (&TokenTree::Delimited(_, ref delimed), _) if delimed.delim == token::NoDelim => { - delimed.tts[index].clone() - } - (&TokenTree::Delimited(span, ref delimed), _) => { - if index == 0 { - return delimed.open_tt(span); - } - if index == delimed.tts.len() + 1 { - return delimed.close_tt(span); - } - delimed.tts[index - 1].clone() - } - _ => panic!("Cannot expand a token tree"), - } - } - /// Use this token tree as a matcher to parse given tts. pub fn parse(cx: &base::ExtCtxt, mtch: &[quoted::TokenTree], tts: &[TokenTree]) -> macro_parser::NamedParseResult { @@ -416,6 +349,51 @@ impl Cursor { } }) } + + pub fn original_stream(self) -> TokenStream { + match self.0 { + CursorKind::Empty => TokenStream::empty(), + CursorKind::Tree(tree, _) => tree.into(), + CursorKind::Stream(cursor) => TokenStream::concat_rc_slice({ + cursor.stack.get(0).cloned().map(|(stream, _)| stream).unwrap_or(cursor.stream) + }), + } + } + + pub fn look_ahead(&self, n: usize) -> Option { + fn look_ahead(streams: &[TokenStream], mut n: usize) -> Result { + for stream in streams { + n = match stream.kind { + TokenStreamKind::Tree(ref tree) if n == 0 => return Ok(tree.clone()), + TokenStreamKind::Tree(..) => n - 1, + TokenStreamKind::Stream(ref stream) => match look_ahead(stream, n) { + Ok(tree) => return Ok(tree), + Err(n) => n, + }, + _ => n, + }; + } + + Err(n) + } + + match self.0 { + CursorKind::Empty | CursorKind::Tree(_, true) => Err(n), + CursorKind::Tree(ref tree, false) => look_ahead(&[tree.clone().into()], n), + CursorKind::Stream(ref cursor) => { + look_ahead(&cursor.stream[cursor.index ..], n).or_else(|mut n| { + for &(ref stream, index) in cursor.stack.iter().rev() { + n = match look_ahead(&stream[index..], n) { + Ok(tree) => return Ok(tree), + Err(n) => n, + } + } + + Err(n) + }) + } + }.ok() + } } impl fmt::Display for TokenStream { From f6eaaf350ea683ae8b33b4a79422ad1a10ea0987 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Tue, 21 Feb 2017 05:05:59 +0000 Subject: [PATCH 10/29] Integrate `TokenStream`. --- src/libproc_macro/lib.rs | 7 +- src/libproc_macro_plugin/qquote.rs | 21 +-- src/librustc/hir/mod.rs | 4 +- .../calculate_svh/svh_visitor.rs | 16 +- src/librustc_metadata/cstore_impl.rs | 6 +- src/librustc_metadata/encoder.rs | 3 +- src/librustc_resolve/build_reduced_graph.rs | 2 +- src/librustc_resolve/macros.rs | 4 +- src/librustc_save_analysis/span_utils.rs | 2 +- src/librustdoc/visit_ast.rs | 6 +- src/libsyntax/ast.rs | 18 ++- src/libsyntax/ext/base.rs | 20 +-- src/libsyntax/ext/expand.rs | 39 +++-- src/libsyntax/ext/placeholders.rs | 3 +- src/libsyntax/ext/quote.rs | 39 ++--- src/libsyntax/ext/tt/macro_parser.rs | 32 ++-- src/libsyntax/ext/tt/macro_rules.rs | 21 ++- src/libsyntax/ext/tt/quoted.rs | 10 +- src/libsyntax/ext/tt/transcribe.rs | 36 +++-- src/libsyntax/fold.rs | 32 ++-- src/libsyntax/parse/lexer/tokentrees.rs | 24 ++- src/libsyntax/parse/mod.rs | 29 ++-- src/libsyntax/parse/parser.rs | 42 ++--- src/libsyntax/print/pprust.rs | 24 +-- src/libsyntax/tokenstream.rs | 146 ++++++++---------- src/libsyntax/util/rc_slice.rs | 7 - src/libsyntax_ext/asm.rs | 2 +- 27 files changed, 276 insertions(+), 319 deletions(-) diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 0516e111be3b3..8d7fe655c23b2 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -101,7 +101,7 @@ pub mod __internal { pub fn token_stream_parse_items(stream: TokenStream) -> Result>, LexError> { with_parse_sess(move |sess| { - let mut parser = parse::new_parser_from_ts(sess, stream.inner); + let mut parser = parse::stream_to_parser(sess, stream.inner); let mut items = Vec::new(); while let Some(item) = try!(parser.parse_item().map_err(super::parse_to_lex_err)) { @@ -177,9 +177,8 @@ impl FromStr for TokenStream { __internal::with_parse_sess(|sess| { let src = src.to_string(); let name = "".to_string(); - let tts = parse::parse_tts_from_source_str(name, src, sess); - - Ok(__internal::token_stream_wrap(tts.into_iter().collect())) + let stream = parse::parse_stream_from_source_str(name, src, sess); + Ok(__internal::token_stream_wrap(stream)) }) } } diff --git a/src/libproc_macro_plugin/qquote.rs b/src/libproc_macro_plugin/qquote.rs index e3d85bca3e0d2..0276587ed52b1 100644 --- a/src/libproc_macro_plugin/qquote.rs +++ b/src/libproc_macro_plugin/qquote.rs @@ -18,7 +18,6 @@ use syntax::tokenstream::{self, Delimited, TokenTree, TokenStream}; use syntax_pos::DUMMY_SP; use std::iter; -use std::rc::Rc; pub fn qquote<'cx>(stream: TokenStream) -> TokenStream { stream.quote() @@ -50,10 +49,7 @@ macro_rules! quote_tree { } fn delimit(delim: token::DelimToken, stream: TokenStream) -> TokenStream { - TokenTree::Delimited(DUMMY_SP, Rc::new(Delimited { - delim: delim, - tts: stream.into_trees().collect(), - })).into() + TokenTree::Delimited(DUMMY_SP, Delimited { delim: delim, tts: stream.into() }).into() } macro_rules! quote { @@ -102,13 +98,6 @@ impl Quote for TokenStream { } } -impl Quote for Vec { - fn quote(&self) -> TokenStream { - let stream = self.iter().cloned().collect::(); - quote!((quote stream).into_trees().collect::<::std::vec::Vec<_> >()) - } -} - impl Quote for TokenTree { fn quote(&self) -> TokenStream { match *self { @@ -124,12 +113,12 @@ impl Quote for TokenTree { } } -impl Quote for Rc { +impl Quote for Delimited { fn quote(&self) -> TokenStream { - quote!(::std::rc::Rc::new(::syntax::tokenstream::Delimited { + quote!(::syntax::tokenstream::Delimited { delim: (quote self.delim), - tts: (quote self.tts), - })) + tts: (quote self.stream()).into(), + }) } } diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 8b6c75886baa8..20b6e285daebe 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -40,7 +40,7 @@ use syntax::ast::{Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect}; use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem}; use syntax::ptr::P; use syntax::symbol::{Symbol, keywords}; -use syntax::tokenstream::TokenTree; +use syntax::tokenstream::TokenStream; use syntax::util::ThinVec; use std::collections::BTreeMap; @@ -466,7 +466,7 @@ pub struct MacroDef { pub attrs: HirVec, pub id: NodeId, pub span: Span, - pub body: HirVec, + pub body: TokenStream, } #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] diff --git a/src/librustc_incremental/calculate_svh/svh_visitor.rs b/src/librustc_incremental/calculate_svh/svh_visitor.rs index b075fa5999249..e113d0971c21b 100644 --- a/src/librustc_incremental/calculate_svh/svh_visitor.rs +++ b/src/librustc_incremental/calculate_svh/svh_visitor.rs @@ -866,8 +866,8 @@ impl<'a, 'hash, 'tcx> visit::Visitor<'tcx> for StrictVersionHashVisitor<'a, 'has debug!("visit_macro_def: st={:?}", self.st); SawMacroDef.hash(self.st); hash_attrs!(self, ¯o_def.attrs); - for tt in ¯o_def.body { - self.hash_token_tree(tt); + for tt in macro_def.body.trees() { + self.hash_token_tree(&tt); } visit::walk_macro_def(self, macro_def) } @@ -1033,15 +1033,9 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> { } tokenstream::TokenTree::Delimited(span, ref delimited) => { hash_span!(self, span); - let tokenstream::Delimited { - ref delim, - ref tts, - } = **delimited; - - delim.hash(self.st); - tts.len().hash(self.st); - for sub_tt in tts { - self.hash_token_tree(sub_tt); + delimited.delim.hash(self.st); + for sub_tt in delimited.stream().trees() { + self.hash_token_tree(&sub_tt); } } } diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index 7b02280ef904b..274ea7094cb6b 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -34,7 +34,7 @@ use std::rc::Rc; use syntax::ast; use syntax::attr; -use syntax::parse::filemap_to_tts; +use syntax::parse::filemap_to_stream; use syntax::symbol::Symbol; use syntax_pos::{mk_sp, Span}; use rustc::hir::svh::Svh; @@ -397,7 +397,7 @@ impl CrateStore for cstore::CStore { let filemap = sess.parse_sess.codemap().new_filemap(source_name, None, def.body); let local_span = mk_sp(filemap.start_pos, filemap.end_pos); - let body = filemap_to_tts(&sess.parse_sess, filemap); + let body = filemap_to_stream(&sess.parse_sess, filemap); // Mark the attrs as used let attrs = data.get_item_attrs(id.index); @@ -415,7 +415,7 @@ impl CrateStore for cstore::CStore { id: ast::DUMMY_NODE_ID, span: local_span, attrs: attrs, - body: body, + body: body.into(), }) } diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index af0edab7a83bd..8ddc1642d9e1c 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -853,9 +853,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { /// Serialize the text of exported macros fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> { + use syntax::print::pprust; Entry { kind: EntryKind::MacroDef(self.lazy(&MacroDef { - body: ::syntax::print::pprust::tts_to_string(¯o_def.body) + body: pprust::tts_to_string(¯o_def.body.trees().collect::>()), })), visibility: self.lazy(&ty::Visibility::Public), span: self.lazy(¯o_def.span), diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 89cff39c59e31..751f59d0290ac 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -516,7 +516,7 @@ impl<'a> Resolver<'a> { expansion: Cell::new(LegacyScope::Empty), }); self.invocations.insert(mark, invocation); - macro_rules.body = mark_tts(¯o_rules.body, mark); + macro_rules.body = mark_tts(macro_rules.stream(), mark).into(); let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, ¯o_rules)); self.macro_map.insert(def_id, ext.clone()); ext diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index b7068f4b09f5f..36645418d4f78 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -545,7 +545,7 @@ impl<'a> Resolver<'a> { pub fn define_macro(&mut self, item: &ast::Item, legacy_scope: &mut LegacyScope<'a>) { let tts = match item.node { - ast::ItemKind::Mac(ref mac) => &mac.node.tts, + ast::ItemKind::Mac(ref mac) => mac.node.stream(), _ => unreachable!(), }; @@ -562,7 +562,7 @@ impl<'a> Resolver<'a> { attrs: item.attrs.clone(), id: ast::DUMMY_NODE_ID, span: item.span, - body: mark_tts(tts, mark), + body: mark_tts(tts, mark).into(), }; *legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding { diff --git a/src/librustc_save_analysis/span_utils.rs b/src/librustc_save_analysis/span_utils.rs index 6c93744f014a3..34402742e6c33 100644 --- a/src/librustc_save_analysis/span_utils.rs +++ b/src/librustc_save_analysis/span_utils.rs @@ -284,7 +284,7 @@ impl<'a> SpanUtils<'a> { pub fn signature_string_for_span(&self, span: Span) -> String { let mut toks = self.retokenise_span(span); toks.real_token(); - let mut toks = toks.parse_all_token_trees().unwrap().into_iter(); + let mut toks = toks.parse_all_token_trees().unwrap().trees(); let mut prev = toks.next().unwrap(); let first_span = prev.span(); diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 236d9f230b5d4..42928427233d7 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -211,7 +211,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { }; // FIXME(jseyfried) merge with `self.visit_macro()` - let matchers = def.body.chunks(4).map(|arm| arm[0].span()).collect(); + let tts = def.stream().trees().collect::>(); + let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect(); om.macros.push(Macro { def_id: def_id, attrs: def.attrs.clone().into(), @@ -520,8 +521,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // convert each exported_macro into a doc item fn visit_local_macro(&self, def: &hir::MacroDef) -> Macro { + let tts = def.body.trees().collect::>(); // Extract the spans of all matchers. They represent the "interface" of the macro. - let matchers = def.body.chunks(4).map(|arm| arm[0].span()).collect(); + let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect(); Macro { def_id: self.cx.tcx.hir.local_def_id(def.id), diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 09fb369cd3568..9cc754cbf4d19 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -24,7 +24,7 @@ use ext::hygiene::SyntaxContext; use print::pprust; use ptr::P; use symbol::{Symbol, keywords}; -use tokenstream::{TokenTree}; +use tokenstream::{ThinTokenStream, TokenStream}; use std::collections::HashSet; use std::fmt; @@ -1033,7 +1033,13 @@ pub type Mac = Spanned; #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct Mac_ { pub path: Path, - pub tts: Vec, + pub tts: ThinTokenStream, +} + +impl Mac_ { + pub fn stream(&self) -> TokenStream { + self.tts.clone().into() + } } #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] @@ -1915,7 +1921,13 @@ pub struct MacroDef { pub attrs: Vec, pub id: NodeId, pub span: Span, - pub body: Vec, + pub body: ThinTokenStream, +} + +impl MacroDef { + pub fn stream(&self) -> TokenStream { + self.body.clone().into() + } } #[cfg(test)] diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index c7d2f0cd31dc6..e242cf2777fe5 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -188,10 +188,7 @@ impl AttrProcMacro for F /// Represents a thing that maps token trees to Macro Results pub trait TTMacroExpander { - fn expand<'cx>(&self, - ecx: &'cx mut ExtCtxt, - span: Span, - token_tree: &[tokenstream::TokenTree]) + fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream) -> Box; } @@ -200,15 +197,11 @@ pub type MacroExpanderFn = -> Box; impl TTMacroExpander for F - where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree]) - -> Box + where F: for<'cx> Fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree]) -> Box { - fn expand<'cx>(&self, - ecx: &'cx mut ExtCtxt, - span: Span, - token_tree: &[tokenstream::TokenTree]) + fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream) -> Box { - (*self)(ecx, span, token_tree) + (*self)(ecx, span, &input.trees().collect::>()) } } @@ -654,9 +647,8 @@ impl<'a> ExtCtxt<'a> { expand::MacroExpander::new(self, true) } - pub fn new_parser_from_tts(&self, tts: &[tokenstream::TokenTree]) - -> parser::Parser<'a> { - parse::tts_to_parser(self.parse_sess, tts.to_vec()) + pub fn new_parser_from_tts(&self, tts: &[tokenstream::TokenTree]) -> parser::Parser<'a> { + parse::stream_to_parser(self.parse_sess, tts.iter().cloned().collect()) } pub fn codemap(&self) -> &'a CodeMap { self.parse_sess.codemap() } pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 8107696b8b920..f1662284a8820 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast::{self, Block, Ident, Mac_, PatKind}; +use ast::{self, Block, Ident, PatKind}; use ast::{Name, MacStmtStyle, StmtKind, ItemKind}; use attr::{self, HasAttrs}; use codemap::{ExpnInfo, NameAndSpan, MacroBang, MacroAttribute}; @@ -20,16 +20,15 @@ use ext::placeholders::{placeholder, PlaceholderExpander}; use feature_gate::{self, Features, is_builtin_attr}; use fold; use fold::*; +use parse::{filemap_to_stream, ParseSess, DirectoryOwnership, PResult, token}; use parse::parser::Parser; -use parse::token; -use parse::{ParseSess, DirectoryOwnership, PResult, filemap_to_tts}; use print::pprust; use ptr::P; use std_inject; use symbol::Symbol; use symbol::keywords; use syntax_pos::{self, Span, ExpnId}; -use tokenstream::{TokenTree, TokenStream}; +use tokenstream::TokenStream; use util::small_vector::SmallVector; use visit::Visitor; @@ -462,8 +461,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { kind.expect_from_annotatables(items) } SyntaxExtension::AttrProcMacro(ref mac) => { - let attr_toks = tts_for_attr_args(&attr, &self.cx.parse_sess).into_iter().collect(); - let item_toks = tts_for_item(&item, &self.cx.parse_sess).into_iter().collect(); + let attr_toks = stream_for_attr_args(&attr, &self.cx.parse_sess); + let item_toks = stream_for_item(&item, &self.cx.parse_sess); let tok_result = mac.expand(self.cx, attr.span, attr_toks, item_toks); self.parse_expansion(tok_result, kind, name, attr.span) @@ -487,11 +486,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> { InvocationKind::Bang { mac, ident, span } => (mac, ident, span), _ => unreachable!(), }; - let Mac_ { path, tts, .. } = mac.node; + let path = &mac.node.path; let extname = path.segments.last().unwrap().identifier.name; let ident = ident.unwrap_or(keywords::Invalid.ident()); - let marked_tts = mark_tts(&tts, mark); + let marked_tts = mark_tts(mac.node.stream(), mark); let opt_expanded = match *ext { NormalTT(ref expandfun, exp_span, allow_internal_unstable) => { if ident.name != keywords::Invalid.name() { @@ -510,7 +509,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { }, }); - kind.make_from(expandfun.expand(self.cx, span, &marked_tts)) + kind.make_from(expandfun.expand(self.cx, span, marked_tts)) } IdentTT(ref expander, tt_span, allow_internal_unstable) => { @@ -529,7 +528,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } }); - kind.make_from(expander.expand(self.cx, span, ident, marked_tts)) + let input: Vec<_> = marked_tts.into_trees().collect(); + kind.make_from(expander.expand(self.cx, span, ident, input)) } MultiDecorator(..) | MultiModifier(..) | SyntaxExtension::AttrProcMacro(..) => { @@ -563,8 +563,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { }, }); - let toks = marked_tts.into_iter().collect(); - let tok_result = expandfun.expand(self.cx, span, toks); + let tok_result = expandfun.expand(self.cx, span, marked_tts); Some(self.parse_expansion(tok_result, kind, extname, span)) } }; @@ -821,23 +820,23 @@ fn find_attr_invoc(attrs: &mut Vec) -> Option { // Therefore, we must use the pretty printer (yuck) to turn the AST node into a // string, which we then re-tokenise (double yuck), but first we have to patch // the pretty-printed string on to the end of the existing codemap (infinity-yuck). -fn tts_for_item(item: &Annotatable, parse_sess: &ParseSess) -> Vec { +fn stream_for_item(item: &Annotatable, parse_sess: &ParseSess) -> TokenStream { let text = match *item { Annotatable::Item(ref i) => pprust::item_to_string(i), Annotatable::TraitItem(ref ti) => pprust::trait_item_to_string(ti), Annotatable::ImplItem(ref ii) => pprust::impl_item_to_string(ii), }; - string_to_tts(text, parse_sess) + string_to_stream(text, parse_sess) } -fn tts_for_attr_args(attr: &ast::Attribute, parse_sess: &ParseSess) -> Vec { +fn stream_for_attr_args(attr: &ast::Attribute, parse_sess: &ParseSess) -> TokenStream { use ast::MetaItemKind::*; use print::pp::Breaks; use print::pprust::PrintState; let token_string = match attr.value.node { // For `#[foo]`, an empty token - Word => return vec![], + Word => return TokenStream::empty(), // For `#[foo(bar, baz)]`, returns `(bar, baz)` List(ref items) => pprust::to_string(|s| { s.popen()?; @@ -853,12 +852,12 @@ fn tts_for_attr_args(attr: &ast::Attribute, parse_sess: &ParseSess) -> Vec Vec { +fn string_to_stream(text: String, parse_sess: &ParseSess) -> TokenStream { let filename = String::from(""); - filemap_to_tts(parse_sess, parse_sess.codemap().new_filemap(filename, None, text)) + filemap_to_stream(parse_sess, parse_sess.codemap().new_filemap(filename, None, text)) } impl<'a, 'b> Folder for InvocationCollector<'a, 'b> { @@ -1162,6 +1161,6 @@ impl Folder for Marker { } // apply a given mark to the given token trees. Used prior to expansion of a macro. -pub fn mark_tts(tts: &[TokenTree], m: Mark) -> Vec { +pub fn mark_tts(tts: TokenStream, m: Mark) -> TokenStream { noop_fold_tts(tts, &mut Marker{mark:m, expn_id: None}) } diff --git a/src/libsyntax/ext/placeholders.rs b/src/libsyntax/ext/placeholders.rs index 0636a78b2152f..e2fb1946e90db 100644 --- a/src/libsyntax/ext/placeholders.rs +++ b/src/libsyntax/ext/placeholders.rs @@ -13,6 +13,7 @@ use codemap::{DUMMY_SP, dummy_spanned}; use ext::base::ExtCtxt; use ext::expand::{Expansion, ExpansionKind}; use ext::hygiene::Mark; +use tokenstream::TokenStream; use fold::*; use ptr::P; use symbol::keywords; @@ -26,7 +27,7 @@ pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion { fn mac_placeholder() -> ast::Mac { dummy_spanned(ast::Mac_ { path: ast::Path { span: DUMMY_SP, segments: Vec::new() }, - tts: Vec::new(), + tts: TokenStream::empty().into(), }) } diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index b1b69c80f4d00..69ff726e719a9 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -16,7 +16,7 @@ use ext::build::AstBuilder; use parse::parser::{Parser, PathStyle}; use parse::token; use ptr::P; -use tokenstream::TokenTree; +use tokenstream::{TokenStream, TokenTree}; /// Quasiquoting works via token trees. @@ -35,7 +35,7 @@ pub mod rt { use std::rc::Rc; use symbol::Symbol; - use tokenstream::{self, TokenTree}; + use tokenstream::{self, TokenTree, TokenStream}; pub use parse::new_parser_from_tts; pub use syntax_pos::{BytePos, Span, DUMMY_SP}; @@ -227,10 +227,10 @@ pub mod rt { if self.style == ast::AttrStyle::Inner { r.push(TokenTree::Token(self.span, token::Not)); } - r.push(TokenTree::Delimited(self.span, Rc::new(tokenstream::Delimited { + r.push(TokenTree::Delimited(self.span, tokenstream::Delimited { delim: token::Bracket, - tts: self.value.to_tokens(cx), - }))); + tts: self.value.to_tokens(cx).into_iter().collect::().into(), + })); r } } @@ -244,10 +244,10 @@ pub mod rt { impl ToTokens for () { fn to_tokens(&self, _cx: &ExtCtxt) -> Vec { - vec![TokenTree::Delimited(DUMMY_SP, Rc::new(tokenstream::Delimited { + vec![TokenTree::Delimited(DUMMY_SP, tokenstream::Delimited { delim: token::Paren, - tts: vec![], - }))] + tts: TokenStream::empty().into(), + })] } } @@ -355,14 +355,15 @@ pub mod rt { } fn parse_tts(&self, s: String) -> Vec { - parse::parse_tts_from_source_str("".to_string(), s, self.parse_sess()) + let source_name = "".to_owned(); + parse::parse_stream_from_source_str(source_name, s, self.parse_sess()) + .into_trees().collect() } } } // Replaces `Token::OpenDelim .. Token::CloseDelim` with `TokenTree::Delimited(..)`. pub fn unflatten(tts: Vec) -> Vec { - use std::rc::Rc; use tokenstream::Delimited; let mut results = Vec::new(); @@ -373,8 +374,10 @@ pub fn unflatten(tts: Vec) -> Vec { results.push(::std::mem::replace(&mut result, Vec::new())); } TokenTree::Token(span, token::CloseDelim(delim)) => { - let tree = - TokenTree::Delimited(span, Rc::new(Delimited { delim: delim, tts: result })); + let tree = TokenTree::Delimited(span, Delimited { + delim: delim, + tts: result.into_iter().map(TokenStream::from).collect::().into(), + }); result = results.pop().unwrap(); result.push(tree); } @@ -747,7 +750,7 @@ fn statements_mk_tt(cx: &ExtCtxt, tt: &TokenTree, quoted: bool) -> Vec { let mut stmts = statements_mk_tt(cx, &delimed.open_tt(span), false); - stmts.extend(statements_mk_tts(cx, &delimed.tts)); + stmts.extend(statements_mk_tts(cx, delimed.stream())); stmts.extend(statements_mk_tt(cx, &delimed.close_tt(span), false)); stmts } @@ -810,14 +813,14 @@ fn mk_stmts_let(cx: &ExtCtxt, sp: Span) -> Vec { vec![stmt_let_sp, stmt_let_tt] } -fn statements_mk_tts(cx: &ExtCtxt, tts: &[TokenTree]) -> Vec { +fn statements_mk_tts(cx: &ExtCtxt, tts: TokenStream) -> Vec { let mut ss = Vec::new(); let mut quoted = false; - for tt in tts { - quoted = match *tt { + for tt in tts.into_trees() { + quoted = match tt { TokenTree::Token(_, token::Dollar) if !quoted => true, _ => { - ss.extend(statements_mk_tt(cx, tt, quoted)); + ss.extend(statements_mk_tt(cx, &tt, quoted)); false } } @@ -829,7 +832,7 @@ fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[TokenTree]) -> (P, P), + Tt(TokenTree), + TtSeq(Vec), } impl TokenTreeOrTokenTreeVec { @@ -113,7 +113,7 @@ impl TokenTreeOrTokenTreeVec { } } - fn get_tt(&self, index: usize) -> quoted::TokenTree { + fn get_tt(&self, index: usize) -> TokenTree { match *self { TtSeq(ref v) => v[index].clone(), Tt(ref tt) => tt.get_tt(index), @@ -144,9 +144,7 @@ struct MatcherPos { pub type NamedParseResult = ParseResult>>; -pub fn count_names(ms: &[quoted::TokenTree]) -> usize { - use self::quoted::TokenTree; - +pub fn count_names(ms: &[TokenTree]) -> usize { ms.iter().fold(0, |count, elt| { count + match *elt { TokenTree::Sequence(_, ref seq) => { @@ -163,7 +161,7 @@ pub fn count_names(ms: &[quoted::TokenTree]) -> usize { }) } -fn initial_matcher_pos(ms: Vec, lo: BytePos) -> Box { +fn initial_matcher_pos(ms: Vec, lo: BytePos) -> Box { let match_idx_hi = count_names(&ms[..]); let matches = create_matches(match_idx_hi); Box::new(MatcherPos { @@ -202,10 +200,8 @@ pub enum NamedMatch { MatchedNonterminal(Rc) } -fn nameize>>(sess: &ParseSess, ms: &[quoted::TokenTree], mut res: I) +fn nameize>>(sess: &ParseSess, ms: &[TokenTree], mut res: I) -> NamedParseResult { - use self::quoted::TokenTree; - fn n_rec>>(sess: &ParseSess, m: &TokenTree, mut res: &mut I, ret_val: &mut HashMap>) -> Result<(), (syntax_pos::Span, String)> { @@ -289,9 +285,8 @@ fn inner_parse_loop(sess: &ParseSess, eof_eis: &mut SmallVector>, bb_eis: &mut SmallVector>, token: &Token, - span: &syntax_pos::Span) -> ParseResult<()> { - use self::quoted::TokenTree; - + span: &syntax_pos::Span) + -> ParseResult<()> { while let Some(mut ei) = cur_eis.pop() { // When unzipped trees end, remove them while ei.idx >= ei.top_elts.len() { @@ -419,13 +414,8 @@ fn inner_parse_loop(sess: &ParseSess, Success(()) } -pub fn parse(sess: &ParseSess, - tts: Vec, - ms: &[quoted::TokenTree], - directory: Option) +pub fn parse(sess: &ParseSess, tts: TokenStream, ms: &[TokenTree], directory: Option) -> NamedParseResult { - use self::quoted::TokenTree; - let mut parser = Parser::new(sess, tts, directory, true); let mut cur_eis = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo)); let mut next_eis = Vec::new(); // or proceed normally diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 193c06707c7a6..1d386c1a3ac93 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -22,9 +22,8 @@ use parse::{Directory, ParseSess}; use parse::parser::Parser; use parse::token::{self, NtTT}; use parse::token::Token::*; -use print; use symbol::Symbol; -use tokenstream::TokenTree; +use tokenstream::{TokenStream, TokenTree}; use std::collections::{HashMap}; use std::collections::hash_map::{Entry}; @@ -68,7 +67,7 @@ impl TTMacroExpander for MacroRulesMacroExpander { fn expand<'cx>(&self, cx: &'cx mut ExtCtxt, sp: Span, - arg: &[TokenTree]) + input: TokenStream) -> Box { if !self.valid { return DummyResult::any(sp); @@ -76,7 +75,7 @@ impl TTMacroExpander for MacroRulesMacroExpander { generic_extension(cx, sp, self.name, - arg, + input, &self.lhses, &self.rhses) } @@ -86,14 +85,12 @@ impl TTMacroExpander for MacroRulesMacroExpander { fn generic_extension<'cx>(cx: &'cx ExtCtxt, sp: Span, name: ast::Ident, - arg: &[TokenTree], + arg: TokenStream, lhses: &[quoted::TokenTree], rhses: &[quoted::TokenTree]) -> Box { if cx.trace_macros() { - println!("{}! {{ {} }}", - name, - print::pprust::tts_to_string(arg)); + println!("{}! {{ {} }}", name, arg); } // Which arm's failure should we report? (the one furthest along) @@ -106,7 +103,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt, _ => cx.span_bug(sp, "malformed macro lhs") }; - match TokenTree::parse(cx, lhs_tt, arg) { + match TokenTree::parse(cx, lhs_tt, arg.clone()) { Success(named_matches) => { let rhs = match rhses[i] { // ignore delimiters @@ -186,7 +183,7 @@ pub fn compile(sess: &ParseSess, def: &ast::MacroDef) -> SyntaxExtension { ]; // Parse the macro_rules! invocation - let argument_map = match parse(sess, def.body.clone(), &argument_gram, None) { + let argument_map = match parse(sess, def.body.clone().into(), &argument_gram, None) { Success(m) => m, Failure(sp, tok) => { let s = parse_failure_msg(tok); @@ -205,7 +202,7 @@ pub fn compile(sess: &ParseSess, def: &ast::MacroDef) -> SyntaxExtension { s.iter().map(|m| { if let MatchedNonterminal(ref nt) = **m { if let NtTT(ref tt) = **nt { - let tt = quoted::parse(&[tt.clone()], true, sess).pop().unwrap(); + let tt = quoted::parse(tt.clone().into(), true, sess).pop().unwrap(); valid &= check_lhs_nt_follows(sess, &tt); return tt; } @@ -221,7 +218,7 @@ pub fn compile(sess: &ParseSess, def: &ast::MacroDef) -> SyntaxExtension { s.iter().map(|m| { if let MatchedNonterminal(ref nt) = **m { if let NtTT(ref tt) = **nt { - return quoted::parse(&[tt.clone()], false, sess).pop().unwrap(); + return quoted::parse(tt.clone().into(), false, sess).pop().unwrap(); } } sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs") diff --git a/src/libsyntax/ext/tt/quoted.rs b/src/libsyntax/ext/tt/quoted.rs index 530824b28348a..d56859d805c87 100644 --- a/src/libsyntax/ext/tt/quoted.rs +++ b/src/libsyntax/ext/tt/quoted.rs @@ -124,10 +124,10 @@ impl TokenTree { } } -pub fn parse(input: &[tokenstream::TokenTree], expect_matchers: bool, sess: &ParseSess) +pub fn parse(input: tokenstream::TokenStream, expect_matchers: bool, sess: &ParseSess) -> Vec { let mut result = Vec::new(); - let mut trees = input.iter().cloned(); + let mut trees = input.trees(); while let Some(tree) = trees.next() { let tree = parse_tree(tree, &mut trees, expect_matchers, sess); match tree { @@ -161,13 +161,13 @@ fn parse_tree(tree: tokenstream::TokenTree, { match tree { tokenstream::TokenTree::Token(span, token::Dollar) => match trees.next() { - Some(tokenstream::TokenTree::Delimited(span, ref delimited)) => { + Some(tokenstream::TokenTree::Delimited(span, delimited)) => { if delimited.delim != token::Paren { let tok = pprust::token_to_string(&token::OpenDelim(delimited.delim)); let msg = format!("expected `(`, found `{}`", tok); sess.span_diagnostic.span_err(span, &msg); } - let sequence = parse(&delimited.tts, expect_matchers, sess); + let sequence = parse(delimited.tts.into(), expect_matchers, sess); let (separator, op) = parse_sep_and_kleene_op(trees, span, sess); let name_captures = macro_parser::count_names(&sequence); TokenTree::Sequence(span, Rc::new(SequenceRepetition { @@ -197,7 +197,7 @@ fn parse_tree(tree: tokenstream::TokenTree, tokenstream::TokenTree::Delimited(span, delimited) => { TokenTree::Delimited(span, Rc::new(Delimited { delim: delimited.delim, - tts: parse(&delimited.tts, expect_matchers, sess), + tts: parse(delimited.tts.into(), expect_matchers, sess), })) } } diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index 90f64a5208f75..24004492be2a0 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -14,7 +14,7 @@ use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal}; use ext::tt::quoted; use parse::token::{self, SubstNt, Token, NtIdent, NtTT}; use syntax_pos::{Span, DUMMY_SP}; -use tokenstream::{TokenTree, Delimited}; +use tokenstream::{TokenStream, TokenTree, Delimited}; use util::small_vector::SmallVector; use std::rc::Rc; @@ -66,11 +66,11 @@ impl Iterator for Frame { pub fn transcribe(sp_diag: &Handler, interp: Option>>, src: Vec) - -> Vec { + -> TokenStream { let mut stack = SmallVector::one(Frame::new(src)); let interpolations = interp.unwrap_or_else(HashMap::new); /* just a convenience */ let mut repeats = Vec::new(); - let mut result = Vec::new(); + let mut result: Vec = Vec::new(); let mut result_stack = Vec::new(); loop { @@ -84,8 +84,11 @@ pub fn transcribe(sp_diag: &Handler, *idx = 0; if let Some(sep) = sep.clone() { // repeat same span, I guess - let prev_span = result.last().map(TokenTree::span).unwrap_or(DUMMY_SP); - result.push(TokenTree::Token(prev_span, sep)); + let prev_span = match result.last() { + Some(stream) => stream.trees().next().unwrap().span(), + None => DUMMY_SP, + }; + result.push(TokenTree::Token(prev_span, sep).into()); } continue } @@ -97,14 +100,14 @@ pub fn transcribe(sp_diag: &Handler, } Frame::Delimited { forest, span, .. } => { if result_stack.is_empty() { - return result; + return TokenStream::concat(result); } - let tree = TokenTree::Delimited(span, Rc::new(Delimited { + let tree = TokenTree::Delimited(span, Delimited { delim: forest.delim, - tts: result, - })); + tts: TokenStream::concat(result).into(), + }); result = result_stack.pop().unwrap(); - result.push(tree); + result.push(tree.into()); } } continue @@ -148,19 +151,20 @@ pub fn transcribe(sp_diag: &Handler, // FIXME #2887: think about span stuff here quoted::TokenTree::Token(sp, SubstNt(ident)) => { match lookup_cur_matched(ident, &interpolations, &repeats) { - None => result.push(TokenTree::Token(sp, SubstNt(ident))), + None => result.push(TokenTree::Token(sp, SubstNt(ident)).into()), Some(cur_matched) => if let MatchedNonterminal(ref nt) = *cur_matched { match **nt { // sidestep the interpolation tricks for ident because // (a) idents can be in lots of places, so it'd be a pain // (b) we actually can, since it's a token. NtIdent(ref sn) => { - result.push(TokenTree::Token(sn.span, token::Ident(sn.node))); + let token = TokenTree::Token(sn.span, token::Ident(sn.node)); + result.push(token.into()); } - NtTT(ref tt) => result.push(tt.clone()), + NtTT(ref tt) => result.push(tt.clone().into()), _ => { - // FIXME(pcwalton): Bad copy - result.push(TokenTree::Token(sp, token::Interpolated(nt.clone()))); + let token = TokenTree::Token(sp, token::Interpolated(nt.clone())); + result.push(token.into()); } } } else { @@ -174,7 +178,7 @@ pub fn transcribe(sp_diag: &Handler, stack.push(Frame::Delimited { forest: delimited, idx: 0, span: span }); result_stack.push(mem::replace(&mut result, Vec::new())); } - quoted::TokenTree::Token(span, tok) => result.push(TokenTree::Token(span, tok)), + quoted::TokenTree::Token(span, tok) => result.push(TokenTree::Token(span, tok).into()), quoted::TokenTree::MetaVarDecl(..) => panic!("unexpected `TokenTree::MetaVarDecl"), } } diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 257b7efba5c8e..4242b0f8b9803 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -233,11 +233,11 @@ pub trait Folder : Sized { noop_fold_ty_params(tps, self) } - fn fold_tt(&mut self, tt: &TokenTree) -> TokenTree { + fn fold_tt(&mut self, tt: TokenTree) -> TokenTree { noop_fold_tt(tt, self) } - fn fold_tts(&mut self, tts: &[TokenTree]) -> Vec { + fn fold_tts(&mut self, tts: TokenStream) -> TokenStream { noop_fold_tts(tts, self) } @@ -497,8 +497,8 @@ pub fn noop_fold_attribute(attr: Attribute, fld: &mut T) -> Option(Spanned {node, span}: Mac, fld: &mut T) -> Mac { Spanned { node: Mac_ { + tts: fld.fold_tts(node.stream()).into(), path: fld.fold_path(node.path), - tts: fld.fold_tts(&node.tts), }, span: fld.new_span(span) } @@ -539,23 +539,19 @@ pub fn noop_fold_arg(Arg {id, pat, ty}: Arg, fld: &mut T) -> Arg { } } -pub fn noop_fold_tt(tt: &TokenTree, fld: &mut T) -> TokenTree { - match *tt { - TokenTree::Token(span, ref tok) => - TokenTree::Token(fld.new_span(span), fld.fold_token(tok.clone())), - TokenTree::Delimited(span, ref delimed) => { - TokenTree::Delimited(fld.new_span(span), Rc::new( - Delimited { - delim: delimed.delim, - tts: fld.fold_tts(&delimed.tts), - } - )) - }, +pub fn noop_fold_tt(tt: TokenTree, fld: &mut T) -> TokenTree { + match tt { + TokenTree::Token(span, tok) => + TokenTree::Token(fld.new_span(span), fld.fold_token(tok)), + TokenTree::Delimited(span, delimed) => TokenTree::Delimited(fld.new_span(span), Delimited { + tts: fld.fold_tts(delimed.stream()).into(), + delim: delimed.delim, + }), } } -pub fn noop_fold_tts(tts: &[TokenTree], fld: &mut T) -> Vec { - tts.iter().map(|tt| fld.fold_tt(tt)).collect() +pub fn noop_fold_tts(tts: TokenStream, fld: &mut T) -> TokenStream { + tts.trees().map(|tt| fld.fold_tt(tt)).collect() } // apply ident folder if it's an ident, apply other folds to interpolated nodes @@ -617,7 +613,7 @@ pub fn noop_fold_interpolated(nt: token::Nonterminal, fld: &mut T) token::NtIdent(id) => token::NtIdent(Spanned::{node: fld.fold_ident(id.node), ..id}), token::NtMeta(meta_item) => token::NtMeta(fld.fold_meta_item(meta_item)), token::NtPath(path) => token::NtPath(fld.fold_path(path)), - token::NtTT(tt) => token::NtTT(fld.fold_tt(&tt)), + token::NtTT(tt) => token::NtTT(fld.fold_tt(tt)), token::NtArm(arm) => token::NtArm(fld.fold_arm(arm)), token::NtImplItem(item) => token::NtImplItem(fld.fold_impl_item(item) diff --git a/src/libsyntax/parse/lexer/tokentrees.rs b/src/libsyntax/parse/lexer/tokentrees.rs index eafc3f77ab052..554a1fcfc71a6 100644 --- a/src/libsyntax/parse/lexer/tokentrees.rs +++ b/src/libsyntax/parse/lexer/tokentrees.rs @@ -12,32 +12,30 @@ use print::pprust::token_to_string; use parse::lexer::StringReader; use parse::{token, PResult}; use syntax_pos::Span; -use tokenstream::{Delimited, TokenTree}; - -use std::rc::Rc; +use tokenstream::{Delimited, TokenStream, TokenTree}; impl<'a> StringReader<'a> { // Parse a stream of tokens into a list of `TokenTree`s, up to an `Eof`. - pub fn parse_all_token_trees(&mut self) -> PResult<'a, Vec> { + pub fn parse_all_token_trees(&mut self) -> PResult<'a, TokenStream> { let mut tts = Vec::new(); while self.token != token::Eof { - tts.push(self.parse_token_tree()?); + tts.push(self.parse_token_tree()?.into()); } - Ok(tts) + Ok(TokenStream::concat(tts)) } // Parse a stream of tokens into a list of `TokenTree`s, up to a `CloseDelim`. - fn parse_token_trees_until_close_delim(&mut self) -> Vec { + fn parse_token_trees_until_close_delim(&mut self) -> TokenStream { let mut tts = vec![]; loop { if let token::CloseDelim(..) = self.token { - return tts; + return TokenStream::concat(tts); } match self.parse_token_tree() { - Ok(tt) => tts.push(tt), + Ok(tt) => tts.push(tt.into()), Err(mut e) => { e.emit(); - return tts; + return TokenStream::concat(tts); } } } @@ -111,10 +109,10 @@ impl<'a> StringReader<'a> { _ => {} } - Ok(TokenTree::Delimited(span, Rc::new(Delimited { + Ok(TokenTree::Delimited(span, Delimited { delim: delim, - tts: tts, - }))) + tts: tts.into(), + })) }, token::CloseDelim(_) => { // An unexpected closing delimiter (i.e., there is no diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index f783e32d62104..7207463e1b9ab 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -19,7 +19,7 @@ use parse::parser::Parser; use ptr::P; use str::char_at; use symbol::Symbol; -use tokenstream; +use tokenstream::{TokenStream, TokenTree}; use std::cell::RefCell; use std::collections::HashSet; @@ -141,9 +141,9 @@ pub fn parse_stmt_from_source_str<'a>(name: String, source: String, sess: &'a Pa new_parser_from_source_str(sess, name, source).parse_stmt() } -pub fn parse_tts_from_source_str<'a>(name: String, source: String, sess: &'a ParseSess) - -> Vec { - filemap_to_tts(sess, sess.codemap().new_filemap(name, None, source)) +pub fn parse_stream_from_source_str<'a>(name: String, source: String, sess: &'a ParseSess) + -> TokenStream { + filemap_to_stream(sess, sess.codemap().new_filemap(name, None, source)) } // Create a new parser from a source string @@ -175,7 +175,7 @@ pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess, /// Given a filemap and config, return a parser pub fn filemap_to_parser<'a>(sess: &'a ParseSess, filemap: Rc, ) -> Parser<'a> { let end_pos = filemap.end_pos; - let mut parser = tts_to_parser(sess, filemap_to_tts(sess, filemap)); + let mut parser = stream_to_parser(sess, filemap_to_stream(sess, filemap)); if parser.token == token::Eof && parser.span == syntax_pos::DUMMY_SP { parser.span = syntax_pos::mk_sp(end_pos, end_pos); @@ -186,13 +186,8 @@ pub fn filemap_to_parser<'a>(sess: &'a ParseSess, filemap: Rc, ) -> Par // must preserve old name for now, because quote! from the *existing* // compiler expands into it -pub fn new_parser_from_tts<'a>(sess: &'a ParseSess, tts: Vec) - -> Parser<'a> { - tts_to_parser(sess, tts) -} - -pub fn new_parser_from_ts<'a>(sess: &'a ParseSess, ts: tokenstream::TokenStream) -> Parser<'a> { - tts_to_parser(sess, ts.into_trees().collect()) +pub fn new_parser_from_tts<'a>(sess: &'a ParseSess, tts: Vec) -> Parser<'a> { + stream_to_parser(sess, tts.into_iter().collect()) } @@ -215,15 +210,15 @@ fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option) } /// Given a filemap, produce a sequence of token-trees -pub fn filemap_to_tts(sess: &ParseSess, filemap: Rc) -> Vec { +pub fn filemap_to_stream(sess: &ParseSess, filemap: Rc) -> TokenStream { let mut srdr = lexer::StringReader::new(sess, filemap); srdr.real_token(); panictry!(srdr.parse_all_token_trees()) } -/// Given tts and the ParseSess, produce a parser -pub fn tts_to_parser<'a>(sess: &'a ParseSess, tts: Vec) -> Parser<'a> { - let mut p = Parser::new(sess, tts, None, false); +/// Given stream and the ParseSess, produce a parser +pub fn stream_to_parser<'a>(sess: &'a ParseSess, stream: TokenStream) -> Parser<'a> { + let mut p = Parser::new(sess, stream, None, false); p.check_unknown_macro_variable(); p } @@ -660,7 +655,7 @@ mod tests { #[test] fn string_to_tts_macro () { let tts = string_to_tts("macro_rules! zip (($a)=>($a))".to_string()); - let tts: &[tokenstream::TokenTree] = &tts[..]; + let tts: &[TokenTree] = &tts[..]; match (tts.len(), tts.get(0), tts.get(1), tts.get(2), tts.get(3)) { ( diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index b12b0c0326701..c88b859e036d4 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -53,7 +53,7 @@ use util::parser::{AssocOp, Fixity}; use print::pprust; use ptr::P; use parse::PResult; -use tokenstream::{self, Delimited, TokenTree, TokenStream}; +use tokenstream::{self, Delimited, ThinTokenStream, TokenTree, TokenStream}; use symbol::{Symbol, keywords}; use util::ThinVec; @@ -200,7 +200,7 @@ impl TokenCursorFrame { delim: delimited.delim, span: sp, open_delim: delimited.delim == token::NoDelim, - tree_cursor: delimited.tts.iter().cloned().collect::().into_trees(), + tree_cursor: delimited.stream().into_trees(), close_delim: delimited.delim == token::NoDelim, } } @@ -211,12 +211,14 @@ impl TokenCursor { loop { let tree = if !self.frame.open_delim { self.frame.open_delim = true; - Delimited { delim: self.frame.delim, tts: Vec::new() }.open_tt(self.frame.span) + Delimited { delim: self.frame.delim, tts: TokenStream::empty().into() } + .open_tt(self.frame.span) } else if let Some(tree) = self.frame.tree_cursor.next() { tree } else if !self.frame.close_delim { self.frame.close_delim = true; - Delimited { delim: self.frame.delim, tts: Vec::new() }.close_tt(self.frame.span) + Delimited { delim: self.frame.delim, tts: TokenStream::empty().into() } + .close_tt(self.frame.span) } else if let Some(frame) = self.stack.pop() { self.frame = frame; continue @@ -255,21 +257,23 @@ impl TokenCursor { num_of_hashes = cmp::max(num_of_hashes, count); } - let body = TokenTree::Delimited(sp, Rc::new(Delimited { + let body = TokenTree::Delimited(sp, Delimited { delim: token::Bracket, - tts: vec![TokenTree::Token(sp, token::Ident(ast::Ident::from_str("doc"))), - TokenTree::Token(sp, token::Eq), - TokenTree::Token(sp, token::Literal( - token::StrRaw(Symbol::intern(&stripped), num_of_hashes), None))], - })); + tts: [TokenTree::Token(sp, token::Ident(ast::Ident::from_str("doc"))), + TokenTree::Token(sp, token::Eq), + TokenTree::Token(sp, token::Literal( + token::StrRaw(Symbol::intern(&stripped), num_of_hashes), None))] + .iter().cloned().collect::().into(), + }); self.stack.push(mem::replace(&mut self.frame, TokenCursorFrame::new(sp, &Delimited { delim: token::NoDelim, tts: if doc_comment_style(&name.as_str()) == AttrStyle::Inner { [TokenTree::Token(sp, token::Pound), TokenTree::Token(sp, token::Not), body] - .iter().cloned().collect() + .iter().cloned().collect::().into() } else { - [TokenTree::Token(sp, token::Pound), body].iter().cloned().collect() + [TokenTree::Token(sp, token::Pound), body] + .iter().cloned().collect::().into() }, }))); @@ -405,7 +409,7 @@ impl From> for LhsExpr { impl<'a> Parser<'a> { pub fn new(sess: &'a ParseSess, - tokens: Vec, + tokens: TokenStream, directory: Option, desugar_doc_comments: bool) -> Self { @@ -423,7 +427,7 @@ impl<'a> Parser<'a> { token_cursor: TokenCursor { frame: TokenCursorFrame::new(syntax_pos::DUMMY_SP, &Delimited { delim: token::NoDelim, - tts: tokens, + tts: tokens.into(), }), stack: Vec::new(), }, @@ -2098,10 +2102,10 @@ impl<'a> Parser<'a> { }) } - fn expect_delimited_token_tree(&mut self) -> PResult<'a, (token::DelimToken, Vec)> { + fn expect_delimited_token_tree(&mut self) -> PResult<'a, (token::DelimToken, ThinTokenStream)> { match self.token { token::OpenDelim(delim) => self.parse_token_tree().map(|tree| match tree { - TokenTree::Delimited(_, delimited) => (delim, delimited.tts.clone()), + TokenTree::Delimited(_, delimited) => (delim, delimited.stream().into()), _ => unreachable!(), }), _ => Err(self.fatal("expected open delimiter")), @@ -2649,10 +2653,10 @@ impl<'a> Parser<'a> { self.token_cursor.stack.pop().unwrap()); self.span = frame.span; self.bump(); - return Ok(TokenTree::Delimited(frame.span, Rc::new(Delimited { + return Ok(TokenTree::Delimited(frame.span, Delimited { delim: frame.delim, - tts: frame.tree_cursor.original_stream().trees().collect(), - }))); + tts: frame.tree_cursor.original_stream().into(), + })); }, token::CloseDelim(_) | token::Eof => unreachable!(), _ => Ok(TokenTree::Token(self.span, self.bump_and_get())), diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index ec962d03458d1..53ef8e8dfa49c 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -286,7 +286,7 @@ pub fn token_to_string(tok: &Token) -> String { token::NtStmt(ref e) => stmt_to_string(&e), token::NtPat(ref e) => pat_to_string(&e), token::NtIdent(ref e) => ident_to_string(e.node), - token::NtTT(ref e) => tt_to_string(&e), + token::NtTT(ref tree) => tt_to_string(tree.clone()), token::NtArm(ref e) => arm_to_string(&e), token::NtImplItem(ref e) => impl_item_to_string(&e), token::NtTraitItem(ref e) => trait_item_to_string(&e), @@ -321,12 +321,12 @@ pub fn lifetime_to_string(e: &ast::Lifetime) -> String { to_string(|s| s.print_lifetime(e)) } -pub fn tt_to_string(tt: &tokenstream::TokenTree) -> String { +pub fn tt_to_string(tt: tokenstream::TokenTree) -> String { to_string(|s| s.print_tt(tt)) } pub fn tts_to_string(tts: &[tokenstream::TokenTree]) -> String { - to_string(|s| s.print_tts(tts)) + to_string(|s| s.print_tts(tts.iter().cloned().collect())) } pub fn stmt_to_string(stmt: &ast::Stmt) -> String { @@ -1324,7 +1324,7 @@ impl<'a> State<'a> { self.print_ident(item.ident)?; self.cbox(INDENT_UNIT)?; self.popen()?; - self.print_tts(&node.tts[..])?; + self.print_tts(node.stream())?; self.pclose()?; word(&mut self.s, ";")?; self.end()?; @@ -1456,8 +1456,8 @@ impl<'a> State<'a> { /// appropriate macro, transcribe back into the grammar we just parsed from, /// and then pretty-print the resulting AST nodes (so, e.g., we print /// expression arguments as expressions). It can be done! I think. - pub fn print_tt(&mut self, tt: &tokenstream::TokenTree) -> io::Result<()> { - match *tt { + pub fn print_tt(&mut self, tt: tokenstream::TokenTree) -> io::Result<()> { + match tt { TokenTree::Token(_, ref tk) => { word(&mut self.s, &token_to_string(tk))?; match *tk { @@ -1470,16 +1470,16 @@ impl<'a> State<'a> { TokenTree::Delimited(_, ref delimed) => { word(&mut self.s, &token_to_string(&delimed.open_token()))?; space(&mut self.s)?; - self.print_tts(&delimed.tts)?; + self.print_tts(delimed.stream())?; space(&mut self.s)?; word(&mut self.s, &token_to_string(&delimed.close_token())) }, } } - pub fn print_tts(&mut self, tts: &[tokenstream::TokenTree]) -> io::Result<()> { + pub fn print_tts(&mut self, tts: tokenstream::TokenStream) -> io::Result<()> { self.ibox(0)?; - for (i, tt) in tts.iter().enumerate() { + for (i, tt) in tts.into_trees().enumerate() { if i != 0 { space(&mut self.s)?; } @@ -1550,7 +1550,7 @@ impl<'a> State<'a> { word(&mut self.s, "! ")?; self.cbox(INDENT_UNIT)?; self.popen()?; - self.print_tts(&node.tts[..])?; + self.print_tts(node.stream())?; self.pclose()?; word(&mut self.s, ";")?; self.end()? @@ -1586,7 +1586,7 @@ impl<'a> State<'a> { word(&mut self.s, "! ")?; self.cbox(INDENT_UNIT)?; self.popen()?; - self.print_tts(&node.tts[..])?; + self.print_tts(node.stream())?; self.pclose()?; word(&mut self.s, ";")?; self.end()? @@ -1779,7 +1779,7 @@ impl<'a> State<'a> { } token::NoDelim => {} } - self.print_tts(&m.node.tts)?; + self.print_tts(m.node.stream())?; match delim { token::Paren => self.pclose(), token::Bracket => word(&mut self.s, "]"), diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 083435a043362..b7728609acaa5 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -22,20 +22,17 @@ //! and a borrowed TokenStream is sufficient to build an owned TokenStream without taking //! ownership of the original. -use ast::{self, LitKind}; use syntax_pos::{BytePos, Span, DUMMY_SP}; -use codemap::Spanned; use ext::base; use ext::tt::{macro_parser, quoted}; -use parse::{self, Directory}; -use parse::token::{self, Token, Lit}; +use parse::Directory; +use parse::token::{self, Token}; use print::pprust; use serialize::{Decoder, Decodable, Encoder, Encodable}; -use symbol::Symbol; use util::RcSlice; use std::{fmt, iter, mem}; -use std::rc::Rc; +use std::hash::{self, Hash}; /// A delimited sequence of token trees #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] @@ -43,7 +40,7 @@ pub struct Delimited { /// The type of delimiter pub delim: token::DelimToken, /// The delimited sequence of token trees - pub tts: Vec, + pub tts: ThinTokenStream, } impl Delimited { @@ -76,8 +73,8 @@ impl Delimited { } /// Returns the token trees inside the delimiters. - pub fn subtrees(&self) -> &[TokenTree] { - &self.tts + pub fn stream(&self) -> TokenStream { + self.tts.clone().into() } } @@ -98,19 +95,19 @@ pub enum TokenTree { /// A single token Token(Span, token::Token), /// A delimited sequence of token trees - Delimited(Span, Rc), + Delimited(Span, Delimited), } impl TokenTree { /// Use this token tree as a matcher to parse given tts. - pub fn parse(cx: &base::ExtCtxt, mtch: &[quoted::TokenTree], tts: &[TokenTree]) + pub fn parse(cx: &base::ExtCtxt, mtch: &[quoted::TokenTree], tts: TokenStream) -> macro_parser::NamedParseResult { // `None` is because we're not interpolating let directory = Directory { path: cx.current_expansion.module.directory.clone(), ownership: cx.current_expansion.directory_ownership, }; - macro_parser::parse(cx.parse_sess(), tts.iter().cloned().collect(), mtch, Some(directory)) + macro_parser::parse(cx.parse_sess(), tts, mtch, Some(directory)) } /// Check if this TokenTree is equal to the other, regardless of span information. @@ -118,15 +115,8 @@ impl TokenTree { match (self, other) { (&TokenTree::Token(_, ref tk), &TokenTree::Token(_, ref tk2)) => tk == tk2, (&TokenTree::Delimited(_, ref dl), &TokenTree::Delimited(_, ref dl2)) => { - (*dl).delim == (*dl2).delim && dl.tts.len() == dl2.tts.len() && - { - for (tt1, tt2) in dl.tts.iter().zip(dl2.tts.iter()) { - if !tt1.eq_unspanned(tt2) { - return false; - } - } - true - } + dl.delim == dl2.delim && + dl.stream().trees().zip(dl2.stream().trees()).all(|(tt, tt2)| tt.eq_unspanned(&tt2)) } (_, _) => false, } @@ -146,64 +136,6 @@ impl TokenTree { _ => false, } } - - /// Indicates if the token is an identifier. - pub fn is_ident(&self) -> bool { - self.maybe_ident().is_some() - } - - /// Returns an identifier. - pub fn maybe_ident(&self) -> Option { - match *self { - TokenTree::Token(_, Token::Ident(t)) => Some(t.clone()), - TokenTree::Delimited(_, ref dl) => { - let tts = dl.subtrees(); - if tts.len() != 1 { - return None; - } - tts[0].maybe_ident() - } - _ => None, - } - } - - /// Returns a Token literal. - pub fn maybe_lit(&self) -> Option { - match *self { - TokenTree::Token(_, Token::Literal(l, _)) => Some(l.clone()), - TokenTree::Delimited(_, ref dl) => { - let tts = dl.subtrees(); - if tts.len() != 1 { - return None; - } - tts[0].maybe_lit() - } - _ => None, - } - } - - /// Returns an AST string literal. - pub fn maybe_str(&self) -> Option { - match *self { - TokenTree::Token(sp, Token::Literal(Lit::Str_(s), _)) => { - let l = LitKind::Str(Symbol::intern(&parse::str_lit(&s.as_str())), - ast::StrStyle::Cooked); - Some(Spanned { - node: l, - span: sp, - }) - } - TokenTree::Token(sp, Token::Literal(Lit::StrRaw(s, n), _)) => { - let l = LitKind::Str(Symbol::intern(&parse::raw_str_lit(&s.as_str())), - ast::StrStyle::Raw(n)); - Some(Spanned { - node: l, - span: sp, - }) - } - _ => None, - } - } } /// # Token Streams @@ -396,6 +328,36 @@ impl Cursor { } } +/// The `TokenStream` type is large enough to represent a single `TokenTree` without allocation. +/// `ThinTokenStream` is smaller, but needs to allocate to represent a single `TokenTree`. +/// We must use `ThinTokenStream` in `TokenTree::Delimited` to avoid infinite size due to recursion. +#[derive(Debug, Clone)] +pub struct ThinTokenStream(Option>); + +impl From for ThinTokenStream { + fn from(stream: TokenStream) -> ThinTokenStream { + ThinTokenStream(match stream.kind { + TokenStreamKind::Empty => None, + TokenStreamKind::Tree(tree) => Some(RcSlice::new(vec![tree.into()])), + TokenStreamKind::Stream(stream) => Some(stream), + }) + } +} + +impl From for TokenStream { + fn from(stream: ThinTokenStream) -> TokenStream { + stream.0.map(TokenStream::concat_rc_slice).unwrap_or_else(TokenStream::empty) + } +} + +impl Eq for ThinTokenStream {} + +impl PartialEq for ThinTokenStream { + fn eq(&self, other: &ThinTokenStream) -> bool { + TokenStream::from(self.clone()) == TokenStream::from(other.clone()) + } +} + impl fmt::Display for TokenStream { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(&pprust::tts_to_string(&self.trees().collect::>())) @@ -414,6 +376,32 @@ impl Decodable for TokenStream { } } +impl Hash for TokenStream { + fn hash(&self, state: &mut H) { + for tree in self.trees() { + tree.hash(state); + } + } +} + +impl Encodable for ThinTokenStream { + fn encode(&self, encoder: &mut E) -> Result<(), E::Error> { + TokenStream::from(self.clone()).encode(encoder) + } +} + +impl Decodable for ThinTokenStream { + fn decode(decoder: &mut D) -> Result { + TokenStream::decode(decoder).map(Into::into) + } +} + +impl Hash for ThinTokenStream { + fn hash(&self, state: &mut H) { + TokenStream::from(self.clone()).hash(state); + } +} + #[cfg(test)] mod tests { diff --git a/src/libsyntax/util/rc_slice.rs b/src/libsyntax/util/rc_slice.rs index cb3becf83f682..195fb23f9d8c7 100644 --- a/src/libsyntax/util/rc_slice.rs +++ b/src/libsyntax/util/rc_slice.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::hash::{self, Hash}; use std::fmt; use std::ops::Deref; use std::rc::Rc; @@ -37,12 +36,6 @@ impl Deref for RcSlice { } } -impl Hash for RcSlice { - fn hash(&self, state: &mut H) { - self.deref().hash(state); - } -} - impl fmt::Debug for RcSlice { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(self.deref(), f) diff --git a/src/libsyntax_ext/asm.rs b/src/libsyntax_ext/asm.rs index a5e083f926a07..767ec94a0ce61 100644 --- a/src/libsyntax_ext/asm.rs +++ b/src/libsyntax_ext/asm.rs @@ -107,7 +107,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, if p2.token != token::Eof { let mut extra_tts = panictry!(p2.parse_all_token_trees()); extra_tts.extend(tts[first_colon..].iter().cloned()); - p = parse::tts_to_parser(cx.parse_sess, extra_tts); + p = parse::stream_to_parser(cx.parse_sess, extra_tts.into_iter().collect()); } asm = s; From a02c18aa524cf330237ec9d8dba202ad91904a88 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Tue, 21 Feb 2017 12:04:45 +0000 Subject: [PATCH 11/29] Fix `token::Eof` spans. --- src/libsyntax/parse/parser.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index c88b859e036d4..6e3724b5fd87b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -223,7 +223,7 @@ impl TokenCursor { self.frame = frame; continue } else { - return TokenAndSpan { tok: token::Eof, sp: self.frame.span } + return TokenAndSpan { tok: token::Eof, sp: syntax_pos::DUMMY_SP } }; match tree { @@ -448,10 +448,14 @@ impl<'a> Parser<'a> { } fn next_tok(&mut self) -> TokenAndSpan { - match self.desugar_doc_comments { + let mut next = match self.desugar_doc_comments { true => self.token_cursor.next_desugared(), false => self.token_cursor.next(), + }; + if next.sp == syntax_pos::DUMMY_SP { + next.sp = self.prev_span; } + next } /// Convert a token to a string using self's reader From 0d554139ad7a54bbd59a5166cc3e9ff7842c5266 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Thu, 2 Mar 2017 01:29:40 +0000 Subject: [PATCH 12/29] Fix fallout in unit tests. --- src/libsyntax/parse/mod.rs | 58 +++++++++---------- src/libsyntax/tokenstream.rs | 14 +---- src/libsyntax/util/parser_testing.rs | 8 +-- .../run-pass-fulldeps/ast_stmt_expr_attr.rs | 2 +- .../auxiliary/cond_plugin.rs | 8 +-- .../auxiliary/plugin_args.rs | 4 +- .../auxiliary/procedural_mbe_matching.rs | 4 +- 7 files changed, 45 insertions(+), 53 deletions(-) diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 7207463e1b9ab..c00d2952b3b42 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -598,7 +598,6 @@ pub fn integer_lit(s: &str, suffix: Option, sd: &Handler, sp: Span) -> a #[cfg(test)] mod tests { use super::*; - use std::rc::Rc; use syntax_pos::{self, Span, BytePos, Pos, NO_EXPANSION}; use codemap::Spanned; use ast::{self, Ident, PatKind}; @@ -609,7 +608,7 @@ mod tests { use print::pprust::item_to_string; use ptr::P; use tokenstream::{self, TokenTree}; - use util::parser_testing::{string_to_tts, string_to_parser}; + use util::parser_testing::{string_to_stream, string_to_parser}; use util::parser_testing::{string_to_expr, string_to_item, string_to_stmt}; use util::ThinVec; @@ -654,7 +653,8 @@ mod tests { // check the token-tree-ization of macros #[test] fn string_to_tts_macro () { - let tts = string_to_tts("macro_rules! zip (($a)=>($a))".to_string()); + let tts: Vec<_> = + string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect(); let tts: &[TokenTree] = &tts[..]; match (tts.len(), tts.get(0), tts.get(1), tts.get(2), tts.get(3)) { @@ -667,7 +667,7 @@ mod tests { ) if name_macro_rules.name == "macro_rules" && name_zip.name == "zip" => { - let tts = ¯o_delimed.tts[..]; + let tts = ¯o_delimed.stream().trees().collect::>(); match (tts.len(), tts.get(0), tts.get(1), tts.get(2)) { ( 3, @@ -676,7 +676,7 @@ mod tests { Some(&TokenTree::Delimited(_, ref second_delimed)), ) if macro_delimed.delim == token::Paren => { - let tts = &first_delimed.tts[..]; + let tts = &first_delimed.stream().trees().collect::>(); match (tts.len(), tts.get(0), tts.get(1)) { ( 2, @@ -684,9 +684,9 @@ mod tests { Some(&TokenTree::Token(_, token::Ident(ident))), ) if first_delimed.delim == token::Paren && ident.name == "a" => {}, - _ => panic!("value 3: {:?}", **first_delimed), + _ => panic!("value 3: {:?}", *first_delimed), } - let tts = &second_delimed.tts[..]; + let tts = &second_delimed.stream().trees().collect::>(); match (tts.len(), tts.get(0), tts.get(1)) { ( 2, @@ -695,10 +695,10 @@ mod tests { ) if second_delimed.delim == token::Paren && ident.name == "a" => {}, - _ => panic!("value 4: {:?}", **second_delimed), + _ => panic!("value 4: {:?}", *second_delimed), } }, - _ => panic!("value 2: {:?}", **macro_delimed), + _ => panic!("value 2: {:?}", *macro_delimed), } }, _ => panic!("value: {:?}",tts), @@ -707,31 +707,31 @@ mod tests { #[test] fn string_to_tts_1() { - let tts = string_to_tts("fn a (b : i32) { b; }".to_string()); + let tts = string_to_stream("fn a (b : i32) { b; }".to_string()); - let expected = vec![ - TokenTree::Token(sp(0, 2), token::Ident(Ident::from_str("fn"))), - TokenTree::Token(sp(3, 4), token::Ident(Ident::from_str("a"))), + let expected = TokenStream::concat(vec![ + TokenTree::Token(sp(0, 2), token::Ident(Ident::from_str("fn"))).into(), + TokenTree::Token(sp(3, 4), token::Ident(Ident::from_str("a"))).into(), TokenTree::Delimited( sp(5, 14), - Rc::new(tokenstream::Delimited { + tokenstream::Delimited { delim: token::DelimToken::Paren, - tts: vec![ - TokenTree::Token(sp(6, 7), token::Ident(Ident::from_str("b"))), - TokenTree::Token(sp(8, 9), token::Colon), - TokenTree::Token(sp(10, 13), token::Ident(Ident::from_str("i32"))), - ], - })), + tts: TokenStream::concat(vec![ + TokenTree::Token(sp(6, 7), token::Ident(Ident::from_str("b"))).into(), + TokenTree::Token(sp(8, 9), token::Colon).into(), + TokenTree::Token(sp(10, 13), token::Ident(Ident::from_str("i32"))).into(), + ]).into(), + }).into(), TokenTree::Delimited( sp(15, 21), - Rc::new(tokenstream::Delimited { + tokenstream::Delimited { delim: token::DelimToken::Brace, - tts: vec![ - TokenTree::Token(sp(17, 18), token::Ident(Ident::from_str("b"))), - TokenTree::Token(sp(18, 19), token::Semi), - ], - })) - ]; + tts: TokenStream::concat(vec![ + TokenTree::Token(sp(17, 18), token::Ident(Ident::from_str("b"))).into(), + TokenTree::Token(sp(18, 19), token::Semi).into(), + ]).into(), + }).into() + ]); assert_eq!(tts, expected); } @@ -974,8 +974,8 @@ mod tests { let expr = parse::parse_expr_from_source_str("foo".to_string(), "foo!( fn main() { body } )".to_string(), &sess).unwrap(); - let tts = match expr.node { - ast::ExprKind::Mac(ref mac) => mac.node.tts.clone(), + let tts: Vec<_> = match expr.node { + ast::ExprKind::Mac(ref mac) => mac.node.stream().trees().collect(), _ => panic!("not a macro"), }; diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index b7728609acaa5..2da442a1a53da 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -409,10 +409,10 @@ mod tests { use syntax::ast::Ident; use syntax_pos::{Span, BytePos, NO_EXPANSION}; use parse::token::Token; - use util::parser_testing::string_to_tts; + use util::parser_testing::string_to_stream; fn string_to_ts(string: &str) -> TokenStream { - string_to_tts(string.to_owned()).into_iter().collect() + string_to_stream(string.to_owned()) } fn sp(a: u32, b: u32) -> Span { @@ -428,20 +428,12 @@ mod tests { let test_res = string_to_ts("foo::bar::baz"); let test_fst = string_to_ts("foo::bar"); let test_snd = string_to_ts("::baz"); - let eq_res = TokenStream::concat([test_fst, test_snd].iter().cloned()); + let eq_res = TokenStream::concat(vec![test_fst, test_snd]); assert_eq!(test_res.trees().count(), 5); assert_eq!(eq_res.trees().count(), 5); assert_eq!(test_res.eq_unspanned(&eq_res), true); } - #[test] - fn test_from_to_bijection() { - let test_start = string_to_tts("foo::bar(baz)".to_string()); - let ts = test_start.iter().cloned().collect::(); - let test_end: Vec = ts.trees().collect(); - assert_eq!(test_start, test_end) - } - #[test] fn test_to_from_bijection() { let test_start = string_to_ts("foo::bar(baz)"); diff --git a/src/libsyntax/util/parser_testing.rs b/src/libsyntax/util/parser_testing.rs index e703dc6b4191c..51eb295b502a7 100644 --- a/src/libsyntax/util/parser_testing.rs +++ b/src/libsyntax/util/parser_testing.rs @@ -9,17 +9,17 @@ // except according to those terms. use ast::{self, Ident}; -use parse::{ParseSess,PResult,filemap_to_tts}; +use parse::{ParseSess, PResult, filemap_to_stream}; use parse::{lexer, new_parser_from_source_str}; use parse::parser::Parser; use ptr::P; -use tokenstream; +use tokenstream::TokenStream; use std::iter::Peekable; /// Map a string to tts, using a made-up filename: -pub fn string_to_tts(source_str: String) -> Vec { +pub fn string_to_stream(source_str: String) -> TokenStream { let ps = ParseSess::new(); - filemap_to_tts(&ps, ps.codemap().new_filemap("bogofile".to_string(), None, source_str)) + filemap_to_stream(&ps, ps.codemap().new_filemap("bogofile".to_string(), None, source_str)) } /// Map string to parser (via tts) diff --git a/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs b/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs index a41b34f6a53d0..5139b68bce7fd 100644 --- a/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs +++ b/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs @@ -18,7 +18,7 @@ use syntax::ast::*; use syntax::attr::*; use syntax::ast; use syntax::parse; -use syntax::parse::{ParseSess,filemap_to_tts, PResult}; +use syntax::parse::{ParseSess, PResult}; use syntax::parse::new_parser_from_source_str; use syntax::parse::parser::Parser; use syntax::parse::token; diff --git a/src/test/run-pass-fulldeps/auxiliary/cond_plugin.rs b/src/test/run-pass-fulldeps/auxiliary/cond_plugin.rs index 730e112c70016..2f94a440e72da 100644 --- a/src/test/run-pass-fulldeps/auxiliary/cond_plugin.rs +++ b/src/test/run-pass-fulldeps/auxiliary/cond_plugin.rs @@ -32,13 +32,13 @@ pub fn plugin_registrar(reg: &mut Registry) { fn cond(input: TokenStream) -> TokenStream { let mut conds = Vec::new(); - let mut input = input.trees(); + let mut input = input.trees().peekable(); while let Some(tree) = input.next() { - let cond: TokenStream = match *tree { - TokenTree::Delimited(_, ref delimited) => delimited.tts.iter().cloned().collect(), + let mut cond = match tree { + TokenTree::Delimited(_, ref delimited) => delimited.stream(), _ => panic!("Invalid input"), }; - let mut trees = cond.trees().cloned(); + let mut trees = cond.trees(); let test = trees.next(); let rhs = trees.collect::(); if rhs.is_empty() { diff --git a/src/test/run-pass-fulldeps/auxiliary/plugin_args.rs b/src/test/run-pass-fulldeps/auxiliary/plugin_args.rs index 3c8868f1664e8..134e36c587bed 100644 --- a/src/test/run-pass-fulldeps/auxiliary/plugin_args.rs +++ b/src/test/run-pass-fulldeps/auxiliary/plugin_args.rs @@ -26,7 +26,7 @@ use syntax::print::pprust; use syntax::ptr::P; use syntax::symbol::Symbol; use syntax_pos::Span; -use syntax::tokenstream; +use syntax::tokenstream::TokenStream; use rustc_plugin::Registry; struct Expander { @@ -37,7 +37,7 @@ impl TTMacroExpander for Expander { fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, sp: Span, - _: &[tokenstream::TokenTree]) -> Box { + _: TokenStream) -> Box { let args = self.args.iter().map(|i| pprust::meta_list_item_to_string(i)) .collect::>().join(", "); MacEager::expr(ecx.expr_str(sp, Symbol::intern(&args))) diff --git a/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs b/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs index 3db69f2167cc6..c9fa96b83c280 100644 --- a/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs +++ b/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs @@ -35,8 +35,8 @@ fn expand_mbe_matches(cx: &mut ExtCtxt, _: Span, args: &[TokenTree]) -> Box { let mbe_matcher = quote_tokens!(cx, $$matched:expr, $$($$pat:pat)|+); - let mbe_matcher = quoted::parse(&mbe_matcher, true, cx.parse_sess); - let map = match TokenTree::parse(cx, &mbe_matcher, args) { + let mbe_matcher = quoted::parse(mbe_matcher.into_iter().collect(), true, cx.parse_sess); + let map = match TokenTree::parse(cx, &mbe_matcher, args.iter().cloned().collect()) { Success(map) => map, Failure(_, tok) => { panic!("expected Success, but got Failure: {}", parse_failure_msg(tok)); From 44a01b8a54b078d15620d1133b94ee21ee7a6915 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 15 Feb 2017 15:57:06 -0800 Subject: [PATCH 13/29] rustbuild: Add support for compiling Cargo This commit adds support to rustbuild for compiling Cargo as part of the release process. Previously rustbuild would simply download a Cargo snapshot and repackage it. With this change we should be able to turn off artifacts from the rust-lang/cargo repository and purely rely on the artifacts Cargo produces here. The infrastructure added here is intended to be extensible to other components, such as the RLS. It won't exactly be a one-line addition, but the addition of Cargo didn't require too much hooplah anyway. The process for release Cargo will now look like: * The rust-lang/rust repository has a Cargo submodule which is used to build a Cargo to pair with the rust-lang/rust release * Periodically we'll update the cargo submodule as necessary on rust-lang/rust's master branch * When branching beta we'll create a new branch of Cargo (as we do today), and the first commit to the beta branch will be to update the Cargo submodule to this exact revision. * When branching stable, we'll ensure that the Cargo submodule is updated and then make a stable release. Backports to Cargo will look like: * Send a PR to cargo's master branch * Send a PR to cargo's release branch (e.g. rust-1.16.0) * Send a PR to rust-lang/rust's beta branch updating the submodule * Eventually send a PR to rust-lang/rust's master branch updating the submodule For reference, the process to add a new component to the rust-lang/rust release would look like: * Add `$foo` as a submodule in `src/tools` * Add a `tool-$foo` step which compiles `$foo` with the specified compiler, likely mirroring what Cargo does. * Add a `dist-$foo` step which uses `src/tools/$foo` and the `tool-$foo` output to create a rust-installer package for `$foo` likely mirroring what Cargo does. * Update the `dist-extended` step with a new dependency on `dist-$foo` * Update `src/tools/build-manifest` for the new component. --- configure | 1 + src/Cargo.lock | 84 +++++++++++++--- src/bootstrap/channel.rs | 95 ++++++++++--------- src/bootstrap/compile.rs | 34 +++++-- src/bootstrap/config.rs | 4 + src/bootstrap/config.toml.example | 5 + src/bootstrap/dist.rs | 134 ++++++++++++++------------ src/bootstrap/doc.rs | 9 +- src/bootstrap/install.rs | 4 +- src/bootstrap/lib.rs | 137 ++++++++++++++++++++++----- src/bootstrap/metadata.rs | 2 + src/bootstrap/native.rs | 93 ++++++++++++++++++ src/bootstrap/step.rs | 30 +++++- src/build_helper/lib.rs | 18 ++++ src/ci/run.sh | 1 + src/tools/build-manifest/src/main.rs | 19 ++-- src/tools/cargo | 2 +- src/tools/tidy/src/cargo.rs | 2 +- src/tools/tidy/src/deps.rs | 2 + src/tools/tidy/src/main.rs | 1 + 20 files changed, 511 insertions(+), 166 deletions(-) diff --git a/configure b/configure index 70952438a3559..be8628de62832 100755 --- a/configure +++ b/configure @@ -651,6 +651,7 @@ opt locked-deps 0 "force Cargo.lock to be up to date" opt vendor 0 "enable usage of vendored Rust crates" opt sanitizers 0 "build the sanitizer runtimes (asan, lsan, msan, tsan)" opt dist-src 1 "when building tarballs enables building a source tarball" +opt cargo-openssl-static 0 "static openssl in cargo" # Optimization and debugging options. These may be overridden by the release channel, etc. opt_nosave optimize 1 "build optimized rust code" diff --git a/src/Cargo.lock b/src/Cargo.lock index 9a4fd3a1d5594..c463f2bb747cc 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -140,11 +140,15 @@ dependencies = [ "psapi-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_ignored 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", "shell-escape 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -163,6 +167,8 @@ dependencies = [ "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -238,7 +244,9 @@ name = "crates-io" version = "0.7.0" dependencies = [ "curl 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -784,6 +792,11 @@ name = "quick-error" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "quote" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rand" version = "0.0.0" @@ -1222,6 +1235,32 @@ name = "serde" version = "0.9.7" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde_codegen_internals" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syn 0.11.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_codegen_internals 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_ignored" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde_json" version = "0.9.7" @@ -1278,6 +1317,24 @@ name = "strsim" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "syn" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "synom" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syntax" version = "0.0.0" @@ -1404,14 +1461,6 @@ dependencies = [ "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "toml" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "toml" version = "0.3.0" @@ -1443,6 +1492,11 @@ name = "unicode-width" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-xid" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unreachable" version = "0.1.1" @@ -1547,8 +1601,8 @@ dependencies = [ "checksum libssh2-sys 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "91e135645c2e198a39552c8c7686bb5b83b1b99f64831c040a6c2798a1195934" "checksum libz-sys 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "e5ee912a45d686d393d5ac87fac15ba0ba18daae14e8e7543c63ebf7fb7e970c" "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" -"checksum mdbook 0.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "dbba458ca886cb082d026afd704eeeeb0531f7e4ffd6c619f72dc309c1c18fe4" "checksum matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efd7622e3022e1a6eaa602c4cea8912254e5582c9c692e9167714182244801b1" +"checksum mdbook 0.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "dbba458ca886cb082d026afd704eeeeb0531f7e4ffd6c619f72dc309c1c18fe4" "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" "checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4" "checksum miniz-sys 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "28eaee17666671fa872e567547e8428e83308ebe5808cdf6a0e28397dbe2c726" @@ -1572,6 +1626,7 @@ dependencies = [ "checksum psapi-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "abcd5d1a07d360e29727f757a9decb3ce8bc6e0efa8969cfaad669a8317a2478" "checksum pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1058d7bb927ca067656537eec4e02c2b4b70eaaa129664c5b90c111e20326f41" "checksum quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0aad603e8d7fb67da22dbdf1f4b826ce8829e406124109e73cf1b2454b93a71c" +"checksum quote 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7375cf7ad34a92e8fd18dd9c42f58b9a11def59ab48bec955bf359a788335592" "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" "checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01" @@ -1581,9 +1636,14 @@ dependencies = [ "checksum semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a3186ec9e65071a2095434b1f5bb24838d4e8e130f584c790f6033c79943537" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1e0ed773960f90a78567fcfbe935284adf50c5d7cf119aa2cf43bb0b4afa69bb" +"checksum serde_codegen_internals 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d52006899f910528a10631e5b727973fe668f3228109d1707ccf5bad5490b6e" +"checksum serde_derive 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)" = "789ee9f3cd78c850948b94121020147f5220b47dafbf230d7098a93a58f726cf" +"checksum serde_ignored 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4b3f5576874721d14690657e9f0ed286e72a52be2f6fdc0cf2f024182bd8f64" "checksum serde_json 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "2eb96d30e4e6f9fc52e08f51176d078b6f79b981dc3ed4134f7b850be9f446a8" "checksum shell-escape 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "dd5cc96481d54583947bfe88bf30c23d53f883c6cd0145368b69989d97b84ef8" "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" +"checksum syn 0.11.8 (registry+https://github.com/rust-lang/crates.io-index)" = "37c279fb816210c9bb28b2c292664581e7b87b4561e86b94df462664d8620bb8" +"checksum synom 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "27e31aa4b09b9f4cb12dff3c30ba503e17b1a624413d764d32dab76e3920e5bc" "checksum tar 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "1eb3bf6ec92843ca93f4fcfb5fc6dfe30534815b147885db4b5759b8e2ff7d52" "checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" "checksum term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d168af3930b369cfe245132550579d47dfd873d69470755a19c2c6568dbbd989" @@ -1593,12 +1653,12 @@ dependencies = [ "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" "checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7" "checksum toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "0590d72182e50e879c4da3b11c6488dae18fccb1ae0c7a3eda18e16795844796" -"checksum toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "736b60249cb25337bc196faa43ee12c705e426f3d55c214d73a4e7be06f92cb4" "checksum toml 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "08272367dd2e766db3fa38f068067d17aa6a9dfd7259af24b3927db92f1e0c2f" "checksum unicode-bidi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a078ebdd62c0e71a709c3d53d2af693fe09fe93fbff8344aebe289b78f9032" "checksum unicode-normalization 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e28fa37426fceeb5cf8f41ee273faa7c82c47dc8fba5853402841e665fcd86ff" "checksum unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18127285758f0e2c6cf325bb3f3d138a12fee27de4f23e146cd6a179f26c2cf3" "checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" +"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" "checksum url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f5ba8a749fb4479b043733416c244fa9d1d3af3d7c23804944651c8a448cb87e" "checksum user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ef4711d107b21b410a3a974b1204d9accc8b10dad75d8324b5d755de1617d47" diff --git a/src/bootstrap/channel.rs b/src/bootstrap/channel.rs index 81e745bc76c9e..c126c076a3d4e 100644 --- a/src/bootstrap/channel.rs +++ b/src/bootstrap/channel.rs @@ -15,6 +15,7 @@ //! `package_vers`, and otherwise indicating to the compiler what it should //! print out as part of its version information. +use std::path::Path; use std::process::Command; use build_helper::output; @@ -22,65 +23,69 @@ use build_helper::output; use Build; // The version number -const CFG_RELEASE_NUM: &'static str = "1.17.0"; +pub const CFG_RELEASE_NUM: &'static str = "1.17.0"; // An optional number to put after the label, e.g. '.2' -> '-beta.2' // Be sure to make this starts with a dot to conform to semver pre-release // versions (section 9) -const CFG_PRERELEASE_VERSION: &'static str = ".1"; +pub const CFG_PRERELEASE_VERSION: &'static str = ".1"; -pub fn collect(build: &mut Build) { - build.release_num = CFG_RELEASE_NUM.to_string(); - build.prerelease_version = CFG_RELEASE_NUM.to_string(); +pub struct GitInfo { + inner: Option, +} - // Depending on the channel, passed in `./configure --release-channel`, - // determine various properties of the build. - match &build.config.channel[..] { - "stable" => { - build.release = CFG_RELEASE_NUM.to_string(); - build.package_vers = build.release.clone(); - build.unstable_features = false; - } - "beta" => { - build.release = format!("{}-beta{}", CFG_RELEASE_NUM, - CFG_PRERELEASE_VERSION); - build.package_vers = "beta".to_string(); - build.unstable_features = false; - } - "nightly" => { - build.release = format!("{}-nightly", CFG_RELEASE_NUM); - build.package_vers = "nightly".to_string(); - build.unstable_features = true; - } - _ => { - build.release = format!("{}-dev", CFG_RELEASE_NUM); - build.package_vers = build.release.clone(); - build.unstable_features = true; - } - } - build.version = build.release.clone(); +struct Info { + commit_date: String, + sha: String, + short_sha: String, +} - // If we have a git directory, add in some various SHA information of what - // commit this compiler was compiled from. - if build.src.join(".git").is_dir() { - let ver_date = output(Command::new("git").current_dir(&build.src) +impl GitInfo { + pub fn new(dir: &Path) -> GitInfo { + if !dir.join(".git").is_dir() { + return GitInfo { inner: None } + } + let ver_date = output(Command::new("git").current_dir(dir) .arg("log").arg("-1") .arg("--date=short") .arg("--pretty=format:%cd")); - let ver_hash = output(Command::new("git").current_dir(&build.src) + let ver_hash = output(Command::new("git").current_dir(dir) .arg("rev-parse").arg("HEAD")); let short_ver_hash = output(Command::new("git") - .current_dir(&build.src) + .current_dir(dir) .arg("rev-parse") .arg("--short=9") .arg("HEAD")); - let ver_date = ver_date.trim().to_string(); - let ver_hash = ver_hash.trim().to_string(); - let short_ver_hash = short_ver_hash.trim().to_string(); - build.version.push_str(&format!(" ({} {})", short_ver_hash, - ver_date)); - build.ver_date = Some(ver_date.to_string()); - build.ver_hash = Some(ver_hash); - build.short_ver_hash = Some(short_ver_hash); + GitInfo { + inner: Some(Info { + commit_date: ver_date.trim().to_string(), + sha: ver_hash.trim().to_string(), + short_sha: short_ver_hash.trim().to_string(), + }), + } + } + + pub fn sha(&self) -> Option<&str> { + self.inner.as_ref().map(|s| &s.sha[..]) + } + + pub fn sha_short(&self) -> Option<&str> { + self.inner.as_ref().map(|s| &s.short_sha[..]) + } + + pub fn commit_date(&self) -> Option<&str> { + self.inner.as_ref().map(|s| &s.commit_date[..]) + } + + pub fn version(&self, build: &Build, num: &str) -> String { + let mut version = build.release(num); + if let Some(ref inner) = self.inner { + version.push_str(" ("); + version.push_str(&inner.short_sha); + version.push_str(" "); + version.push_str(&inner.commit_date); + version.push_str(")"); + } + return version } } diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index cea8b13366657..46d8d4b4aab2d 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -24,6 +24,7 @@ use std::process::Command; use build_helper::{output, mtime, up_to_date}; use filetime::FileTime; +use channel::GitInfo; use util::{exe, libdir, is_dylib, copy}; use {Build, Compiler, Mode}; @@ -210,9 +211,9 @@ pub fn rustc(build: &Build, target: &str, compiler: &Compiler) { // Set some configuration variables picked up by build scripts and // the compiler alike - cargo.env("CFG_RELEASE", &build.release) + cargo.env("CFG_RELEASE", build.rust_release()) .env("CFG_RELEASE_CHANNEL", &build.config.channel) - .env("CFG_VERSION", &build.version) + .env("CFG_VERSION", build.rust_version()) .env("CFG_PREFIX", build.config.prefix.clone().unwrap_or(PathBuf::new())); if compiler.stage == 0 { @@ -229,13 +230,13 @@ pub fn rustc(build: &Build, target: &str, compiler: &Compiler) { cargo.env_remove("RUSTC_DEBUGINFO_LINES"); } - if let Some(ref ver_date) = build.ver_date { + if let Some(ref ver_date) = build.rust_info.commit_date() { cargo.env("CFG_VER_DATE", ver_date); } - if let Some(ref ver_hash) = build.ver_hash { + if let Some(ref ver_hash) = build.rust_info.sha() { cargo.env("CFG_VER_HASH", ver_hash); } - if !build.unstable_features { + if !build.unstable_features() { cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1"); } // Flag that rust llvm is in use @@ -416,13 +417,32 @@ pub fn tool(build: &Build, stage: u32, target: &str, tool: &str) { // build.clear_if_dirty(&out_dir, &libstd_stamp(build, stage, &host, target)); let mut cargo = build.cargo(&compiler, Mode::Tool, target, "build"); - cargo.arg("--manifest-path") - .arg(build.src.join(format!("src/tools/{}/Cargo.toml", tool))); + let dir = build.src.join("src/tools").join(tool); + cargo.arg("--manifest-path").arg(dir.join("Cargo.toml")); // We don't want to build tools dynamically as they'll be running across // stages and such and it's just easier if they're not dynamically linked. cargo.env("RUSTC_NO_PREFER_DYNAMIC", "1"); + if let Some(dir) = build.openssl_install_dir(target) { + cargo.env("OPENSSL_STATIC", "1"); + cargo.env("OPENSSL_DIR", dir); + cargo.env("LIBZ_SYS_STATIC", "1"); + } + + cargo.env("CFG_RELEASE_CHANNEL", &build.config.channel); + + let info = GitInfo::new(&dir); + if let Some(sha) = info.sha() { + cargo.env("CFG_COMMIT_HASH", sha); + } + if let Some(sha_short) = info.sha_short() { + cargo.env("CFG_SHORT_COMMIT_HASH", sha_short); + } + if let Some(date) = info.commit_date() { + cargo.env("CFG_COMMIT_DATE", date); + } + build.run(&mut cargo); } diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index 8308dc3202fff..438ce6103d624 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -106,6 +106,7 @@ pub struct Config { pub gdb: Option, pub python: Option, pub configure_args: Vec, + pub openssl_static: bool, } /// Per-target configuration stored in the global configuration structure. @@ -155,6 +156,7 @@ struct Build { extended: Option, verbose: Option, sanitizers: Option, + openssl_static: Option, } /// TOML representation of various global install decisions. @@ -305,6 +307,7 @@ impl Config { set(&mut config.extended, build.extended); set(&mut config.verbose, build.verbose); set(&mut config.sanitizers, build.sanitizers); + set(&mut config.openssl_static, build.openssl_static); if let Some(ref install) = toml.install { config.prefix = install.prefix.clone().map(PathBuf::from); @@ -453,6 +456,7 @@ impl Config { ("EXTENDED", self.extended), ("SANITIZERS", self.sanitizers), ("DIST_SRC", self.rust_dist_src), + ("CARGO_OPENSSL_STATIC", self.openssl_static), } match key { diff --git a/src/bootstrap/config.toml.example b/src/bootstrap/config.toml.example index f95e890f346ee..30763e38a336f 100644 --- a/src/bootstrap/config.toml.example +++ b/src/bootstrap/config.toml.example @@ -134,6 +134,11 @@ # Build the sanitizer runtimes #sanitizers = false +# Indicates whether the OpenSSL linked into Cargo will be statically linked or +# not. If static linkage is specified then the build system will download a +# known-good version of OpenSSL, compile it, and link it to Cargo. +#openssl-static = false + # ============================================================================= # General install configuration options # ============================================================================= diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index c468d4896a614..67e4dad83ce88 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -33,19 +33,12 @@ const SH_CMD: &'static str = "sh"; const SH_CMD: &'static str = "bash"; use {Build, Compiler, Mode}; -use util::{cp_r, libdir, is_dylib, cp_filtered, copy}; - -pub fn package_vers(build: &Build) -> &str { - match &build.config.channel[..] { - "stable" => &build.release, - "beta" => "beta", - "nightly" => "nightly", - _ => &build.release, - } -} +use channel; +use util::{cp_r, libdir, is_dylib, cp_filtered, copy, exe}; fn pkgname(build: &Build, component: &str) -> String { - format!("{}-{}", component, package_vers(build)) + assert!(component.starts_with("rust")); // does not work with cargo + format!("{}-{}", component, build.rust_package_vers()) } fn distdir(build: &Build) -> PathBuf { @@ -93,7 +86,7 @@ pub fn docs(build: &Build, stage: u32, host: &str) { // As part of this step, *also* copy the docs directory to a directory which // buildbot typically uploads. if host == build.config.build { - let dst = distdir(build).join("doc").join(&build.package_vers); + let dst = distdir(build).join("doc").join(build.rust_package_vers()); t!(fs::create_dir_all(&dst)); cp_r(&src, &dst); } @@ -162,7 +155,7 @@ pub fn rustc(build: &Build, stage: u32, host: &str) { cp("LICENSE-MIT"); cp("README.md"); // tiny morsel of metadata is used by rust-packaging - let version = &build.version; + let version = build.rust_version(); t!(t!(File::create(overlay.join("version"))).write_all(version.as_bytes())); // On MinGW we've got a few runtime DLL dependencies that we need to @@ -312,7 +305,7 @@ pub fn std(build: &Build, compiler: &Compiler, target: &str) { } pub fn rust_src_location(build: &Build) -> PathBuf { - let plain_name = format!("rustc-{}-src", package_vers(build)); + let plain_name = format!("rustc-{}-src", build.rust_package_vers()); distdir(build).join(&format!("{}.tar.gz", plain_name)) } @@ -477,14 +470,14 @@ pub fn rust_src(build: &Build) { build.run(&mut cmd); // Rename directory, so that root folder of tarball has the correct name - let plain_name = format!("rustc-{}-src", package_vers(build)); + let plain_name = format!("rustc-{}-src", build.rust_package_vers()); let plain_dst_src = tmpdir(build).join(&plain_name); let _ = fs::remove_dir_all(&plain_dst_src); t!(fs::create_dir_all(&plain_dst_src)); cp_r(&dst_src, &plain_dst_src); // Create the version file - write_file(&plain_dst_src.join("version"), build.version.as_bytes()); + write_file(&plain_dst_src.join("version"), build.rust_version().as_bytes()); // Create plain source tarball let mut cmd = Command::new("tar"); @@ -536,43 +529,64 @@ fn write_file(path: &Path, data: &[u8]) { t!(vf.write_all(data)); } -// FIXME(#38531) eventually this should package up a Cargo that we just compiled -// and tested locally, but for now we're downloading cargo -// artifacts from their compiled location. pub fn cargo(build: &Build, stage: u32, target: &str) { println!("Dist cargo stage{} ({})", stage, target); + let compiler = Compiler::new(stage, &build.config.build); - let branch = match &build.config.channel[..] { - "stable" | - "beta" => format!("rust-{}", build.release_num), - _ => "master".to_string(), - }; + let src = build.src.join("src/tools/cargo"); + let etc = src.join("src/etc"); + let release_num = &build.crates["cargo"].version; + let name = format!("cargo-{}", build.package_vers(release_num)); + let version = build.cargo_info.version(build, release_num); + + let tmp = tmpdir(build); + let image = tmp.join("cargo-image"); + drop(fs::remove_dir_all(&image)); + t!(fs::create_dir_all(&image)); - let dst = tmpdir(build).join("cargo"); - let _ = fs::remove_dir_all(&dst); - build.run(Command::new("git") - .arg("clone") - .arg("--depth").arg("1") - .arg("--branch").arg(&branch) - .arg("https://github.com/rust-lang/cargo") - .current_dir(dst.parent().unwrap())); - let sha = output(Command::new("git") - .arg("rev-parse") - .arg("HEAD") - .current_dir(&dst)); - let sha = sha.trim(); - println!("\tgot cargo sha: {}", sha); - - let input = format!("https://s3.amazonaws.com/rust-lang-ci/cargo-builds\ - /{}/cargo-nightly-{}.tar.gz", sha, target); - let output = distdir(build).join(format!("cargo-nightly-{}.tar.gz", target)); - println!("\tdownloading {}", input); - let mut curl = Command::new("curl"); - curl.arg("-f") - .arg("-o").arg(&output) - .arg(&input) - .arg("--retry").arg("3"); - build.run(&mut curl); + // Prepare the image directory + t!(fs::create_dir_all(image.join("share/zsh/site-functions"))); + t!(fs::create_dir_all(image.join("etc/bash_completions.d"))); + let cargo = build.cargo_out(&compiler, Mode::Tool, target) + .join(exe("cargo", target)); + install(&cargo, &image.join("bin"), 0o755); + for man in t!(etc.join("man").read_dir()) { + let man = t!(man); + install(&man.path(), &image.join("share/man/man1"), 0o644); + } + install(&etc.join("_cargo"), &image.join("share/zsh/site-functions"), 0o644); + copy(&etc.join("cargo.bashcomp.sh"), + &image.join("etc/bash_completions.d/cargo")); + let doc = image.join("share/doc/cargo"); + install(&src.join("README.md"), &doc, 0o644); + install(&src.join("LICENSE-MIT"), &doc, 0o644); + install(&src.join("LICENSE-APACHE"), &doc, 0o644); + install(&src.join("LICENSE-THIRD-PARTY"), &doc, 0o644); + + // Prepare the overlay + let overlay = tmp.join("cargo-overlay"); + drop(fs::remove_dir_all(&overlay)); + t!(fs::create_dir_all(&overlay)); + install(&src.join("README.md"), &overlay, 0o644); + install(&src.join("LICENSE-MIT"), &overlay, 0o644); + install(&src.join("LICENSE-APACHE"), &overlay, 0o644); + install(&src.join("LICENSE-THIRD-PARTY"), &overlay, 0o644); + t!(t!(File::create(overlay.join("version"))).write_all(version.as_bytes())); + + // Generate the installer tarball + let mut cmd = Command::new("sh"); + cmd.arg(sanitize_sh(&build.src.join("src/rust-installer/gen-installer.sh"))) + .arg("--product-name=Rust") + .arg("--rel-manifest-dir=rustlib") + .arg("--success-message=Rust-is-ready-to-roll.") + .arg(format!("--image-dir={}", sanitize_sh(&image))) + .arg(format!("--work-dir={}", sanitize_sh(&tmpdir(build)))) + .arg(format!("--output-dir={}", sanitize_sh(&distdir(build)))) + .arg(format!("--non-installed-overlay={}", sanitize_sh(&overlay))) + .arg(format!("--package-name={}-{}", name, target)) + .arg("--component-name=cargo") + .arg("--legacy-manifest-dirs=rustlib,cargo"); + build.run(&mut cmd); } /// Creates a combined installer for the specified target in the provided stage. @@ -580,10 +594,13 @@ pub fn extended(build: &Build, stage: u32, target: &str) { println!("Dist extended stage{} ({})", stage, target); let dist = distdir(build); + let cargo_vers = &build.crates["cargo"].version; let rustc_installer = dist.join(format!("{}-{}.tar.gz", pkgname(build, "rustc"), target)); - let cargo_installer = dist.join(format!("cargo-nightly-{}.tar.gz", target)); + let cargo_installer = dist.join(format!("cargo-{}-{}.tar.gz", + build.package_vers(&cargo_vers), + target)); let docs_installer = dist.join(format!("{}-{}.tar.gz", pkgname(build, "rust-docs"), target)); @@ -603,7 +620,7 @@ pub fn extended(build: &Build, stage: u32, target: &str) { install(&build.src.join("COPYRIGHT"), &overlay, 0o644); install(&build.src.join("LICENSE-APACHE"), &overlay, 0o644); install(&build.src.join("LICENSE-MIT"), &overlay, 0o644); - let version = &build.version; + let version = build.rust_version(); t!(t!(File::create(overlay.join("version"))).write_all(version.as_bytes())); install(&etc.join("README.md"), &overlay, 0o644); @@ -876,16 +893,16 @@ pub fn extended(build: &Build, stage: u32, target: &str) { } fn add_env(build: &Build, cmd: &mut Command, target: &str) { - let mut parts = build.release_num.split('.'); - cmd.env("CFG_RELEASE_INFO", &build.version) - .env("CFG_RELEASE_NUM", &build.release_num) - .env("CFG_RELEASE", &build.release) - .env("CFG_PRERELEASE_VERSION", &build.prerelease_version) + let mut parts = channel::CFG_RELEASE_NUM.split('.'); + cmd.env("CFG_RELEASE_INFO", build.rust_version()) + .env("CFG_RELEASE_NUM", channel::CFG_RELEASE_NUM) + .env("CFG_RELEASE", build.rust_release()) + .env("CFG_PRERELEASE_VERSION", channel::CFG_PRERELEASE_VERSION) .env("CFG_VER_MAJOR", parts.next().unwrap()) .env("CFG_VER_MINOR", parts.next().unwrap()) .env("CFG_VER_PATCH", parts.next().unwrap()) .env("CFG_VER_BUILD", "0") // just needed to build - .env("CFG_PACKAGE_VERS", package_vers(build)) + .env("CFG_PACKAGE_VERS", build.rust_package_vers()) .env("CFG_PACKAGE_NAME", pkgname(build, "rust")) .env("CFG_BUILD", target) .env("CFG_CHANNEL", &build.config.channel); @@ -925,7 +942,8 @@ pub fn hash_and_sign(build: &Build) { cmd.arg(sign); cmd.arg(distdir(build)); cmd.arg(today.trim()); - cmd.arg(package_vers(build)); + cmd.arg(build.rust_package_vers()); + cmd.arg(build.cargo_info.version(build, &build.crates["cargo"].version)); cmd.arg(addr); t!(fs::create_dir_all(distdir(build))); diff --git a/src/bootstrap/doc.rs b/src/bootstrap/doc.rs index 5fd6570e1610c..d19e5b1b88456 100644 --- a/src/bootstrap/doc.rs +++ b/src/bootstrap/doc.rs @@ -77,12 +77,9 @@ pub fn standalone(build: &Build, target: &str) { if !up_to_date(&version_input, &version_info) { let mut info = String::new(); t!(t!(File::open(&version_input)).read_to_string(&mut info)); - let blank = String::new(); - let short = build.short_ver_hash.as_ref().unwrap_or(&blank); - let hash = build.ver_hash.as_ref().unwrap_or(&blank); - let info = info.replace("VERSION", &build.release) - .replace("SHORT_HASH", short) - .replace("STAMP", hash); + let info = info.replace("VERSION", &build.rust_release()) + .replace("SHORT_HASH", build.rust_info.sha_short().unwrap_or("")) + .replace("STAMP", build.rust_info.sha().unwrap_or("")); t!(t!(File::create(&version_info)).write_all(info.as_bytes())); } diff --git a/src/bootstrap/install.rs b/src/bootstrap/install.rs index efc460f35838c..ba8442ebd8c37 100644 --- a/src/bootstrap/install.rs +++ b/src/bootstrap/install.rs @@ -19,7 +19,7 @@ use std::path::{Path, PathBuf, Component}; use std::process::Command; use Build; -use dist::{package_vers, sanitize_sh, tmpdir}; +use dist::{sanitize_sh, tmpdir}; /// Installs everything. pub fn install(build: &Build, stage: u32, host: &str) { @@ -59,7 +59,7 @@ pub fn install(build: &Build, stage: u32, host: &str) { fn install_sh(build: &Build, package: &str, name: &str, stage: u32, host: &str, prefix: &Path, docdir: &Path, libdir: &Path, mandir: &Path, empty_dir: &Path) { println!("Install {} stage{} ({})", package, stage, host); - let package_name = format!("{}-{}-{}", name, package_vers(build), host); + let package_name = format!("{}-{}-{}", name, build.rust_package_vers(), host); let mut cmd = Command::new("sh"); cmd.current_dir(empty_dir) diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 2b34142b3b0f3..5215b281fac0b 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -84,7 +84,7 @@ use std::fs::{self, File}; use std::path::{Component, PathBuf, Path}; use std::process::Command; -use build_helper::{run_silent, output, mtime}; +use build_helper::{run_silent, run_suppressed, output, mtime}; use util::{exe, libdir, add_lib_path}; @@ -148,16 +148,9 @@ pub struct Build { rustc: PathBuf, src: PathBuf, out: PathBuf, - release: String, - unstable_features: bool, - ver_hash: Option, - short_ver_hash: Option, - ver_date: Option, - version: String, - package_vers: String, + rust_info: channel::GitInfo, + cargo_info: channel::GitInfo, local_rebuild: bool, - release_num: String, - prerelease_version: String, // Probed tools at runtime lldb_version: Option, @@ -173,6 +166,7 @@ pub struct Build { #[derive(Debug)] struct Crate { name: String, + version: String, deps: Vec, path: PathBuf, doc_step: String, @@ -236,6 +230,8 @@ impl Build { } None => false, }; + let rust_info = channel::GitInfo::new(&src); + let cargo_info = channel::GitInfo::new(&src.join("src/tools/cargo")); Build { flags: flags, @@ -245,22 +241,15 @@ impl Build { src: src, out: out, - release: String::new(), - unstable_features: false, - ver_hash: None, - short_ver_hash: None, - ver_date: None, - version: String::new(), + rust_info: rust_info, + cargo_info: cargo_info, local_rebuild: local_rebuild, - package_vers: String::new(), cc: HashMap::new(), cxx: HashMap::new(), crates: HashMap::new(), lldb_version: None, lldb_python_dir: None, is_sudo: is_sudo, - release_num: String::new(), - prerelease_version: String::new(), } } @@ -278,15 +267,14 @@ impl Build { cc::find(self); self.verbose("running sanity check"); sanity::check(self); - self.verbose("collecting channel variables"); - channel::collect(self); // If local-rust is the same major.minor as the current version, then force a local-rebuild let local_version_verbose = output( Command::new(&self.rustc).arg("--version").arg("--verbose")); let local_release = local_version_verbose .lines().filter(|x| x.starts_with("release:")) .next().unwrap().trim_left_matches("release:").trim(); - if local_release.split('.').take(2).eq(self.release.split('.').take(2)) { + let my_version = channel::CFG_RELEASE_NUM; + if local_release.split('.').take(2).eq(my_version.split('.').take(2)) { self.verbose(&format!("auto-detected local-rebuild {}", local_release)); self.local_rebuild = true; } @@ -478,10 +466,8 @@ impl Build { self.config.rust_codegen_units.to_string()) .env("RUSTC_DEBUG_ASSERTIONS", self.config.rust_debug_assertions.to_string()) - .env("RUSTC_SNAPSHOT", &self.rustc) .env("RUSTC_SYSROOT", self.sysroot(compiler)) .env("RUSTC_LIBDIR", self.rustc_libdir(compiler)) - .env("RUSTC_SNAPSHOT_LIBDIR", self.rustc_snapshot_libdir()) .env("RUSTC_RPATH", self.config.rust_rpath.to_string()) .env("RUSTDOC", self.out.join("bootstrap/debug/rustdoc")) .env("RUSTDOC_REAL", self.rustdoc(compiler)) @@ -491,6 +477,27 @@ impl Build { cargo.env("RUSTC_BOOTSTRAP", "1"); self.add_rust_test_threads(&mut cargo); + // Almost all of the crates that we compile as part of the bootstrap may + // have a build script, including the standard library. To compile a + // build script, however, it itself needs a standard library! This + // introduces a bit of a pickle when we're compiling the standard + // library itself. + // + // To work around this we actually end up using the snapshot compiler + // (stage0) for compiling build scripts of the standard library itself. + // The stage0 compiler is guaranteed to have a libstd available for use. + // + // For other crates, however, we know that we've already got a standard + // library up and running, so we can use the normal compiler to compile + // build scripts in that situation. + if let Mode::Libstd = mode { + cargo.env("RUSTC_SNAPSHOT", &self.rustc) + .env("RUSTC_SNAPSHOT_LIBDIR", self.rustc_snapshot_libdir()); + } else { + cargo.env("RUSTC_SNAPSHOT", self.compiler_path(compiler)) + .env("RUSTC_SNAPSHOT_LIBDIR", self.rustc_libdir(compiler)); + } + // Ignore incremental modes except for stage0, since we're // not guaranteeing correctness acros builds if the compiler // is changing under your feet.` @@ -797,6 +804,12 @@ impl Build { run_silent(cmd) } + /// Runs a command, printing out nice contextual information if it fails. + fn run_quiet(&self, cmd: &mut Command) { + self.verbose(&format!("running: {:?}", cmd)); + run_suppressed(cmd) + } + /// Prints a message if this build is configured in verbose mode. fn verbose(&self, msg: &str) { if self.flags.verbose() || self.config.verbose() { @@ -914,6 +927,82 @@ impl Build { compiler.stage >= 2 && self.config.host.iter().any(|h| h == target) } + + /// Returns the directory that OpenSSL artifacts are compiled into if + /// configured to do so. + fn openssl_dir(&self, target: &str) -> Option { + // OpenSSL not used on Windows + if target.contains("windows") { + None + } else if self.config.openssl_static { + Some(self.out.join(target).join("openssl")) + } else { + None + } + } + + /// Returns the directory that OpenSSL artifacts are installed into if + /// configured as such. + fn openssl_install_dir(&self, target: &str) -> Option { + self.openssl_dir(target).map(|p| p.join("install")) + } + + /// Given `num` in the form "a.b.c" return a "release string" which + /// describes the release version number. + /// + /// For example on nightly this returns "a.b.c-nightly", on beta it returns + /// "a.b.c-beta.1" and on stable it just returns "a.b.c". + fn release(&self, num: &str) -> String { + match &self.config.channel[..] { + "stable" => num.to_string(), + "beta" => format!("{}-beta{}", num, channel::CFG_PRERELEASE_VERSION), + "nightly" => format!("{}-nightly", num), + _ => format!("{}-dev", num), + } + } + + /// Returns the value of `release` above for Rust itself. + fn rust_release(&self) -> String { + self.release(channel::CFG_RELEASE_NUM) + } + + /// Returns the "package version" for a component given the `num` release + /// number. + /// + /// The package version is typically what shows up in the names of tarballs. + /// For channels like beta/nightly it's just the channel name, otherwise + /// it's the `num` provided. + fn package_vers(&self, num: &str) -> String { + match &self.config.channel[..] { + "stable" => num.to_string(), + "beta" => "beta".to_string(), + "nightly" => "nightly".to_string(), + _ => format!("{}-dev", num), + } + } + + /// Returns the value of `package_vers` above for Rust itself. + fn rust_package_vers(&self) -> String { + self.package_vers(channel::CFG_RELEASE_NUM) + } + + /// Returns the `version` string associated with this compiler for Rust + /// itself. + /// + /// Note that this is a descriptive string which includes the commit date, + /// sha, version, etc. + fn rust_version(&self) -> String { + self.rust_info.version(self, channel::CFG_RELEASE_NUM) + } + + /// Returns whether unstable features should be enabled for the compiler + /// we're building. + fn unstable_features(&self) -> bool { + match &self.config.channel[..] { + "stable" | "beta" => false, + "nightly" | _ => true, + } + } } impl<'a> Compiler<'a> { diff --git a/src/bootstrap/metadata.rs b/src/bootstrap/metadata.rs index 5ab542b6a2489..5918fe41e7c8b 100644 --- a/src/bootstrap/metadata.rs +++ b/src/bootstrap/metadata.rs @@ -27,6 +27,7 @@ struct Output { struct Package { id: String, name: String, + version: String, source: Option, manifest_path: String, } @@ -72,6 +73,7 @@ fn build_krate(build: &mut Build, krate: &str) { test_step: format!("test-crate-{}", package.name), bench_step: format!("bench-crate-{}", package.name), name: package.name, + version: package.version, deps: Vec::new(), path: path, }); diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index f16fc2092f616..90e1530308f4a 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -191,3 +191,96 @@ pub fn test_helpers(build: &Build, target: &str) { .file(build.src.join("src/rt/rust_test_helpers.c")) .compile("librust_test_helpers.a"); } +const OPENSSL_VERS: &'static str = "1.0.2k"; +const OPENSSL_SHA256: &'static str = + "6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0"; + +pub fn openssl(build: &Build, target: &str) { + let out = match build.openssl_dir(target) { + Some(dir) => dir, + None => return, + }; + + let stamp = out.join(".stamp"); + let mut contents = String::new(); + drop(File::open(&stamp).and_then(|mut f| f.read_to_string(&mut contents))); + if contents == OPENSSL_VERS { + return + } + t!(fs::create_dir_all(&out)); + + let name = format!("openssl-{}.tar.gz", OPENSSL_VERS); + let tarball = out.join(&name); + if !tarball.exists() { + let tmp = tarball.with_extension("tmp"); + build.run(Command::new("curl") + .arg("-o").arg(&tmp) + .arg(format!("https://www.openssl.org/source/{}", name))); + let mut shasum = if target.contains("apple") { + let mut cmd = Command::new("shasum"); + cmd.arg("-a").arg("256"); + cmd + } else { + Command::new("sha256sum") + }; + let output = output(&mut shasum.arg(&tmp)); + let found = output.split_whitespace().next().unwrap(); + if found != OPENSSL_SHA256 { + panic!("downloaded openssl sha256 different\n\ + expected: {}\n\ + found: {}\n", OPENSSL_SHA256, found); + } + t!(fs::rename(&tmp, &tarball)); + } + let obj = out.join(format!("openssl-{}", OPENSSL_VERS)); + let dst = build.openssl_install_dir(target).unwrap(); + drop(fs::remove_dir_all(&obj)); + drop(fs::remove_dir_all(&dst)); + build.run(Command::new("tar").arg("xf").arg(&tarball).current_dir(&out)); + + let mut configure = Command::new(obj.join("Configure")); + configure.arg(format!("--prefix={}", dst.display())); + configure.arg("no-dso"); + configure.arg("no-ssl2"); + configure.arg("no-ssl3"); + + let os = match target { + "aarch64-unknown-linux-gnu" => "linux-aarch64", + "arm-unknown-linux-gnueabi" => "linux-armv4", + "arm-unknown-linux-gnueabihf" => "linux-armv4", + "armv7-unknown-linux-gnueabihf" => "linux-armv4", + "i686-apple-darwin" => "darwin-i386-cc", + "i686-unknown-freebsd" => "BSD-x86-elf", + "i686-unknown-linux-gnu" => "linux-elf", + "i686-unknown-linux-musl" => "linux-elf", + "mips-unknown-linux-gnu" => "linux-mips32", + "mips64-unknown-linux-gnuabi64" => "linux64-mips64", + "mips64el-unknown-linux-gnuabi64" => "linux64-mips64", + "mipsel-unknown-linux-gnu" => "linux-mips32", + "powerpc-unknown-linux-gnu" => "linux-ppc", + "powerpc64-unknown-linux-gnu" => "linux-ppc64", + "powerpc64le-unknown-linux-gnu" => "linux-ppc64le", + "s390x-unknown-linux-gnu" => "linux64-s390x", + "x86_64-apple-darwin" => "darwin64-x86_64-cc", + "x86_64-unknown-freebsd" => "BSD-x86_64", + "x86_64-unknown-linux-gnu" => "linux-x86_64", + "x86_64-unknown-linux-musl" => "linux-x86_64", + "x86_64-unknown-netbsd" => "BSD-x86_64", + _ => panic!("don't know how to configure OpenSSL for {}", target), + }; + configure.arg(os); + configure.env("CC", build.cc(target)); + for flag in build.cflags(target) { + configure.arg(flag); + } + configure.current_dir(&obj); + println!("Configuring openssl for {}", target); + build.run_quiet(&mut configure); + println!("Building openssl for {}", target); + build.run_quiet(Command::new("make").current_dir(&obj)); + println!("Installing openssl for {}", target); + build.run_quiet(Command::new("make").arg("install").current_dir(&obj)); + + let mut f = t!(File::create(&stamp)); + t!(f.write_all(OPENSSL_VERS.as_bytes())); +} diff --git a/src/bootstrap/step.rs b/src/bootstrap/step.rs index dcd3407e174aa..6f8975309d112 100644 --- a/src/bootstrap/step.rs +++ b/src/bootstrap/step.rs @@ -278,9 +278,19 @@ pub fn build_rules<'a>(build: &'a Build) -> Rules { rules.build(&krate.build_step, path) .dep(|s| s.name("libtest-link")) .dep(move |s| s.name("llvm").host(&build.config.build).stage(0)) + .dep(|s| s.name("may-run-build-script")) .run(move |s| compile::rustc(build, s.target, &s.compiler())); } + // Crates which have build scripts need to rely on this rule to ensure that + // the necessary prerequisites for a build script are linked and located in + // place. + rules.build("may-run-build-script", "path/to/nowhere") + .dep(move |s| { + s.name("libstd-link") + .host(&build.config.build) + .target(&build.config.build) + }); rules.build("startup-objects", "src/rtstartup") .dep(|s| s.name("create-sysroot").target(s.host)) .run(move |s| compile::build_startup_objects(build, &s.compiler(), s.target)); @@ -478,9 +488,10 @@ pub fn build_rules<'a>(build: &'a Build) -> Rules { .dep(|s| s.name("dist-src")) .run(move |_| check::distcheck(build)); - rules.build("test-helpers", "src/rt/rust_test_helpers.c") .run(move |s| native::test_helpers(build, s.target)); + rules.build("openssl", "path/to/nowhere") + .run(move |s| native::openssl(build, s.target)); // Some test suites are run inside emulators, and most of our test binaries // are linked dynamically which means we need to ship the standard library @@ -547,6 +558,17 @@ pub fn build_rules<'a>(build: &'a Build) -> Rules { rules.build("tool-qemu-test-client", "src/tools/qemu-test-client") .dep(|s| s.name("libstd")) .run(move |s| compile::tool(build, s.stage, s.target, "qemu-test-client")); + rules.build("tool-cargo", "src/tools/cargo") + .dep(|s| s.name("libstd")) + .dep(|s| s.stage(0).host(s.target).name("openssl")) + .dep(move |s| { + // Cargo depends on procedural macros, which requires a full host + // compiler to be available, so we need to depend on that. + s.name("librustc-link") + .target(&build.config.build) + .host(&build.config.build) + }) + .run(move |s| compile::tool(build, s.stage, s.target, "cargo")); // ======================================================================== // Documentation targets @@ -673,6 +695,7 @@ pub fn build_rules<'a>(build: &'a Build) -> Rules { rules.dist("dist-cargo", "cargo") .host(true) .only_host_build(true) + .dep(|s| s.name("tool-cargo")) .run(move |s| dist::cargo(build, s.stage, s.target)); rules.dist("dist-extended", "extended") .default(build.config.extended) @@ -1180,6 +1203,7 @@ mod tests { build_step: "build-crate-std".to_string(), test_step: "test-std".to_string(), bench_step: "bench-std".to_string(), + version: String::new(), }); build.crates.insert("test".to_string(), ::Crate { name: "test".to_string(), @@ -1189,10 +1213,12 @@ mod tests { build_step: "build-crate-test".to_string(), test_step: "test-test".to_string(), bench_step: "bench-test".to_string(), + version: String::new(), }); build.crates.insert("rustc-main".to_string(), ::Crate { name: "rustc-main".to_string(), deps: Vec::new(), + version: String::new(), path: cwd.join("src/rustc-main"), doc_step: "doc-rustc-main".to_string(), build_step: "build-crate-rustc-main".to_string(), @@ -1378,7 +1404,7 @@ mod tests { let all = rules.expand(&plan); println!("all rules: {:#?}", all); assert!(!all.contains(&step.name("rustc"))); - assert!(!all.contains(&step.name("build-crate-std").stage(1))); + assert!(!all.contains(&step.name("build-crate-test").stage(1))); // all stage0 compiles should be for the build target, A for step in all.iter().filter(|s| s.stage == 0) { diff --git a/src/build_helper/lib.rs b/src/build_helper/lib.rs index 3dfd293808286..08f1f31c2d74b 100644 --- a/src/build_helper/lib.rs +++ b/src/build_helper/lib.rs @@ -53,6 +53,24 @@ pub fn run_silent(cmd: &mut Command) { } } +pub fn run_suppressed(cmd: &mut Command) { + let output = match cmd.output() { + Ok(status) => status, + Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", + cmd, e)), + }; + if !output.status.success() { + fail(&format!("command did not execute successfully: {:?}\n\ + expected success, got: {}\n\n\ + stdout ----\n{}\n\ + stderr ----\n{}\n", + cmd, + output.status, + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr))); + } +} + pub fn gnu_target(target: &str) -> String { match target { "i686-pc-windows-msvc" => "i686-pc-win32".to_string(), diff --git a/src/ci/run.sh b/src/ci/run.sh index 53d16f6239e66..4c4836d7ca230 100755 --- a/src/ci/run.sh +++ b/src/ci/run.sh @@ -27,6 +27,7 @@ RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-sccache" RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-quiet-tests" RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --disable-manage-submodules" RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-locked-deps" +RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-cargo-openssl-static" if [ "$DIST_SRC" = "" ]; then RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --disable-dist-src" diff --git a/src/tools/build-manifest/src/main.rs b/src/tools/build-manifest/src/main.rs index c2c91487b0c17..ceefcc9e0ec46 100644 --- a/src/tools/build-manifest/src/main.rs +++ b/src/tools/build-manifest/src/main.rs @@ -131,7 +131,8 @@ macro_rules! t { } struct Builder { - channel: String, + rust_release: String, + cargo_release: String, input: PathBuf, output: PathBuf, gpg_passphrase: String, @@ -147,13 +148,15 @@ fn main() { let input = PathBuf::from(args.next().unwrap()); let output = PathBuf::from(args.next().unwrap()); let date = args.next().unwrap(); - let channel = args.next().unwrap(); + let rust_release = args.next().unwrap(); + let cargo_release = args.next().unwrap(); let s3_address = args.next().unwrap(); let mut passphrase = String::new(); t!(io::stdin().read_to_string(&mut passphrase)); Builder { - channel: channel, + rust_release: rust_release, + cargo_release: cargo_release, input: input, output: output, gpg_passphrase: passphrase, @@ -184,10 +187,10 @@ impl Builder { manifest.insert("pkg".to_string(), toml::encode(&pkg)); let manifest = toml::Value::Table(manifest).to_string(); - let filename = format!("channel-rust-{}.toml", self.channel); + let filename = format!("channel-rust-{}.toml", self.rust_release); self.write_manifest(&manifest, &filename); - if self.channel != "beta" && self.channel != "nightly" { + if self.rust_release != "beta" && self.rust_release != "nightly" { self.write_manifest(&manifest, "channel-rust-stable.toml"); } } @@ -336,11 +339,11 @@ impl Builder { fn filename(&self, component: &str, target: &str) -> String { if component == "rust-src" { - format!("rust-src-{}.tar.gz", self.channel) + format!("rust-src-{}.tar.gz", self.rust_release) } else if component == "cargo" { - format!("cargo-nightly-{}.tar.gz", target) + format!("cargo-{}-{}.tar.gz", self.cargo_release, target) } else { - format!("{}-{}-{}.tar.gz", component, self.channel, target) + format!("{}-{}-{}.tar.gz", component, self.rust_release, target) } } diff --git a/src/tools/cargo b/src/tools/cargo index 6ed5a43bb566e..d17b61aa5a2ca 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit 6ed5a43bb566e0ee3fe7981de5aa5a36e2905ebd +Subproject commit d17b61aa5a2ca790f268a043bffdb0ffb04f0ec7 diff --git a/src/tools/tidy/src/cargo.rs b/src/tools/tidy/src/cargo.rs index 11acb64743a7a..053f0bbe3b81d 100644 --- a/src/tools/tidy/src/cargo.rs +++ b/src/tools/tidy/src/cargo.rs @@ -20,7 +20,7 @@ use std::fs::File; use std::path::Path; pub fn check(path: &Path, bad: &mut bool) { - if path.ends_with("vendor") { + if !super::filter_dirs(path) { return } for entry in t!(path.read_dir(), path).map(|e| t!(e)) { diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index e1c44a20e9756..3bf396db4d39d 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -16,6 +16,7 @@ use std::path::Path; static LICENSES: &'static [&'static str] = &[ "MIT/Apache-2.0", + "MIT / Apache-2.0", "Apache-2.0/MIT", "MIT OR Apache-2.0", "MIT", @@ -25,6 +26,7 @@ static LICENSES: &'static [&'static str] = &[ /// These MPL licensed projects are acceptable, but only these. static EXCEPTIONS: &'static [&'static str] = &[ "mdbook", + "openssl", "pest", "thread-id", ]; diff --git a/src/tools/tidy/src/main.rs b/src/tools/tidy/src/main.rs index 1bb7919c1d35b..2af891b5b8562 100644 --- a/src/tools/tidy/src/main.rs +++ b/src/tools/tidy/src/main.rs @@ -70,6 +70,7 @@ fn filter_dirs(path: &Path) -> bool { "src/rustllvm", "src/rust-installer", "src/liblibc", + "src/tools/cargo", "src/vendor", ]; skip.iter().any(|p| path.ends_with(p)) From 75a35ef028ca5ba9b9a2f0626ba26783834425d2 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Fri, 3 Mar 2017 09:40:44 -0800 Subject: [PATCH 14/29] Fix missing WhileLet pattern scope --- src/librustc_resolve/lib.rs | 2 ++ src/test/run-pass/issue-40235.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/test/run-pass/issue-40235.rs diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 0565db28ec5c9..093994ba8257d 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -2897,8 +2897,10 @@ impl<'a> Resolver<'a> { ExprKind::WhileLet(ref pattern, ref subexpression, ref block, label) => { self.with_resolved_label(label, expr.id, |this| { this.visit_expr(subexpression); + this.ribs[ValueNS].push(Rib::new(NormalRibKind)); this.resolve_pattern(pattern, PatternSource::WhileLet, &mut FxHashMap()); this.visit_block(block); + this.ribs[ValueNS].pop(); }); } diff --git a/src/test/run-pass/issue-40235.rs b/src/test/run-pass/issue-40235.rs new file mode 100644 index 0000000000000..90f170d8e42e0 --- /dev/null +++ b/src/test/run-pass/issue-40235.rs @@ -0,0 +1,16 @@ +// Copyright 2014 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. + +fn foo() {} + +fn main() { + while let Some(foo) = Some(1) { break } + foo(); +} From 111fbe7921a7de649a9b76b3f247fc8762217819 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 3 Mar 2017 11:39:11 -0800 Subject: [PATCH 15/29] Let `-Crelocation-model` better control `-pie` linking Prior to this, if relocation model in the target options was "pic", as most targets have it, then the user's `-Crelocation-model` had no effect on whether `-pie` would be used. Only `-Clink-arg=-static` would have a further override here. Now we use `context::get_reloc_model`, which gives precedence to the user's option, and if it's `RelocMode::PIC` we enable `-pie`. This is the same test as `context::is_pie_binary`, except that folds across all `sess.crate_types`, not just the current one. --- src/librustc_trans/back/link.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index 1da9fcb0e95e4..b58f96033bf5b 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -31,6 +31,8 @@ use rustc::hir::svh::Svh; use rustc_back::tempdir::TempDir; use rustc_back::PanicStrategy; use rustc_incremental::IncrementalHashesMap; +use context::get_reloc_model; +use llvm; use std::ascii; use std::char; @@ -859,13 +861,11 @@ fn link_args(cmd: &mut Linker, if crate_type == config::CrateTypeExecutable && t.options.position_independent_executables { let empty_vec = Vec::new(); - let empty_str = String::new(); let args = sess.opts.cg.link_args.as_ref().unwrap_or(&empty_vec); let more_args = &sess.opts.cg.link_arg; let mut args = args.iter().chain(more_args.iter()).chain(used_link_args.iter()); - let relocation_model = sess.opts.cg.relocation_model.as_ref() - .unwrap_or(&empty_str); - if (t.options.relocation_model == "pic" || *relocation_model == "pic") + + if get_reloc_model(sess) == llvm::RelocMode::PIC && !args.any(|x| *x == "-static") { cmd.position_independent_executable(); } From 0f48db83ba2c8d35b2d8880f616b85a624df217f Mon Sep 17 00:00:00 2001 From: steveklabnik Date: Thu, 2 Mar 2017 18:44:45 -0500 Subject: [PATCH 16/29] remove reference --- src/doc/reference/.gitignore | 1 - src/doc/reference/src/SUMMARY.md | 58 - src/doc/reference/src/attributes.md | 630 ---------- .../src/behavior-considered-undefined.md | 35 - .../src/behavior-not-considered-unsafe.md | 15 - src/doc/reference/src/comments.md | 20 - .../reference/src/crates-and-source-files.md | 73 -- src/doc/reference/src/expressions.md | 863 ------------- src/doc/reference/src/identifiers.md | 24 - src/doc/reference/src/influences.md | 22 - src/doc/reference/src/input-format.md | 10 - src/doc/reference/src/introduction.md | 31 - src/doc/reference/src/items-and-attributes.md | 7 - src/doc/reference/src/items.md | 1091 ----------------- src/doc/reference/src/lexical-structure.md | 1 - src/doc/reference/src/linkage.md | 127 -- src/doc/reference/src/macros-by-example.md | 88 -- src/doc/reference/src/macros.md | 17 - .../src/memory-allocation-and-lifetime.md | 13 - src/doc/reference/src/memory-model.md | 10 - src/doc/reference/src/memory-ownership.md | 4 - src/doc/reference/src/notation.md | 1 - src/doc/reference/src/paths.md | 105 -- src/doc/reference/src/procedural-macros.md | 23 - src/doc/reference/src/special-traits.md | 3 - .../src/statements-and-expressions.md | 11 - src/doc/reference/src/statements.md | 42 - .../reference/src/string-table-productions.md | 18 - src/doc/reference/src/subtyping.md | 19 - src/doc/reference/src/the-copy-trait.md | 4 - src/doc/reference/src/the-deref-trait.md | 7 - src/doc/reference/src/the-drop-trait.md | 4 - src/doc/reference/src/the-send-trait.md | 4 - src/doc/reference/src/the-sized-trait.md | 3 - src/doc/reference/src/the-sync-trait.md | 4 - src/doc/reference/src/theme/book.css | 798 ------------ src/doc/reference/src/tokens.md | 326 ----- src/doc/reference/src/type-coercions.md | 145 --- src/doc/reference/src/type-system.md | 1 - src/doc/reference/src/types.md | 398 ------ src/doc/reference/src/unicode-productions.md | 9 - src/doc/reference/src/unsafe-blocks.md | 22 - src/doc/reference/src/unsafe-functions.md | 5 - src/doc/reference/src/unsafety.md | 11 - src/doc/reference/src/variables.md | 31 - .../reference/src/visibility-and-privacy.md | 160 --- src/doc/reference/src/whitespace.md | 22 - 47 files changed, 5316 deletions(-) delete mode 100644 src/doc/reference/.gitignore delete mode 100644 src/doc/reference/src/SUMMARY.md delete mode 100644 src/doc/reference/src/attributes.md delete mode 100644 src/doc/reference/src/behavior-considered-undefined.md delete mode 100644 src/doc/reference/src/behavior-not-considered-unsafe.md delete mode 100644 src/doc/reference/src/comments.md delete mode 100644 src/doc/reference/src/crates-and-source-files.md delete mode 100644 src/doc/reference/src/expressions.md delete mode 100644 src/doc/reference/src/identifiers.md delete mode 100644 src/doc/reference/src/influences.md delete mode 100644 src/doc/reference/src/input-format.md delete mode 100644 src/doc/reference/src/introduction.md delete mode 100644 src/doc/reference/src/items-and-attributes.md delete mode 100644 src/doc/reference/src/items.md delete mode 100644 src/doc/reference/src/lexical-structure.md delete mode 100644 src/doc/reference/src/linkage.md delete mode 100644 src/doc/reference/src/macros-by-example.md delete mode 100644 src/doc/reference/src/macros.md delete mode 100644 src/doc/reference/src/memory-allocation-and-lifetime.md delete mode 100644 src/doc/reference/src/memory-model.md delete mode 100644 src/doc/reference/src/memory-ownership.md delete mode 100644 src/doc/reference/src/notation.md delete mode 100644 src/doc/reference/src/paths.md delete mode 100644 src/doc/reference/src/procedural-macros.md delete mode 100644 src/doc/reference/src/special-traits.md delete mode 100644 src/doc/reference/src/statements-and-expressions.md delete mode 100644 src/doc/reference/src/statements.md delete mode 100644 src/doc/reference/src/string-table-productions.md delete mode 100644 src/doc/reference/src/subtyping.md delete mode 100644 src/doc/reference/src/the-copy-trait.md delete mode 100644 src/doc/reference/src/the-deref-trait.md delete mode 100644 src/doc/reference/src/the-drop-trait.md delete mode 100644 src/doc/reference/src/the-send-trait.md delete mode 100644 src/doc/reference/src/the-sized-trait.md delete mode 100644 src/doc/reference/src/the-sync-trait.md delete mode 100644 src/doc/reference/src/theme/book.css delete mode 100644 src/doc/reference/src/tokens.md delete mode 100644 src/doc/reference/src/type-coercions.md delete mode 100644 src/doc/reference/src/type-system.md delete mode 100644 src/doc/reference/src/types.md delete mode 100644 src/doc/reference/src/unicode-productions.md delete mode 100644 src/doc/reference/src/unsafe-blocks.md delete mode 100644 src/doc/reference/src/unsafe-functions.md delete mode 100644 src/doc/reference/src/unsafety.md delete mode 100644 src/doc/reference/src/variables.md delete mode 100644 src/doc/reference/src/visibility-and-privacy.md delete mode 100644 src/doc/reference/src/whitespace.md diff --git a/src/doc/reference/.gitignore b/src/doc/reference/.gitignore deleted file mode 100644 index 7585238efedfc..0000000000000 --- a/src/doc/reference/.gitignore +++ /dev/null @@ -1 +0,0 @@ -book diff --git a/src/doc/reference/src/SUMMARY.md b/src/doc/reference/src/SUMMARY.md deleted file mode 100644 index a07e195a7184f..0000000000000 --- a/src/doc/reference/src/SUMMARY.md +++ /dev/null @@ -1,58 +0,0 @@ -# The Rust Reference - -[Introduction](introduction.md) - -- [Notation](notation.md) - - [Unicode productions](unicode-productions.md) - - [String table productions](string-table-productions.md) - -- [Lexical structure](lexical-structure.md) - - [Input format](input-format.md) - - [Identifiers](identifiers.md) - - [Comments](comments.md) - - [Whitespace](whitespace.md) - - [Tokens](tokens.md) - - [Paths](paths.md) - -- [Macros](macros.md) - - [Macros By Example](macros-by-example.md) - - [Procedrual Macros](procedural-macros.md) - -- [Crates and source files](crates-and-source-files.md) - -- [Items and attributes](items-and-attributes.md) - - [Items](items.md) - - [Visibility and Privacy](visibility-and-privacy.md) - - [Attributes](attributes.md) - -- [Statements and expressions](statements-and-expressions.md) - - [Statements](statements.md) - - [Expressions](expressions.md) - -- [Type system](type-system.md) - - [Types](types.md) - - [Subtyping](subtyping.md) - - [Type coercions](type-coercions.md) - -- [Special traits](special-traits.md) - - [The Copy trait](the-copy-trait.md) - - [The Sized trait](the-sized-trait.md) - - [The Drop trait](the-drop-trait.md) - - [The Deref trait](the-deref-trait.md) - - [The Send trait](the-send-trait.md) - - [The Sync trait](the-sync-trait.md) - -- [Memory model](memory-model.md) - - [Memory allocation and lifetime](memory-allocation-and-lifetime.md) - - [Memory ownership](memory-ownership.md) - - [Variables](variables.md) - -- [Linkage](linkage.md) - -- [Unsafety](unsafety.md) - - [Unsafe functions](unsafe-functions.md) - - [Unsafe blocks](unsafe-blocks.md) - - [Behavior considered undefined](behavior-considered-undefined.md) - - [Behavior not considered unsafe](behavior-not-considered-unsafe.md) - -[Appendix: Influences](influences.md) diff --git a/src/doc/reference/src/attributes.md b/src/doc/reference/src/attributes.md deleted file mode 100644 index 31814ef8c5794..0000000000000 --- a/src/doc/reference/src/attributes.md +++ /dev/null @@ -1,630 +0,0 @@ -# Attributes - -Any item declaration may have an _attribute_ applied to it. Attributes in Rust -are modeled on Attributes in ECMA-335, with the syntax coming from ECMA-334 -(C#). An attribute is a general, free-form metadatum that is interpreted -according to name, convention, and language and compiler version. Attributes -may appear as any of: - -* A single identifier, the attribute name -* An identifier followed by the equals sign '=' and a literal, providing a - key/value pair -* An identifier followed by a parenthesized list of sub-attribute arguments - -Attributes with a bang ("!") after the hash ("#") apply to the item that the -attribute is declared within. Attributes that do not have a bang after the hash -apply to the item that follows the attribute. - -An example of attributes: - -```{.rust} -// General metadata applied to the enclosing module or crate. -#![crate_type = "lib"] - -// A function marked as a unit test -#[test] -fn test_foo() { - /* ... */ -} - -// A conditionally-compiled module -#[cfg(target_os="linux")] -mod bar { - /* ... */ -} - -// A lint attribute used to suppress a warning/error -#[allow(non_camel_case_types)] -type int8_t = i8; -``` - -> **Note:** At some point in the future, the compiler will distinguish between -> language-reserved and user-available attributes. Until then, there is -> effectively no difference between an attribute handled by a loadable syntax -> extension and the compiler. - -## Crate-only attributes - -- `crate_name` - specify the crate's crate name. -- `crate_type` - see [linkage](linkage.html). -- `feature` - see [compiler features](#compiler-features). -- `no_builtins` - disable optimizing certain code patterns to invocations of - library functions that are assumed to exist -- `no_main` - disable emitting the `main` symbol. Useful when some other - object being linked to defines `main`. -- `no_start` - disable linking to the `native` crate, which specifies the - "start" language item. -- `no_std` - disable linking to the `std` crate. -- `plugin` - load a list of named crates as compiler plugins, e.g. - `#![plugin(foo, bar)]`. Optional arguments for each plugin, - i.e. `#![plugin(foo(... args ...))]`, are provided to the plugin's - registrar function. The `plugin` feature gate is required to use - this attribute. -- `recursion_limit` - Sets the maximum depth for potentially - infinitely-recursive compile-time operations like - auto-dereference or macro expansion. The default is - `#![recursion_limit="64"]`. - -### Module-only attributes - -- `no_implicit_prelude` - disable injecting `use std::prelude::*` in this - module. -- `path` - specifies the file to load the module from. `#[path="foo.rs"] mod - bar;` is equivalent to `mod bar { /* contents of foo.rs */ }`. The path is - taken relative to the directory that the current module is in. - -## Function-only attributes - -- `main` - indicates that this function should be passed to the entry point, - rather than the function in the crate root named `main`. -- `plugin_registrar` - mark this function as the registration point for - [compiler plugins][plugin], such as loadable syntax extensions. -- `start` - indicates that this function should be used as the entry point, - overriding the "start" language item. See the "start" [language - item](#language-items) for more details. -- `test` - indicates that this function is a test function, to only be compiled - in case of `--test`. -- `should_panic` - indicates that this test function should panic, inverting the success condition. -- `cold` - The function is unlikely to be executed, so optimize it (and calls - to it) differently. -- `naked` - The function utilizes a custom ABI or custom inline ASM that requires - epilogue and prologue to be skipped. - -## Static-only attributes - -- `thread_local` - on a `static mut`, this signals that the value of this - static may change depending on the current thread. The exact consequences of - this are implementation-defined. - -## FFI attributes - -On an `extern` block, the following attributes are interpreted: - -- `link_args` - specify arguments to the linker, rather than just the library - name and type. This is feature gated and the exact behavior is - implementation-defined (due to variety of linker invocation syntax). -- `link` - indicate that a native library should be linked to for the - declarations in this block to be linked correctly. `link` supports an optional - `kind` key with three possible values: `dylib`, `static`, and `framework`. See - [external blocks](items.html#external-blocks) for more about external blocks. Two - examples: `#[link(name = "readline")]` and - `#[link(name = "CoreFoundation", kind = "framework")]`. -- `linked_from` - indicates what native library this block of FFI items is - coming from. This attribute is of the form `#[linked_from = "foo"]` where - `foo` is the name of a library in either `#[link]` or a `-l` flag. This - attribute is currently required to export symbols from a Rust dynamic library - on Windows, and it is feature gated behind the `linked_from` feature. - -On declarations inside an `extern` block, the following attributes are -interpreted: - -- `link_name` - the name of the symbol that this function or static should be - imported as. -- `linkage` - on a static, this specifies the [linkage - type](http://llvm.org/docs/LangRef.html#linkage-types). - -On `enum`s: - -- `repr` - on C-like enums, this sets the underlying type used for - representation. Takes one argument, which is the primitive - type this enum should be represented for, or `C`, which specifies that it - should be the default `enum` size of the C ABI for that platform. Note that - enum representation in C is undefined, and this may be incorrect when the C - code is compiled with certain flags. - -On `struct`s: - -- `repr` - specifies the representation to use for this struct. Takes a list - of options. The currently accepted ones are `C` and `packed`, which may be - combined. `C` will use a C ABI compatible struct layout, and `packed` will - remove any padding between fields (note that this is very fragile and may - break platforms which require aligned access). - -## Macro-related attributes - -- `macro_use` on a `mod` — macros defined in this module will be visible in the - module's parent, after this module has been included. - -- `macro_use` on an `extern crate` — load macros from this crate. An optional - list of names `#[macro_use(foo, bar)]` restricts the import to just those - macros named. The `extern crate` must appear at the crate root, not inside - `mod`, which ensures proper function of the [`$crate` macro - variable](../book/macros.html#the-variable-crate). - -- `macro_reexport` on an `extern crate` — re-export the named macros. - -- `macro_export` - export a macro for cross-crate usage. - -- `no_link` on an `extern crate` — even if we load this crate for macros, don't - link it into the output. - -See the [macros section of the -book](../book/macros.html#scoping-and-macro-importexport) for more information on -macro scope. - -## Miscellaneous attributes - -- `deprecated` - mark the item as deprecated; the full attribute is - `#[deprecated(since = "crate version", note = "...")`, where both arguments - are optional. -- `export_name` - on statics and functions, this determines the name of the - exported symbol. -- `link_section` - on statics and functions, this specifies the section of the - object file that this item's contents will be placed into. -- `no_mangle` - on any item, do not apply the standard name mangling. Set the - symbol for this item to its identifier. -- `simd` - on certain tuple structs, derive the arithmetic operators, which - lower to the target's SIMD instructions, if any; the `simd` feature gate - is necessary to use this attribute. -- `unsafe_destructor_blind_to_params` - on `Drop::drop` method, asserts that the - destructor code (and all potential specializations of that code) will - never attempt to read from nor write to any references with lifetimes - that come in via generic parameters. This is a constraint we cannot - currently express via the type system, and therefore we rely on the - programmer to assert that it holds. Adding this to a Drop impl causes - the associated destructor to be considered "uninteresting" by the - Drop-Check rule, and thus it can help sidestep data ordering - constraints that would otherwise be introduced by the Drop-Check - rule. Such sidestepping of the constraints, if done incorrectly, can - lead to undefined behavior (in the form of reading or writing to data - outside of its dynamic extent), and thus this attribute has the word - "unsafe" in its name. To use this, the - `unsafe_destructor_blind_to_params` feature gate must be enabled. -- `doc` - Doc comments such as `/// foo` are equivalent to `#[doc = "foo"]`. -- `rustc_on_unimplemented` - Write a custom note to be shown along with the error - when the trait is found to be unimplemented on a type. - You may use format arguments like `{T}`, `{A}` to correspond to the - types at the point of use corresponding to the type parameters of the - trait of the same name. `{Self}` will be replaced with the type that is supposed - to implement the trait but doesn't. To use this, the `on_unimplemented` feature gate - must be enabled. -- `must_use` - on structs and enums, will warn if a value of this type isn't used or - assigned to a variable. You may also include an optional message by using - `#[must_use = "message"]` which will be given alongside the warning. - -### Conditional compilation - -Sometimes one wants to have different compiler outputs from the same code, -depending on build target, such as targeted operating system, or to enable -release builds. - -Configuration options are boolean (on or off) and are named either with a -single identifier (e.g. `foo`) or an identifier and a string (e.g. `foo = "bar"`; -the quotes are required and spaces around the `=` are unimportant). Note that -similarly-named options, such as `foo`, `foo="bar"` and `foo="baz"` may each be set -or unset independently. - -Configuration options are either provided by the compiler or passed in on the -command line using `--cfg` (e.g. `rustc main.rs --cfg foo --cfg 'bar="baz"'`). -Rust code then checks for their presence using the `#[cfg(...)]` attribute: - -``` -// The function is only included in the build when compiling for macOS -#[cfg(target_os = "macos")] -fn macos_only() { - // ... -} - -// This function is only included when either foo or bar is defined -#[cfg(any(foo, bar))] -fn needs_foo_or_bar() { - // ... -} - -// This function is only included when compiling for a unixish OS with a 32-bit -// architecture -#[cfg(all(unix, target_pointer_width = "32"))] -fn on_32bit_unix() { - // ... -} - -// This function is only included when foo is not defined -#[cfg(not(foo))] -fn needs_not_foo() { - // ... -} -``` - -This illustrates some conditional compilation can be achieved using the -`#[cfg(...)]` attribute. `any`, `all` and `not` can be used to assemble -arbitrarily complex configurations through nesting. - -The following configurations must be defined by the implementation: - -* `target_arch = "..."` - Target CPU architecture, such as `"x86"`, - `"x86_64"` `"mips"`, `"powerpc"`, `"powerpc64"`, `"arm"`, or - `"aarch64"`. This value is closely related to the first element of - the platform target triple, though it is not identical. -* `target_os = "..."` - Operating system of the target, examples - include `"windows"`, `"macos"`, `"ios"`, `"linux"`, `"android"`, - `"freebsd"`, `"dragonfly"`, `"bitrig"` , `"openbsd"` or - `"netbsd"`. This value is closely related to the second and third - element of the platform target triple, though it is not identical. -* `target_family = "..."` - Operating system family of the target, e. g. - `"unix"` or `"windows"`. The value of this configuration option is defined - as a configuration itself, like `unix` or `windows`. -* `unix` - See `target_family`. -* `windows` - See `target_family`. -* `target_env = ".."` - Further disambiguates the target platform with - information about the ABI/libc. Presently this value is either - `"gnu"`, `"msvc"`, `"musl"`, or the empty string. For historical - reasons this value has only been defined as non-empty when needed - for disambiguation. Thus on many GNU platforms this value will be - empty. This value is closely related to the fourth element of the - platform target triple, though it is not identical. For example, - embedded ABIs such as `gnueabihf` will simply define `target_env` as - `"gnu"`. -* `target_endian = "..."` - Endianness of the target CPU, either `"little"` or - `"big"`. -* `target_pointer_width = "..."` - Target pointer width in bits. This is set - to `"32"` for targets with 32-bit pointers, and likewise set to `"64"` for - 64-bit pointers. -* `target_has_atomic = "..."` - Set of integer sizes on which the target can perform - atomic operations. Values are `"8"`, `"16"`, `"32"`, `"64"` and `"ptr"`. -* `target_vendor = "..."` - Vendor of the target, for example `apple`, `pc`, or - simply `"unknown"`. -* `test` - Enabled when compiling the test harness (using the `--test` flag). -* `debug_assertions` - Enabled by default when compiling without optimizations. - This can be used to enable extra debugging code in development but not in - production. For example, it controls the behavior of the standard library's - `debug_assert!` macro. - -You can also set another attribute based on a `cfg` variable with `cfg_attr`: - -```rust,ignore -#[cfg_attr(a, b)] -``` - -This is the same as `#[b]` if `a` is set by `cfg`, and nothing otherwise. - -Lastly, configuration options can be used in expressions by invoking the `cfg!` -macro: `cfg!(a)` evaluates to `true` if `a` is set, and `false` otherwise. - -### Lint check attributes - -A lint check names a potentially undesirable coding pattern, such as -unreachable code or omitted documentation, for the static entity to which the -attribute applies. - -For any lint check `C`: - -* `allow(C)` overrides the check for `C` so that violations will go - unreported, -* `deny(C)` signals an error after encountering a violation of `C`, -* `forbid(C)` is the same as `deny(C)`, but also forbids changing the lint - level afterwards, -* `warn(C)` warns about violations of `C` but continues compilation. - -The lint checks supported by the compiler can be found via `rustc -W help`, -along with their default settings. [Compiler -plugins](../unstable-book/plugin.html#lint-plugins) can provide additional -lint checks. - -```{.ignore} -pub mod m1 { - // Missing documentation is ignored here - #[allow(missing_docs)] - pub fn undocumented_one() -> i32 { 1 } - - // Missing documentation signals a warning here - #[warn(missing_docs)] - pub fn undocumented_too() -> i32 { 2 } - - // Missing documentation signals an error here - #[deny(missing_docs)] - pub fn undocumented_end() -> i32 { 3 } -} -``` - -This example shows how one can use `allow` and `warn` to toggle a particular -check on and off: - -```{.ignore} -#[warn(missing_docs)] -pub mod m2{ - #[allow(missing_docs)] - pub mod nested { - // Missing documentation is ignored here - pub fn undocumented_one() -> i32 { 1 } - - // Missing documentation signals a warning here, - // despite the allow above. - #[warn(missing_docs)] - pub fn undocumented_two() -> i32 { 2 } - } - - // Missing documentation signals a warning here - pub fn undocumented_too() -> i32 { 3 } -} -``` - -This example shows how one can use `forbid` to disallow uses of `allow` for -that lint check: - -```{.ignore} -#[forbid(missing_docs)] -pub mod m3 { - // Attempting to toggle warning signals an error here - #[allow(missing_docs)] - /// Returns 2. - pub fn undocumented_too() -> i32 { 2 } -} -``` - -### Language items - -Some primitive Rust operations are defined in Rust code, rather than being -implemented directly in C or assembly language. The definitions of these -operations have to be easy for the compiler to find. The `lang` attribute -makes it possible to declare these operations. For example, the `str` module -in the Rust standard library defines the string equality function: - -```{.ignore} -#[lang = "str_eq"] -pub fn eq_slice(a: &str, b: &str) -> bool { - // details elided -} -``` - -The name `str_eq` has a special meaning to the Rust compiler, and the presence -of this definition means that it will use this definition when generating calls -to the string equality function. - -The set of language items is currently considered unstable. A complete -list of the built-in language items will be added in the future. - -### Inline attributes - -The inline attribute suggests that the compiler should place a copy of -the function or static in the caller, rather than generating code to -call the function or access the static where it is defined. - -The compiler automatically inlines functions based on internal heuristics. -Incorrectly inlining functions can actually make the program slower, so it -should be used with care. - -`#[inline]` and `#[inline(always)]` always cause the function to be serialized -into the crate metadata to allow cross-crate inlining. - -There are three different types of inline attributes: - -* `#[inline]` hints the compiler to perform an inline expansion. -* `#[inline(always)]` asks the compiler to always perform an inline expansion. -* `#[inline(never)]` asks the compiler to never perform an inline expansion. - -### `derive` - -The `derive` attribute allows certain traits to be automatically implemented -for data structures. For example, the following will create an `impl` for the -`PartialEq` and `Clone` traits for `Foo`, the type parameter `T` will be given -the `PartialEq` or `Clone` constraints for the appropriate `impl`: - -``` -#[derive(PartialEq, Clone)] -struct Foo { - a: i32, - b: T, -} -``` - -The generated `impl` for `PartialEq` is equivalent to - -``` -# struct Foo { a: i32, b: T } -impl PartialEq for Foo { - fn eq(&self, other: &Foo) -> bool { - self.a == other.a && self.b == other.b - } - - fn ne(&self, other: &Foo) -> bool { - self.a != other.a || self.b != other.b - } -} -``` - -You can implement `derive` for your own type through [procedural -macros](procedural-macros.html). - -### Compiler Features - -Certain aspects of Rust may be implemented in the compiler, but they're not -necessarily ready for every-day use. These features are often of "prototype -quality" or "almost production ready", but may not be stable enough to be -considered a full-fledged language feature. - -For this reason, Rust recognizes a special crate-level attribute of the form: - -```{.ignore} -#![feature(feature1, feature2, feature3)] -``` - -This directive informs the compiler that the feature list: `feature1`, -`feature2`, and `feature3` should all be enabled. This is only recognized at a -crate-level, not at a module-level. Without this directive, all features are -considered off, and using the features will result in a compiler error. - -The currently implemented features of the reference compiler are: - -* `advanced_slice_patterns` - See the [match - expressions](expressions.html#match-expressions) - section for discussion; the exact semantics of -slice patterns are subject to change, so some types are still unstable. - -* `slice_patterns` - OK, actually, slice patterns are just scary and - completely unstable. - -* `asm` - The `asm!` macro provides a means for inline assembly. This is often - useful, but the exact syntax for this feature along with its - semantics are likely to change, so this macro usage must be opted - into. - -* `associated_consts` - Allows constants to be defined in `impl` and `trait` - blocks, so that they can be associated with a type or - trait in a similar manner to methods and associated - types. - -* `box_patterns` - Allows `box` patterns, the exact semantics of which - is subject to change. - -* `box_syntax` - Allows use of `box` expressions, the exact semantics of which - is subject to change. - -* `cfg_target_vendor` - Allows conditional compilation using the `target_vendor` - matcher which is subject to change. - -* `cfg_target_has_atomic` - Allows conditional compilation using the `target_has_atomic` - matcher which is subject to change. - -* `concat_idents` - Allows use of the `concat_idents` macro, which is in many - ways insufficient for concatenating identifiers, and may be - removed entirely for something more wholesome. - -* `custom_attribute` - Allows the usage of attributes unknown to the compiler - so that new attributes can be added in a backwards compatible - manner (RFC 572). - -* `custom_derive` - Allows the use of `#[derive(Foo,Bar)]` as sugar for - `#[derive_Foo] #[derive_Bar]`, which can be user-defined syntax - extensions. - -* `inclusive_range_syntax` - Allows use of the `a...b` and `...b` syntax for inclusive ranges. - -* `inclusive_range` - Allows use of the types that represent desugared inclusive ranges. - -* `intrinsics` - Allows use of the "rust-intrinsics" ABI. Compiler intrinsics - are inherently unstable and no promise about them is made. - -* `lang_items` - Allows use of the `#[lang]` attribute. Like `intrinsics`, - lang items are inherently unstable and no promise about them - is made. - -* `link_args` - This attribute is used to specify custom flags to the linker, - but usage is strongly discouraged. The compiler's usage of the - system linker is not guaranteed to continue in the future, and - if the system linker is not used then specifying custom flags - doesn't have much meaning. - -* `link_llvm_intrinsics` - Allows linking to LLVM intrinsics via - `#[link_name="llvm.*"]`. - -* `linkage` - Allows use of the `linkage` attribute, which is not portable. - -* `log_syntax` - Allows use of the `log_syntax` macro attribute, which is a - nasty hack that will certainly be removed. - -* `main` - Allows use of the `#[main]` attribute, which changes the entry point - into a Rust program. This capability is subject to change. - -* `macro_reexport` - Allows macros to be re-exported from one crate after being imported - from another. This feature was originally designed with the sole - use case of the Rust standard library in mind, and is subject to - change. - -* `non_ascii_idents` - The compiler supports the use of non-ascii identifiers, - but the implementation is a little rough around the - edges, so this can be seen as an experimental feature - for now until the specification of identifiers is fully - fleshed out. - -* `no_std` - Allows the `#![no_std]` crate attribute, which disables the implicit - `extern crate std`. This typically requires use of the unstable APIs - behind the libstd "facade", such as libcore and libcollections. It - may also cause problems when using syntax extensions, including - `#[derive]`. - -* `on_unimplemented` - Allows the `#[rustc_on_unimplemented]` attribute, which allows - trait definitions to add specialized notes to error messages - when an implementation was expected but not found. - -* `optin_builtin_traits` - Allows the definition of default and negative trait - implementations. Experimental. - -* `plugin` - Usage of [compiler plugins][plugin] for custom lints or syntax extensions. - These depend on compiler internals and are subject to change. - -* `plugin_registrar` - Indicates that a crate provides [compiler plugins][plugin]. - -* `quote` - Allows use of the `quote_*!` family of macros, which are - implemented very poorly and will likely change significantly - with a proper implementation. - -* `rustc_attrs` - Gates internal `#[rustc_*]` attributes which may be - for internal use only or have meaning added to them in the future. - -* `rustc_diagnostic_macros`- A mysterious feature, used in the implementation - of rustc, not meant for mortals. - -* `simd` - Allows use of the `#[simd]` attribute, which is overly simple and - not the SIMD interface we want to expose in the long term. - -* `simd_ffi` - Allows use of SIMD vectors in signatures for foreign functions. - The SIMD interface is subject to change. - -* `start` - Allows use of the `#[start]` attribute, which changes the entry point - into a Rust program. This capability, especially the signature for the - annotated function, is subject to change. - -* `thread_local` - The usage of the `#[thread_local]` attribute is experimental - and should be seen as unstable. This attribute is used to - declare a `static` as being unique per-thread leveraging - LLVM's implementation which works in concert with the kernel - loader and dynamic linker. This is not necessarily available - on all platforms, and usage of it is discouraged. - -* `trace_macros` - Allows use of the `trace_macros` macro, which is a nasty - hack that will certainly be removed. - -* `unboxed_closures` - Rust's new closure design, which is currently a work in - progress feature with many known bugs. - -* `allow_internal_unstable` - Allows `macro_rules!` macros to be tagged with the - `#[allow_internal_unstable]` attribute, designed - to allow `std` macros to call - `#[unstable]`/feature-gated functionality - internally without imposing on callers - (i.e. making them behave like function calls in - terms of encapsulation). - -* `default_type_parameter_fallback` - Allows type parameter defaults to - influence type inference. - -* `stmt_expr_attributes` - Allows attributes on expressions. - -* `type_ascription` - Allows type ascription expressions `expr: Type`. - -* `abi_vectorcall` - Allows the usage of the vectorcall calling convention - (e.g. `extern "vectorcall" func fn_();`) - -* `abi_sysv64` - Allows the usage of the system V AMD64 calling convention - (e.g. `extern "sysv64" func fn_();`) - -If a feature is promoted to a language feature, then all existing programs will -start to receive compilation warnings about `#![feature]` directives which enabled -the new feature (because the directive is no longer necessary). However, if a -feature is decided to be removed from the language, errors will be issued (if -there isn't a parser error first). The directive in this case is no longer -necessary, and it's likely that existing code will break if the feature isn't -removed. - -If an unknown feature is found in a directive, it results in a compiler error. -An unknown feature is one which has never been recognized by the compiler. diff --git a/src/doc/reference/src/behavior-considered-undefined.md b/src/doc/reference/src/behavior-considered-undefined.md deleted file mode 100644 index b617ee3d78fa7..0000000000000 --- a/src/doc/reference/src/behavior-considered-undefined.md +++ /dev/null @@ -1,35 +0,0 @@ -## Behavior considered undefined - -The following is a list of behavior which is forbidden in all Rust code, -including within `unsafe` blocks and `unsafe` functions. Type checking provides -the guarantee that these issues are never caused by safe code. - -* Data races -* Dereferencing a null/dangling raw pointer -* Reads of [undef](http://llvm.org/docs/LangRef.html#undefined-values) - (uninitialized) memory -* Breaking the [pointer aliasing - rules](http://llvm.org/docs/LangRef.html#pointer-aliasing-rules) - with raw pointers (a subset of the rules used by C) -* `&mut T` and `&T` follow LLVM’s scoped [noalias] model, except if the `&T` - contains an `UnsafeCell`. Unsafe code must not violate these aliasing - guarantees. -* Mutating non-mutable data (that is, data reached through a shared reference or - data owned by a `let` binding), unless that data is contained within an `UnsafeCell`. -* Invoking undefined behavior via compiler intrinsics: - * Indexing outside of the bounds of an object with `std::ptr::offset` - (`offset` intrinsic), with - the exception of one byte past the end which is permitted. - * Using `std::ptr::copy_nonoverlapping_memory` (`memcpy32`/`memcpy64` - intrinsics) on overlapping buffers -* Invalid values in primitive types, even in private fields/locals: - * Dangling/null references or boxes - * A value other than `false` (0) or `true` (1) in a `bool` - * A discriminant in an `enum` not included in the type definition - * A value in a `char` which is a surrogate or above `char::MAX` - * Non-UTF-8 byte sequences in a `str` -* Unwinding into Rust from foreign code or unwinding from Rust into foreign - code. Rust's failure system is not compatible with exception handling in - other languages. Unwinding must be caught and handled at FFI boundaries. - -[noalias]: http://llvm.org/docs/LangRef.html#noalias diff --git a/src/doc/reference/src/behavior-not-considered-unsafe.md b/src/doc/reference/src/behavior-not-considered-unsafe.md deleted file mode 100644 index e16103372f552..0000000000000 --- a/src/doc/reference/src/behavior-not-considered-unsafe.md +++ /dev/null @@ -1,15 +0,0 @@ -## Behavior not considered unsafe - -This is a list of behavior not considered *unsafe* in Rust terms, but that may -be undesired. - -* Deadlocks -* Leaks of memory and other resources -* Exiting without calling destructors -* Integer overflow - - Overflow is considered "unexpected" behavior and is always user-error, - unless the `wrapping` primitives are used. In non-optimized builds, the compiler - will insert debug checks that panic on overflow, but in optimized builds overflow - instead results in wrapped values. See [RFC 560] for the rationale and more details. - -[RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md diff --git a/src/doc/reference/src/comments.md b/src/doc/reference/src/comments.md deleted file mode 100644 index 784e19affd9da..0000000000000 --- a/src/doc/reference/src/comments.md +++ /dev/null @@ -1,20 +0,0 @@ -# Comments - -Comments in Rust code follow the general C++ style of line (`//`) and -block (`/* ... */`) comment forms. Nested block comments are supported. - -Line comments beginning with exactly _three_ slashes (`///`), and block -comments (`/** ... */`), are interpreted as a special syntax for `doc` -[attributes]. That is, they are equivalent to writing -`#[doc="..."]` around the body of the comment, i.e., `/// Foo` turns into -`#[doc="Foo"]`. - -Line comments beginning with `//!` and block comments `/*! ... */` are -doc comments that apply to the parent of the comment, rather than the item -that follows. That is, they are equivalent to writing `#![doc="..."]` around -the body of the comment. `//!` comments are usually used to document -modules that occupy a source file. - -Non-doc comments are interpreted as a form of whitespace. - -[attributes]: attributes.html diff --git a/src/doc/reference/src/crates-and-source-files.md b/src/doc/reference/src/crates-and-source-files.md deleted file mode 100644 index d41a8dc964095..0000000000000 --- a/src/doc/reference/src/crates-and-source-files.md +++ /dev/null @@ -1,73 +0,0 @@ -# Crates and source files - -Although Rust, like any other language, can be implemented by an interpreter as -well as a compiler, the only existing implementation is a compiler, -and the language has -always been designed to be compiled. For these reasons, this section assumes a -compiler. - -Rust's semantics obey a *phase distinction* between compile-time and -run-time.[^phase-distinction] Semantic rules that have a *static -interpretation* govern the success or failure of compilation, while -semantic rules -that have a *dynamic interpretation* govern the behavior of the program at -run-time. - -The compilation model centers on artifacts called _crates_. Each compilation -processes a single crate in source form, and if successful, produces a single -crate in binary form: either an executable or some sort of -library.[^cratesourcefile] - -A _crate_ is a unit of compilation and linking, as well as versioning, -distribution and runtime loading. A crate contains a _tree_ of nested -[module] scopes. The top level of this tree is a module that is -anonymous (from the point of view of paths within the module) and any item -within a crate has a canonical [module path] denoting its location -within the crate's module tree. - -The Rust compiler is always invoked with a single source file as input, and -always produces a single output crate. The processing of that source file may -result in other source files being loaded as modules. Source files have the -extension `.rs`. - -A Rust source file describes a module, the name and location of which — -in the module tree of the current crate — are defined from outside the -source file: either by an explicit `mod_item` in a referencing source file, or -by the name of the crate itself. Every source file is a module, but not every -module needs its own source file: [module definitions][module] can be nested -within one file. - -Each source file contains a sequence of zero or more `item` definitions, and -may optionally begin with any number of [attributes] -that apply to the containing module, most of which influence the behavior of -the compiler. The anonymous crate module can have additional attributes that -apply to the crate as a whole. - -```rust,no_run -// Specify the crate name. -#![crate_name = "projx"] - -// Specify the type of output artifact. -#![crate_type = "lib"] - -// Turn on a warning. -// This can be done in any module, not just the anonymous crate module. -#![warn(non_camel_case_types)] -``` - -A crate that contains a `main` function can be compiled to an executable. If a -`main` function is present, its return type must be `()` -("[unit]") and it must take no arguments. - -[^phase-distinction]: This distinction would also exist in an interpreter. - Static checks like syntactic analysis, type checking, and lints should - happen before the program is executed regardless of when it is executed. - -[^cratesourcefile]: A crate is somewhat analogous to an *assembly* in the - ECMA-335 CLI model, a *library* in the SML/NJ Compilation Manager, a *unit* - in the Owens and Flatt module system, or a *configuration* in Mesa. - -[module]: items.html#modules -[module path]: paths.html -[attributes]: items-and-attributes.html -[unit]: types.html#tuple-types \ No newline at end of file diff --git a/src/doc/reference/src/expressions.md b/src/doc/reference/src/expressions.md deleted file mode 100644 index c9c0496dac61c..0000000000000 --- a/src/doc/reference/src/expressions.md +++ /dev/null @@ -1,863 +0,0 @@ -# Expressions - -An expression may have two roles: it always produces a *value*, and it may have -*effects* (otherwise known as "side effects"). An expression *evaluates to* a -value, and has effects during *evaluation*. Many expressions contain -sub-expressions (operands). The meaning of each kind of expression dictates -several things: - -* Whether or not to evaluate the sub-expressions when evaluating the expression -* The order in which to evaluate the sub-expressions -* How to combine the sub-expressions' values to obtain the value of the expression - -In this way, the structure of expressions dictates the structure of execution. -Blocks are just another kind of expression, so blocks, statements, expressions, -and blocks again can recursively nest inside each other to an arbitrary depth. - -### Lvalues, rvalues and temporaries - -Expressions are divided into two main categories: _lvalues_ and _rvalues_. -Likewise within each expression, sub-expressions may occur in _lvalue context_ -or _rvalue context_. The evaluation of an expression depends both on its own -category and the context it occurs within. - -An lvalue is an expression that represents a memory location. These expressions -are [paths](#path-expressions) (which refer to local variables, function and -method arguments, or static variables), dereferences (`*expr`), [indexing -expressions](#index-expressions) (`expr[expr]`), and [field -references](#field-expressions) (`expr.f`). All other expressions are rvalues. - -The left operand of an [assignment](#assignment-expressions) or -[compound-assignment](#compound-assignment-expressions) expression is -an lvalue context, as is the single operand of a unary -[borrow](#unary-operator-expressions). The discriminant or subject of -a [match expression](#match-expressions) may be an lvalue context, if -ref bindings are made, but is otherwise an rvalue context. All other -expression contexts are rvalue contexts. - -When an lvalue is evaluated in an _lvalue context_, it denotes a memory -location; when evaluated in an _rvalue context_, it denotes the value held _in_ -that memory location. - -#### Temporary lifetimes - -When an rvalue is used in an lvalue context, a temporary un-named -lvalue is created and used instead. The lifetime of temporary values -is typically the innermost enclosing statement; the tail expression of -a block is considered part of the statement that encloses the block. - -When a temporary rvalue is being created that is assigned into a `let` -declaration, however, the temporary is created with the lifetime of -the enclosing block instead, as using the enclosing statement (the -`let` declaration) would be a guaranteed error (since a pointer to the -temporary would be stored into a variable, but the temporary would be -freed before the variable could be used). The compiler uses simple -syntactic rules to decide which values are being assigned into a `let` -binding, and therefore deserve a longer temporary lifetime. - -Here are some examples: - -- `let x = foo(&temp())`. The expression `temp()` is an rvalue. As it - is being borrowed, a temporary is created which will be freed after - the innermost enclosing statement (the `let` declaration, in this case). -- `let x = temp().foo()`. This is the same as the previous example, - except that the value of `temp()` is being borrowed via autoref on a - method-call. Here we are assuming that `foo()` is an `&self` method - defined in some trait, say `Foo`. In other words, the expression - `temp().foo()` is equivalent to `Foo::foo(&temp())`. -- `let x = &temp()`. Here, the same temporary is being assigned into - `x`, rather than being passed as a parameter, and hence the - temporary's lifetime is considered to be the enclosing block. -- `let x = SomeStruct { foo: &temp() }`. As in the previous case, the - temporary is assigned into a struct which is then assigned into a - binding, and hence it is given the lifetime of the enclosing block. -- `let x = [ &temp() ]`. As in the previous case, the - temporary is assigned into an array which is then assigned into a - binding, and hence it is given the lifetime of the enclosing block. -- `let ref x = temp()`. In this case, the temporary is created using a ref binding, - but the result is the same: the lifetime is extended to the enclosing block. - -### Moved and copied types - -When a [local variable](variables.html) is used as an -[rvalue](expressions.html#lvalues-rvalues-and-temporaries), the variable will -be copied if its type implements `Copy`. All others are moved. - -## Literal expressions - -A _literal expression_ consists of one of the [literal](tokens.html#literals) forms -described earlier. It directly describes a number, character, string, boolean -value, or the unit value. - -```text -(); // unit type -"hello"; // string type -'5'; // character type -5; // integer type -``` - -## Path expressions - -A [path](paths.html) used as an expression context denotes either a local -variable or an item. Path expressions are -[lvalues](expressions.html#lvalues-rvalues-and-temporaries). - -## Tuple expressions - -Tuples are written by enclosing zero or more comma-separated expressions in -parentheses. They are used to create [tuple-typed](types.html#tuple-types) -values. - -```{.tuple} -(0.0, 4.5); -("a", 4usize, true); -``` - -You can disambiguate a single-element tuple from a value in parentheses with a -comma: - -``` -(0,); // single-element tuple -(0); // zero in parentheses -``` - -## Struct expressions - -There are several forms of struct expressions. A _struct expression_ -consists of the [path](paths.html) of a [struct item](items.html#structs), followed -by a brace-enclosed list of zero or more comma-separated name-value pairs, -providing the field values of a new instance of the struct. A field name can be -any identifier, and is separated from its value expression by a colon. The -location denoted by a struct field is mutable if and only if the enclosing -struct is mutable. - -A _tuple struct expression_ consists of the [path](paths.html) of a [struct -item](items.html#structs), followed by a parenthesized list of one or more -comma-separated expressions (in other words, the path of a struct item followed -by a tuple expression). The struct item must be a tuple struct item. - -A _unit-like struct expression_ consists only of the [path](paths.html) of a -[struct item](items.html#structs). - -The following are examples of struct expressions: - -``` -# struct Point { x: f64, y: f64 } -# struct NothingInMe { } -# struct TuplePoint(f64, f64); -# mod game { pub struct User<'a> { pub name: &'a str, pub age: u32, pub score: usize } } -# struct Cookie; fn some_fn(t: T) {} -Point {x: 10.0, y: 20.0}; -NothingInMe {}; -TuplePoint(10.0, 20.0); -let u = game::User {name: "Joe", age: 35, score: 100_000}; -some_fn::(Cookie); -``` - -A struct expression forms a new value of the named struct type. Note -that for a given *unit-like* struct type, this will always be the same -value. - -A struct expression can terminate with the syntax `..` followed by an -expression to denote a functional update. The expression following `..` (the -base) must have the same struct type as the new struct type being formed. -The entire expression denotes the result of constructing a new struct (with -the same type as the base expression) with the given values for the fields that -were explicitly specified and the values in the base expression for all other -fields. - -``` -# struct Point3d { x: i32, y: i32, z: i32 } -let base = Point3d {x: 1, y: 2, z: 3}; -Point3d {y: 0, z: 10, .. base}; -``` - -#### Struct field init shorthand - -When initializing a data structure (struct, enum, union) with named fields, -it is allowed to write `fieldname` as a shorthand for `fieldname: fieldname`. -This allows a compact syntax with less duplication. - -Example: - -``` -# struct Point3d { x: i32, y: i32, z: i32 } -# let x = 0; -# let y_value = 0; -# let z = 0; -Point3d { x: x, y: y_value, z: z }; -Point3d { x, y: y_value, z }; -``` - -## Block expressions - -A _block expression_ is similar to a module in terms of the declarations that -are possible. Each block conceptually introduces a new namespace scope. Use -items can bring new names into scopes and declared items are in scope for only -the block itself. - -A block will execute each statement sequentially, and then execute the -expression (if given). If the block ends in a statement, its value is `()`: - -``` -let x: () = { println!("Hello."); }; -``` - -If it ends in an expression, its value and type are that of the expression: - -``` -let x: i32 = { println!("Hello."); 5 }; - -assert_eq!(5, x); -``` - -## Method-call expressions - -A _method call_ consists of an expression followed by a single dot, an -identifier, and a parenthesized expression-list. Method calls are resolved to -methods on specific traits, either statically dispatching to a method if the -exact `self`-type of the left-hand-side is known, or dynamically dispatching if -the left-hand-side expression is an indirect [trait -object](types.html#trait-objects). - -## Field expressions - -A _field expression_ consists of an expression followed by a single dot and an -identifier, when not immediately followed by a parenthesized expression-list -(the latter is a [method call expression](#method-call-expressions)). A field -expression denotes a field of a [struct](types.html#struct-types). - -```{.ignore .field} -mystruct.myfield; -foo().x; -(Struct {a: 10, b: 20}).a; -``` - -A field access is an [lvalue](expressions.html#lvalues-rvalues-and-temporaries) -referring to the value of that field. When the type providing the field -inherits mutability, it can be [assigned](#assignment-expressions) to. - -Also, if the type of the expression to the left of the dot is a -pointer, it is automatically dereferenced as many times as necessary -to make the field access possible. In cases of ambiguity, we prefer -fewer autoderefs to more. - -## Array expressions - -An [array](types.html#array-and-slice-types) _expression_ is written by -enclosing zero or more comma-separated expressions of uniform type in square -brackets. - -In the `[expr ';' expr]` form, the expression after the `';'` must be a -constant expression that can be evaluated at compile time, such as a -[literal](tokens.html#literals) or a [static item](items.html#static-items). - -``` -[1, 2, 3, 4]; -["a", "b", "c", "d"]; -[0; 128]; // array with 128 zeros -[0u8, 0u8, 0u8, 0u8]; -``` - -## Index expressions - -[Array](types.html#array-and-slice-types)-typed expressions can be indexed by -writing a square-bracket-enclosed expression (the index) after them. When the -array is mutable, the resulting -[lvalue](expressions.html#lvalues-rvalues-and-temporaries) can be assigned to. - -Indices are zero-based, and may be of any integral type. Vector access is -bounds-checked at compile-time for constant arrays being accessed with a -constant index value. Otherwise a check will be performed at run-time that -will put the thread in a _panicked state_ if it fails. - -```{should-fail} -([1, 2, 3, 4])[0]; - -let x = (["a", "b"])[10]; // compiler error: const index-expr is out of bounds - -let n = 10; -let y = (["a", "b"])[n]; // panics - -let arr = ["a", "b"]; -arr[10]; // panics -``` - -Also, if the type of the expression to the left of the brackets is a -pointer, it is automatically dereferenced as many times as necessary -to make the indexing possible. In cases of ambiguity, we prefer fewer -autoderefs to more. - -## Range expressions - -The `..` operator will construct an object of one of the `std::ops::Range` variants. - -``` -1..2; // std::ops::Range -3..; // std::ops::RangeFrom -..4; // std::ops::RangeTo -..; // std::ops::RangeFull -``` - -The following expressions are equivalent. - -``` -let x = std::ops::Range {start: 0, end: 10}; -let y = 0..10; - -assert_eq!(x, y); -``` - -Similarly, the `...` operator will construct an object of one of the -`std::ops::RangeInclusive` variants. - -``` -# #![feature(inclusive_range_syntax)] -1...2; // std::ops::RangeInclusive -...4; // std::ops::RangeToInclusive -``` - -The following expressions are equivalent. - -``` -# #![feature(inclusive_range_syntax, inclusive_range)] -let x = std::ops::RangeInclusive::NonEmpty {start: 0, end: 10}; -let y = 0...10; - -assert_eq!(x, y); -``` - -## Unary operator expressions - -Rust defines the following unary operators. With the exception of `?`, they are -all written as prefix operators, before the expression they apply to. - -* `-` - : Negation. Signed integer types and floating-point types support negation. It - is an error to apply negation to unsigned types; for example, the compiler - rejects `-1u32`. -* `*` - : Dereference. When applied to a [pointer](types.html#pointer-types) it - denotes the pointed-to location. For pointers to mutable locations, the - resulting [lvalue](expressions.html#lvalues-rvalues-and-temporaries) can be - assigned to. On non-pointer types, it calls the `deref` method of the - `std::ops::Deref` trait, or the `deref_mut` method of the - `std::ops::DerefMut` trait (if implemented by the type and required for an - outer expression that will or could mutate the dereference), and produces - the result of dereferencing the `&` or `&mut` borrowed pointer returned - from the overload method. -* `!` - : Logical negation. On the boolean type, this flips between `true` and - `false`. On integer types, this inverts the individual bits in the - two's complement representation of the value. -* `&` and `&mut` - : Borrowing. When applied to an lvalue, these operators produce a - reference (pointer) to the lvalue. The lvalue is also placed into - a borrowed state for the duration of the reference. For a shared - borrow (`&`), this implies that the lvalue may not be mutated, but - it may be read or shared again. For a mutable borrow (`&mut`), the - lvalue may not be accessed in any way until the borrow expires. - If the `&` or `&mut` operators are applied to an rvalue, a - temporary value is created; the lifetime of this temporary value - is defined by [syntactic rules](#temporary-lifetimes). -* `?` - : Propagating errors if applied to `Err(_)` and unwrapping if - applied to `Ok(_)`. Only works on the `Result` type, - and written in postfix notation. - -## Binary operator expressions - -Binary operators expressions are given in terms of [operator -precedence](#operator-precedence). - -### Arithmetic operators - -Binary arithmetic expressions are syntactic sugar for calls to built-in traits, -defined in the `std::ops` module of the `std` library. This means that -arithmetic operators can be overridden for user-defined types. The default -meaning of the operators on standard types is given here. - -* `+` - : Addition and array/string concatenation. - Calls the `add` method on the `std::ops::Add` trait. -* `-` - : Subtraction. - Calls the `sub` method on the `std::ops::Sub` trait. -* `*` - : Multiplication. - Calls the `mul` method on the `std::ops::Mul` trait. -* `/` - : Quotient. - Calls the `div` method on the `std::ops::Div` trait. -* `%` - : Remainder. - Calls the `rem` method on the `std::ops::Rem` trait. - -### Bitwise operators - -Like the [arithmetic operators](#arithmetic-operators), bitwise operators are -syntactic sugar for calls to methods of built-in traits. This means that -bitwise operators can be overridden for user-defined types. The default -meaning of the operators on standard types is given here. Bitwise `&`, `|` and -`^` applied to boolean arguments are equivalent to logical `&&`, `||` and `!=` -evaluated in non-lazy fashion. - -* `&` - : Bitwise AND. - Calls the `bitand` method of the `std::ops::BitAnd` trait. -* `|` - : Bitwise inclusive OR. - Calls the `bitor` method of the `std::ops::BitOr` trait. -* `^` - : Bitwise exclusive OR. - Calls the `bitxor` method of the `std::ops::BitXor` trait. -* `<<` - : Left shift. - Calls the `shl` method of the `std::ops::Shl` trait. -* `>>` - : Right shift (arithmetic). - Calls the `shr` method of the `std::ops::Shr` trait. - -### Lazy boolean operators - -The operators `||` and `&&` may be applied to operands of boolean type. The -`||` operator denotes logical 'or', and the `&&` operator denotes logical -'and'. They differ from `|` and `&` in that the right-hand operand is only -evaluated when the left-hand operand does not already determine the result of -the expression. That is, `||` only evaluates its right-hand operand when the -left-hand operand evaluates to `false`, and `&&` only when it evaluates to -`true`. - -### Comparison operators - -Comparison operators are, like the [arithmetic -operators](#arithmetic-operators), and [bitwise operators](#bitwise-operators), -syntactic sugar for calls to built-in traits. This means that comparison -operators can be overridden for user-defined types. The default meaning of the -operators on standard types is given here. - -* `==` - : Equal to. - Calls the `eq` method on the `std::cmp::PartialEq` trait. -* `!=` - : Unequal to. - Calls the `ne` method on the `std::cmp::PartialEq` trait. -* `<` - : Less than. - Calls the `lt` method on the `std::cmp::PartialOrd` trait. -* `>` - : Greater than. - Calls the `gt` method on the `std::cmp::PartialOrd` trait. -* `<=` - : Less than or equal. - Calls the `le` method on the `std::cmp::PartialOrd` trait. -* `>=` - : Greater than or equal. - Calls the `ge` method on the `std::cmp::PartialOrd` trait. - -### Type cast expressions - -A type cast expression is denoted with the binary operator `as`. - -Executing an `as` expression casts the value on the left-hand side to the type -on the right-hand side. - -An example of an `as` expression: - -``` -# fn sum(values: &[f64]) -> f64 { 0.0 } -# fn len(values: &[f64]) -> i32 { 0 } - -fn average(values: &[f64]) -> f64 { - let sum: f64 = sum(values); - let size: f64 = len(values) as f64; - sum / size -} -``` - -Some of the conversions which can be done through the `as` operator -can also be done implicitly at various points in the program, such as -argument passing and assignment to a `let` binding with an explicit -type. Implicit conversions are limited to "harmless" conversions that -do not lose information and which have minimal or no risk of -surprising side-effects on the dynamic execution semantics. - -### Assignment expressions - -An _assignment expression_ consists of an -[lvalue](expressions.html#lvalues-rvalues-and-temporaries) expression followed -by an equals sign (`=`) and an -[rvalue](expressions.html#lvalues-rvalues-and-temporaries) expression. - -Evaluating an assignment expression [either copies or -moves](#moved-and-copied-types) its right-hand operand to its left-hand -operand. - -``` -# let mut x = 0; -# let y = 0; -x = y; -``` - -### Compound assignment expressions - -The `+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, and `>>` operators may be -composed with the `=` operator. The expression `lval OP= val` is equivalent to -`lval = lval OP val`. For example, `x = x + 1` may be written as `x += 1`. - -Any such expression always has the [`unit`](types.html#tuple-types) type. - -### Operator precedence - -The precedence of Rust binary operators is ordered as follows, going from -strong to weak: - -```{.text .precedence} -as : -* / % -+ - -<< >> -& -^ -| -== != < > <= >= -&& -|| -.. ... -<- -= -``` - -Operators at the same precedence level are evaluated left-to-right. [Unary -operators](#unary-operator-expressions) have the same precedence level and are -stronger than any of the binary operators. - -## Grouped expressions - -An expression enclosed in parentheses evaluates to the result of the enclosed -expression. Parentheses can be used to explicitly specify evaluation order -within an expression. - -An example of a parenthesized expression: - -``` -let x: i32 = (2 + 3) * 4; -``` - - -## Call expressions - -A _call expression_ invokes a function, providing zero or more input variables -and an optional location to move the function's output into. If the function -eventually returns, then the expression completes. - -Some examples of call expressions: - -``` -# fn add(x: i32, y: i32) -> i32 { 0 } - -let x: i32 = add(1i32, 2i32); -let pi: Result = "3.14".parse(); -``` - -## Lambda expressions - -A _lambda expression_ (sometimes called an "anonymous function expression") -defines a function and denotes it as a value, in a single expression. A lambda -expression is a pipe-symbol-delimited (`|`) list of identifiers followed by an -expression. - -A lambda expression denotes a function that maps a list of parameters -(`ident_list`) onto the expression that follows the `ident_list`. The -identifiers in the `ident_list` are the parameters to the function. These -parameters' types need not be specified, as the compiler infers them from -context. - -Lambda expressions are most useful when passing functions as arguments to other -functions, as an abbreviation for defining and capturing a separate function. - -Significantly, lambda expressions _capture their environment_, which regular -[function definitions](items.html#functions) do not. The exact type of capture -depends on the [function type](types.html#function-types) inferred for the -lambda expression. In the simplest and least-expensive form (analogous to a -```|| { }``` expression), the lambda expression captures its environment by -reference, effectively borrowing pointers to all outer variables mentioned -inside the function. Alternately, the compiler may infer that a lambda -expression should copy or move values (depending on their type) from the -environment into the lambda expression's captured environment. A lambda can be -forced to capture its environment by moving values by prefixing it with the -`move` keyword. - -In this example, we define a function `ten_times` that takes a higher-order -function argument, and we then call it with a lambda expression as an argument, -followed by a lambda expression that moves values from its environment. - -``` -fn ten_times(f: F) where F: Fn(i32) { - for index in 0..10 { - f(index); - } -} - -ten_times(|j| println!("hello, {}", j)); - -let word = "konnichiwa".to_owned(); -ten_times(move |j| println!("{}, {}", word, j)); -``` - -## Infinite loops - -A `loop` expression denotes an infinite loop. - -A `loop` expression may optionally have a _label_. The label is written as -a lifetime preceding the loop expression, as in `'foo: loop{ }`. If a -label is present, then labeled `break` and `continue` expressions nested -within this loop may exit out of this loop or return control to its head. -See [break expressions](#break-expressions) and [continue -expressions](#continue-expressions). - -## `break` expressions - -A `break` expression has an optional _label_. If the label is absent, then -executing a `break` expression immediately terminates the innermost loop -enclosing it. It is only permitted in the body of a loop. If the label is -present, then `break 'foo` terminates the loop with label `'foo`, which need not -be the innermost label enclosing the `break` expression, but must enclose it. - -## `continue` expressions - -A `continue` expression has an optional _label_. If the label is absent, then -executing a `continue` expression immediately terminates the current iteration -of the innermost loop enclosing it, returning control to the loop *head*. In -the case of a `while` loop, the head is the conditional expression controlling -the loop. In the case of a `for` loop, the head is the call-expression -controlling the loop. If the label is present, then `continue 'foo` returns -control to the head of the loop with label `'foo`, which need not be the -innermost label enclosing the `continue` expression, but must enclose it. - -A `continue` expression is only permitted in the body of a loop. - -## `while` loops - -A `while` loop begins by evaluating the boolean loop conditional expression. -If the loop conditional expression evaluates to `true`, the loop body block -executes and control returns to the loop conditional expression. If the loop -conditional expression evaluates to `false`, the `while` expression completes. - -An example: - -``` -let mut i = 0; - -while i < 10 { - println!("hello"); - i = i + 1; -} -``` - -Like `loop` expressions, `while` loops can be controlled with `break` or -`continue`, and may optionally have a _label_. See [infinite -loops](#infinite-loops), [break expressions](#break-expressions), and -[continue expressions](#continue-expressions) for more information. - -## `for` expressions - -A `for` expression is a syntactic construct for looping over elements provided -by an implementation of `std::iter::IntoIterator`. - -An example of a `for` loop over the contents of an array: - -``` -# type Foo = i32; -# fn bar(f: &Foo) { } -# let a = 0; -# let b = 0; -# let c = 0; - -let v: &[Foo] = &[a, b, c]; - -for e in v { - bar(e); -} -``` - -An example of a for loop over a series of integers: - -``` -# fn bar(b:usize) { } -for i in 0..256 { - bar(i); -} -``` - -Like `loop` expressions, `for` loops can be controlled with `break` or -`continue`, and may optionally have a _label_. See [infinite -loops](#infinite-loops), [break expressions](#break-expressions), and -[continue expressions](#continue-expressions) for more information. - -## `if` expressions - -An `if` expression is a conditional branch in program control. The form of an -`if` expression is a condition expression, followed by a consequent block, any -number of `else if` conditions and blocks, and an optional trailing `else` -block. The condition expressions must have type `bool`. If a condition -expression evaluates to `true`, the consequent block is executed and any -subsequent `else if` or `else` block is skipped. If a condition expression -evaluates to `false`, the consequent block is skipped and any subsequent `else -if` condition is evaluated. If all `if` and `else if` conditions evaluate to -`false` then any `else` block is executed. - -## `match` expressions - -A `match` expression branches on a *pattern*. The exact form of matching that -occurs depends on the pattern. Patterns consist of some combination of -literals, destructured arrays or enum constructors, structs and tuples, -variable binding specifications, wildcards (`..`), and placeholders (`_`). A -`match` expression has a *head expression*, which is the value to compare to -the patterns. The type of the patterns must equal the type of the head -expression. - -In a pattern whose head expression has an `enum` type, a placeholder (`_`) -stands for a *single* data field, whereas a wildcard `..` stands for *all* the -fields of a particular variant. - -A `match` behaves differently depending on whether or not the head expression -is an [lvalue or an rvalue](expressions.html#lvalues-rvalues-and-temporaries). -If the head expression is an rvalue, it is first evaluated into a temporary -location, and the resulting value is sequentially compared to the patterns in -the arms until a match is found. The first arm with a matching pattern is -chosen as the branch target of the `match`, any variables bound by the pattern -are assigned to local variables in the arm's block, and control enters the -block. - -When the head expression is an lvalue, the match does not allocate a temporary -location (however, a by-value binding may copy or move from the lvalue). When -possible, it is preferable to match on lvalues, as the lifetime of these -matches inherits the lifetime of the lvalue, rather than being restricted to -the inside of the match. - -An example of a `match` expression: - -``` -let x = 1; - -match x { - 1 => println!("one"), - 2 => println!("two"), - 3 => println!("three"), - 4 => println!("four"), - 5 => println!("five"), - _ => println!("something else"), -} -``` - -Patterns that bind variables default to binding to a copy or move of the -matched value (depending on the matched value's type). This can be changed to -bind to a reference by using the `ref` keyword, or to a mutable reference using -`ref mut`. - -Subpatterns can also be bound to variables by the use of the syntax `variable @ -subpattern`. For example: - -``` -let x = 1; - -match x { - e @ 1 ... 5 => println!("got a range element {}", e), - _ => println!("anything"), -} -``` - -Patterns can also dereference pointers by using the `&`, `&mut` and `box` -symbols, as appropriate. For example, these two matches on `x: &i32` are -equivalent: - -``` -# let x = &3; -let y = match *x { 0 => "zero", _ => "some" }; -let z = match x { &0 => "zero", _ => "some" }; - -assert_eq!(y, z); -``` - -Multiple match patterns may be joined with the `|` operator. A range of values -may be specified with `...`. For example: - -``` -# let x = 2; - -let message = match x { - 0 | 1 => "not many", - 2 ... 9 => "a few", - _ => "lots" -}; -``` - -Range patterns only work on scalar types (like integers and characters; not -like arrays and structs, which have sub-components). A range pattern may not -be a sub-range of another range pattern inside the same `match`. - -Finally, match patterns can accept *pattern guards* to further refine the -criteria for matching a case. Pattern guards appear after the pattern and -consist of a bool-typed expression following the `if` keyword. A pattern guard -may refer to the variables bound within the pattern they follow. - -``` -# let maybe_digit = Some(0); -# fn process_digit(i: i32) { } -# fn process_other(i: i32) { } - -let message = match maybe_digit { - Some(x) if x < 10 => process_digit(x), - Some(x) => process_other(x), - None => panic!(), -}; -``` - -## `if let` expressions - -An `if let` expression is semantically identical to an `if` expression but in -place of a condition expression it expects a `let` statement with a refutable -pattern. If the value of the expression on the right hand side of the `let` -statement matches the pattern, the corresponding block will execute, otherwise -flow proceeds to the first `else` block that follows. - -``` -let dish = ("Ham", "Eggs"); - -// this body will be skipped because the pattern is refuted -if let ("Bacon", b) = dish { - println!("Bacon is served with {}", b); -} - -// this body will execute -if let ("Ham", b) = dish { - println!("Ham is served with {}", b); -} -``` - -## `while let` loops - -A `while let` loop is semantically identical to a `while` loop but in place of -a condition expression it expects `let` statement with a refutable pattern. If -the value of the expression on the right hand side of the `let` statement -matches the pattern, the loop body block executes and control returns to the -pattern matching statement. Otherwise, the while expression completes. - -## `return` expressions - -Return expressions are denoted with the keyword `return`. Evaluating a `return` -expression moves its argument into the designated output location for the -current function call, destroys the current function activation frame, and -transfers control to the caller frame. - -An example of a `return` expression: - -``` -fn max(a: i32, b: i32) -> i32 { - if a > b { - return a; - } - return b; -} -``` diff --git a/src/doc/reference/src/identifiers.md b/src/doc/reference/src/identifiers.md deleted file mode 100644 index de657e3e312d5..0000000000000 --- a/src/doc/reference/src/identifiers.md +++ /dev/null @@ -1,24 +0,0 @@ -# Identifiers - -An identifier is any nonempty Unicode[^non_ascii_idents] string of the following form: - -Either - - * The first character has property `XID_start` - * The remaining characters have property `XID_continue` - -Or - - * The first character is `_` - * The identifier is more than one character, `_` alone is not an identifier - * The remaining characters have property `XID_continue` - -that does _not_ occur in the set of [keywords]. - -> **Note**: `XID_start` and `XID_continue` as character properties cover the -> character ranges used to form the more familiar C and Java language-family -> identifiers. - -[keywords]: ../grammar.html#keywords -[^non_ascii_idents]: Non-ASCII characters in identifiers are currently feature - gated. This is expected to improve soon. diff --git a/src/doc/reference/src/influences.md b/src/doc/reference/src/influences.md deleted file mode 100644 index 46082bfc0b006..0000000000000 --- a/src/doc/reference/src/influences.md +++ /dev/null @@ -1,22 +0,0 @@ -# Influences - -Rust is not a particularly original language, with design elements coming from -a wide range of sources. Some of these are listed below (including elements -that have since been removed): - -* SML, OCaml: algebraic data types, pattern matching, type inference, - semicolon statement separation -* C++: references, RAII, smart pointers, move semantics, monomorphization, - memory model -* ML Kit, Cyclone: region based memory management -* Haskell (GHC): typeclasses, type families -* Newsqueak, Alef, Limbo: channels, concurrency -* Erlang: message passing, thread failure, linked thread failure, - lightweight concurrency -* Swift: optional bindings -* Scheme: hygienic macros -* C#: attributes -* Ruby: block syntax -* NIL, Hermes: typestate -* [Unicode Annex #31](http://www.unicode.org/reports/tr31/): identifier and - pattern syntax diff --git a/src/doc/reference/src/input-format.md b/src/doc/reference/src/input-format.md deleted file mode 100644 index 0dbba4be92a05..0000000000000 --- a/src/doc/reference/src/input-format.md +++ /dev/null @@ -1,10 +0,0 @@ -# Input format - -Rust input is interpreted as a sequence of Unicode code points encoded in UTF-8. -Most Rust grammar rules are defined in terms of printable ASCII-range -code points, but a small number are defined in terms of Unicode properties or -explicit code point lists. [^inputformat] - -[^inputformat]: Substitute definitions for the special Unicode productions are - provided to the grammar verifier, restricted to ASCII range, when verifying the - grammar in this document. diff --git a/src/doc/reference/src/introduction.md b/src/doc/reference/src/introduction.md deleted file mode 100644 index 3a00dfa4572bf..0000000000000 --- a/src/doc/reference/src/introduction.md +++ /dev/null @@ -1,31 +0,0 @@ -# Introduction - -This document is the primary reference for the Rust programming language. It -provides three kinds of material: - - - Chapters that informally describe each language construct and their use. - - Chapters that informally describe the memory model, concurrency model, - runtime services, linkage model and debugging facilities. - - Appendix chapters providing rationale and references to languages that - influenced the design. - -This document does not serve as an introduction to the language. Background -familiarity with the language is assumed. A separate [book] is available to -help acquire such background familiarity. - -This document also does not serve as a reference to the [standard] library -included in the language distribution. Those libraries are documented -separately by extracting documentation attributes from their source code. Many -of the features that one might expect to be language features are library -features in Rust, so what you're looking for may be there, not here. - -Finally, this document is not normative. It may include details that are -specific to `rustc` itself, and should not be taken as a specification for -the Rust language. We intend to produce such a document someday, but this -is what we have for now. - -You may also be interested in the [grammar]. - -[book]: ../book/index.html -[standard]: ../std/index.html -[grammar]: ../grammar.html diff --git a/src/doc/reference/src/items-and-attributes.md b/src/doc/reference/src/items-and-attributes.md deleted file mode 100644 index bd5018d69cc78..0000000000000 --- a/src/doc/reference/src/items-and-attributes.md +++ /dev/null @@ -1,7 +0,0 @@ -# Items and attributes - -Crates contain [items], each of which may have some number of -[attributes] attached to it. - -[items]: items.html -[attributes]: attributes.html diff --git a/src/doc/reference/src/items.md b/src/doc/reference/src/items.md deleted file mode 100644 index ba3f4195ba62d..0000000000000 --- a/src/doc/reference/src/items.md +++ /dev/null @@ -1,1091 +0,0 @@ -# Items - -An _item_ is a component of a crate. Items are organized within a crate by a -nested set of [modules]. Every crate has a single "outermost" -anonymous module; all further items within the crate have [paths] -within the module tree of the crate. - -[modules]: #modules -[paths]: paths.html - -Items are entirely determined at compile-time, generally remain fixed during -execution, and may reside in read-only memory. - -There are several kinds of item: - -* [`extern crate` declarations](#extern-crate-declarations) -* [`use` declarations](#use-declarations) -* [modules](#modules) -* [function definitions](#functions) -* [`extern` blocks](#external-blocks) -* [type definitions](../grammar.html#type-definitions) -* [struct definitions](#structs) -* [enumeration definitions](#enumerations) -* [constant items](#constant-items) -* [static items](#static-items) -* [trait definitions](#traits) -* [implementations](#implementations) - -Some items form an implicit scope for the declaration of sub-items. In other -words, within a function or module, declarations of items can (in many cases) -be mixed with the statements, control blocks, and similar artifacts that -otherwise compose the item body. The meaning of these scoped items is the same -as if the item was declared outside the scope — it is still a static item -— except that the item's *path name* within the module namespace is -qualified by the name of the enclosing item, or is private to the enclosing -item (in the case of functions). The grammar specifies the exact locations in -which sub-item declarations may appear. - -## Type Parameters - -All items except modules, constants and statics may be *parameterized* by type. -Type parameters are given as a comma-separated list of identifiers enclosed in -angle brackets (`<...>`), after the name of the item and before its definition. -The type parameters of an item are considered "part of the name", not part of -the type of the item. A referencing [path] must (in principle) provide -type arguments as a list of comma-separated types enclosed within angle -brackets, in order to refer to the type-parameterized item. In practice, the -type-inference system can usually infer such argument types from context. There -are no general type-parametric types, only type-parametric items. That is, Rust -has no notion of type abstraction: there are no higher-ranked (or "forall") types -abstracted over other types, though higher-ranked types do exist for lifetimes. - -[path]: paths.html - -## Modules - -A module is a container for zero or more [items]. - -[items]: items.html - -A _module item_ is a module, surrounded in braces, named, and prefixed with the -keyword `mod`. A module item introduces a new, named module into the tree of -modules making up a crate. Modules can nest arbitrarily. - -An example of a module: - -```rust -mod math { - type Complex = (f64, f64); - fn sin(f: f64) -> f64 { - /* ... */ -# panic!(); - } - fn cos(f: f64) -> f64 { - /* ... */ -# panic!(); - } - fn tan(f: f64) -> f64 { - /* ... */ -# panic!(); - } -} -``` - -Modules and types share the same namespace. Declaring a named type with -the same name as a module in scope is forbidden: that is, a type definition, -trait, struct, enumeration, or type parameter can't shadow the name of a module -in scope, or vice versa. - -A module without a body is loaded from an external file, by default with the -same name as the module, plus the `.rs` extension. When a nested submodule is -loaded from an external file, it is loaded from a subdirectory path that -mirrors the module hierarchy. - -```rust,ignore -// Load the `vec` module from `vec.rs` -mod vec; - -mod thread { - // Load the `local_data` module from `thread/local_data.rs` - // or `thread/local_data/mod.rs`. - mod local_data; -} -``` - -The directories and files used for loading external file modules can be -influenced with the `path` attribute. - -```rust,ignore -#[path = "thread_files"] -mod thread { - // Load the `local_data` module from `thread_files/tls.rs` - #[path = "tls.rs"] - mod local_data; -} -``` - -### Extern crate declarations - -An _`extern crate` declaration_ specifies a dependency on an external crate. -The external crate is then bound into the declaring scope as the `ident` -provided in the `extern_crate_decl`. - -The external crate is resolved to a specific `soname` at compile time, and a -runtime linkage requirement to that `soname` is passed to the linker for -loading at runtime. The `soname` is resolved at compile time by scanning the -compiler's library path and matching the optional `crateid` provided against -the `crateid` attributes that were declared on the external crate when it was -compiled. If no `crateid` is provided, a default `name` attribute is assumed, -equal to the `ident` given in the `extern_crate_decl`. - -Three examples of `extern crate` declarations: - -```rust,ignore -extern crate pcre; - -extern crate std; // equivalent to: extern crate std as std; - -extern crate std as ruststd; // linking to 'std' under another name -``` - -When naming Rust crates, hyphens are disallowed. However, Cargo packages may -make use of them. In such case, when `Cargo.toml` doesn't specify a crate name, -Cargo will transparently replace `-` with `_` (Refer to [RFC 940] for more -details). - -Here is an example: - -```rust,ignore -// Importing the Cargo package hello-world -extern crate hello_world; // hyphen replaced with an underscore -``` - -[RFC 940]: https://github.com/rust-lang/rfcs/blob/master/text/0940-hyphens-considered-harmful.md - -### Use declarations - -A _use declaration_ creates one or more local name bindings synonymous with -some other [path]. Usually a `use` declaration is used to shorten the -path required to refer to a module item. These declarations may appear in -[modules] and [blocks], usually at the top. - -[path]: paths.html -[modules]: #modules -[blocks]: ../grammar.html#block-expressions - -> **Note**: Unlike in many languages, -> `use` declarations in Rust do *not* declare linkage dependency with external crates. -> Rather, [`extern crate` declarations](#extern-crate-declarations) declare linkage dependencies. - -Use declarations support a number of convenient shortcuts: - -* Rebinding the target name as a new local name, using the syntax `use p::q::r as x;` -* Simultaneously binding a list of paths differing only in their final element, - using the glob-like brace syntax `use a::b::{c,d,e,f};` -* Binding all paths matching a given prefix, using the asterisk wildcard syntax - `use a::b::*;` -* Simultaneously binding a list of paths differing only in their final element - and their immediate parent module, using the `self` keyword, such as - `use a::b::{self, c, d};` - -An example of `use` declarations: - -```rust -use std::option::Option::{Some, None}; -use std::collections::hash_map::{self, HashMap}; - -fn foo(_: T){} -fn bar(map1: HashMap, map2: hash_map::HashMap){} - -fn main() { - // Equivalent to 'foo(vec![std::option::Option::Some(1.0f64), - // std::option::Option::None]);' - foo(vec![Some(1.0f64), None]); - - // Both `hash_map` and `HashMap` are in scope. - let map1 = HashMap::new(); - let map2 = hash_map::HashMap::new(); - bar(map1, map2); -} -``` - -Like items, `use` declarations are private to the containing module, by -default. Also like items, a `use` declaration can be public, if qualified by -the `pub` keyword. Such a `use` declaration serves to _re-export_ a name. A -public `use` declaration can therefore _redirect_ some public name to a -different target definition: even a definition with a private canonical path, -inside a different module. If a sequence of such redirections form a cycle or -cannot be resolved unambiguously, they represent a compile-time error. - -An example of re-exporting: - -```rust -# fn main() { } -mod quux { - pub use quux::foo::{bar, baz}; - - pub mod foo { - pub fn bar() { } - pub fn baz() { } - } -} -``` - -In this example, the module `quux` re-exports two public names defined in -`foo`. - -Also note that the paths contained in `use` items are relative to the crate -root. So, in the previous example, the `use` refers to `quux::foo::{bar, -baz}`, and not simply to `foo::{bar, baz}`. This also means that top-level -module declarations should be at the crate root if direct usage of the declared -modules within `use` items is desired. It is also possible to use `self` and -`super` at the beginning of a `use` item to refer to the current and direct -parent modules respectively. All rules regarding accessing declared modules in -`use` declarations apply to both module declarations and `extern crate` -declarations. - -An example of what will and will not work for `use` items: - -```rust -# #![allow(unused_imports)] -use foo::baz::foobaz; // good: foo is at the root of the crate - -mod foo { - - mod example { - pub mod iter {} - } - - use foo::example::iter; // good: foo is at crate root -// use example::iter; // bad: example is not at the crate root - use self::baz::foobaz; // good: self refers to module 'foo' - use foo::bar::foobar; // good: foo is at crate root - - pub mod bar { - pub fn foobar() { } - } - - pub mod baz { - use super::bar::foobar; // good: super refers to module 'foo' - pub fn foobaz() { } - } -} - -fn main() {} -``` - -## Functions - -A _function item_ defines a sequence of [statements] and a -final [expression], along with a name and a set of -parameters. Other than a name, all these are optional. -Functions are declared with the keyword `fn`. Functions may declare a -set of *input* [*variables*][variables] as parameters, through which the caller -passes arguments into the function, and the *output* [*type*][type] -of the value the function will return to its caller on completion. - -[statements]: statements.html -[expression]: expressions.html -[variables]: variables.html -[type]: types.html - -A function may also be copied into a first-class *value*, in which case the -value has the corresponding [*function type*][function type], and can be used -otherwise exactly as a function item (with a minor additional cost of calling -the function indirectly). - -[function type]: types.html#function-types - -Every control path in a function logically ends with a `return` expression or a -diverging expression. If the outermost block of a function has a -value-producing expression in its final-expression position, that expression is -interpreted as an implicit `return` expression applied to the final-expression. - -An example of a function: - -```rust -fn add(x: i32, y: i32) -> i32 { - x + y -} -``` - -As with `let` bindings, function arguments are irrefutable patterns, so any -pattern that is valid in a let binding is also valid as an argument. - -```rust -fn first((value, _): (i32, i32)) -> i32 { value } -``` - - -### Generic functions - -A _generic function_ allows one or more _parameterized types_ to appear in its -signature. Each type parameter must be explicitly declared in an -angle-bracket-enclosed and comma-separated list, following the function name. - -```rust,ignore -// foo is generic over A and B - -fn foo(x: A, y: B) { -``` - -Inside the function signature and body, the name of the type parameter can be -used as a type name. [Trait](#traits) bounds can be specified for type parameters -to allow methods with that trait to be called on values of that type. This is -specified using the `where` syntax: - -```rust,ignore -fn foo(x: T) where T: Debug { -``` - -When a generic function is referenced, its type is instantiated based on the -context of the reference. For example, calling the `foo` function here: - -```rust -use std::fmt::Debug; - -fn foo(x: &[T]) where T: Debug { - // details elided - # () -} - -foo(&[1, 2]); -``` - -will instantiate type parameter `T` with `i32`. - -The type parameters can also be explicitly supplied in a trailing -[path] component after the function name. This might be necessary if -there is not sufficient context to determine the type parameters. For example, -`mem::size_of::() == 4`. - -[path]: paths.html - -### Diverging functions - -A special kind of function can be declared with a `!` character where the -output type would normally be. For example: - -```rust -fn my_err(s: &str) -> ! { - println!("{}", s); - panic!(); -} -``` - -We call such functions "diverging" because they never return a value to the -caller. Every control path in a diverging function must end with a `panic!()` or -a call to another diverging function on every control path. The `!` annotation -does *not* denote a type. - -It might be necessary to declare a diverging function because as mentioned -previously, the typechecker checks that every control path in a function ends -with a [`return`] or diverging expression. So, if `my_err` -were declared without the `!` annotation, the following code would not -typecheck: - -[`return`]: expressions.html#return-expressions - -```rust -# fn my_err(s: &str) -> ! { panic!() } - -fn f(i: i32) -> i32 { - if i == 42 { - return 42; - } - else { - my_err("Bad number!"); - } -} -``` - -This will not compile without the `!` annotation on `my_err`, since the `else` -branch of the conditional in `f` does not return an `i32`, as required by the -signature of `f`. Adding the `!` annotation to `my_err` informs the -typechecker that, should control ever enter `my_err`, no further type judgments -about `f` need to hold, since control will never resume in any context that -relies on those judgments. Thus the return type on `f` only needs to reflect -the `if` branch of the conditional. - -### Extern functions - -Extern functions are part of Rust's foreign function interface, providing the -opposite functionality to [external blocks](#external-blocks). Whereas -external blocks allow Rust code to call foreign code, extern functions with -bodies defined in Rust code _can be called by foreign code_. They are defined -in the same way as any other Rust function, except that they have the `extern` -modifier. - -```rust -// Declares an extern fn, the ABI defaults to "C" -extern fn new_i32() -> i32 { 0 } - -// Declares an extern fn with "stdcall" ABI -extern "stdcall" fn new_i32_stdcall() -> i32 { 0 } -``` - -Unlike normal functions, extern fns have type `extern "ABI" fn()`. This is the -same type as the functions declared in an extern block. - -```rust -# extern fn new_i32() -> i32 { 0 } -let fptr: extern "C" fn() -> i32 = new_i32; -``` - -Extern functions may be called directly from Rust code as Rust uses large, -contiguous stack segments like C. - -## Type aliases - -A _type alias_ defines a new name for an existing [type]. Type -aliases are declared with the keyword `type`. Every value has a single, -specific type, but may implement several different traits, or be compatible with -several different type constraints. - -[type]: types.html - -For example, the following defines the type `Point` as a synonym for the type -`(u8, u8)`, the type of pairs of unsigned 8 bit integers: - -```rust -type Point = (u8, u8); -let p: Point = (41, 68); -``` - -Currently a type alias to an enum type cannot be used to qualify the -constructors: - -```rust -enum E { A } -type F = E; -let _: F = E::A; // OK -// let _: F = F::A; // Doesn't work -``` - -## Structs - -A _struct_ is a nominal [struct type] defined with the -keyword `struct`. - -An example of a `struct` item and its use: - -```rust -struct Point {x: i32, y: i32} -let p = Point {x: 10, y: 11}; -let px: i32 = p.x; -``` - -A _tuple struct_ is a nominal [tuple type], also defined with -the keyword `struct`. For example: - -[struct type]: types.html#struct-types -[tuple type]: types.html#tuple-types - -```rust -struct Point(i32, i32); -let p = Point(10, 11); -let px: i32 = match p { Point(x, _) => x }; -``` - -A _unit-like struct_ is a struct without any fields, defined by leaving off -the list of fields entirely. Such a struct implicitly defines a constant of -its type with the same name. For example: - -```rust -struct Cookie; -let c = [Cookie, Cookie {}, Cookie, Cookie {}]; -``` - -is equivalent to - -```rust -struct Cookie {} -const Cookie: Cookie = Cookie {}; -let c = [Cookie, Cookie {}, Cookie, Cookie {}]; -``` - -The precise memory layout of a struct is not specified. One can specify a -particular layout using the [`repr` attribute]. - -[`repr` attribute]: attributes.html#ffi-attributes - -## Enumerations - -An _enumeration_ is a simultaneous definition of a nominal [enumerated -type] as well as a set of *constructors*, that can be used -to create or pattern-match values of the corresponding enumerated type. - -[enumerated type]: types.html#enumerated-types - -Enumerations are declared with the keyword `enum`. - -An example of an `enum` item and its use: - -```rust -enum Animal { - Dog, - Cat, -} - -let mut a: Animal = Animal::Dog; -a = Animal::Cat; -``` - -Enumeration constructors can have either named or unnamed fields: - -```rust -enum Animal { - Dog (String, f64), - Cat { name: String, weight: f64 }, -} - -let mut a: Animal = Animal::Dog("Cocoa".to_string(), 37.2); -a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 }; -``` - -In this example, `Cat` is a _struct-like enum variant_, -whereas `Dog` is simply called an enum variant. - -Each enum value has a _discriminant_ which is an integer associated to it. You -can specify it explicitly: - -```rust -enum Foo { - Bar = 123, -} -``` - -The right hand side of the specification is interpreted as an `isize` value, -but the compiler is allowed to use a smaller type in the actual memory layout. -The [`repr` attribute] can be added in order to change -the type of the right hand side and specify the memory layout. - -[`repr` attribute]: attributes.html#ffi-attributes - -If a discriminant isn't specified, they start at zero, and add one for each -variant, in order. - -You can cast an enum to get its discriminant: - -```rust -# enum Foo { Bar = 123 } -let x = Foo::Bar as u32; // x is now 123u32 -``` - -This only works as long as none of the variants have data attached. If -it were `Bar(i32)`, this is disallowed. - -## Constant items - -A *constant item* is a named _constant value_ which is not associated with a -specific memory location in the program. Constants are essentially inlined -wherever they are used, meaning that they are copied directly into the relevant -context when used. References to the same constant are not necessarily -guaranteed to refer to the same memory address. - -Constant values must not have destructors, and otherwise permit most forms of -data. Constants may refer to the address of other constants, in which case the -address will have elided lifetimes where applicable, otherwise – in most cases – -defaulting to the `static` lifetime. (See below on [static lifetime elision].) -The compiler is, however, still at liberty to translate the constant many times, -so the address referred to may not be stable. - -[static lifetime elision]: #static-lifetime-elision - -Constants must be explicitly typed. The type may be `bool`, `char`, a number, or -a type derived from those primitive types. The derived types are references with -the `static` lifetime, fixed-size arrays, tuples, enum variants, and structs. - -```rust -const BIT1: u32 = 1 << 0; -const BIT2: u32 = 1 << 1; - -const BITS: [u32; 2] = [BIT1, BIT2]; -const STRING: &'static str = "bitstring"; - -struct BitsNStrings<'a> { - mybits: [u32; 2], - mystring: &'a str, -} - -const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings { - mybits: BITS, - mystring: STRING, -}; -``` - -## Static items - -A *static item* is similar to a *constant*, except that it represents a precise -memory location in the program. A static is never "inlined" at the usage site, -and all references to it refer to the same memory location. Static items have -the `static` lifetime, which outlives all other lifetimes in a Rust program. -Static items may be placed in read-only memory if they do not contain any -interior mutability. - -Statics may contain interior mutability through the `UnsafeCell` language item. -All access to a static is safe, but there are a number of restrictions on -statics: - -* Statics may not contain any destructors. -* The types of static values must ascribe to `Sync` to allow thread-safe access. -* Statics may not refer to other statics by value, only by reference. -* Constants cannot refer to statics. - -Constants should in general be preferred over statics, unless large amounts of -data are being stored, or single-address and mutability properties are required. - -### Mutable statics - -If a static item is declared with the `mut` keyword, then it is allowed to -be modified by the program. One of Rust's goals is to make concurrency bugs -hard to run into, and this is obviously a very large source of race conditions -or other bugs. For this reason, an `unsafe` block is required when either -reading or writing a mutable static variable. Care should be taken to ensure -that modifications to a mutable static are safe with respect to other threads -running in the same process. - -Mutable statics are still very useful, however. They can be used with C -libraries and can also be bound from C libraries (in an `extern` block). - -```rust -# fn atomic_add(_: &mut u32, _: u32) -> u32 { 2 } - -static mut LEVELS: u32 = 0; - -// This violates the idea of no shared state, and this doesn't internally -// protect against races, so this function is `unsafe` -unsafe fn bump_levels_unsafe1() -> u32 { - let ret = LEVELS; - LEVELS += 1; - return ret; -} - -// Assuming that we have an atomic_add function which returns the old value, -// this function is "safe" but the meaning of the return value may not be what -// callers expect, so it's still marked as `unsafe` -unsafe fn bump_levels_unsafe2() -> u32 { - return atomic_add(&mut LEVELS, 1); -} -``` - -Mutable statics have the same restrictions as normal statics, except that the -type of the value is not required to ascribe to `Sync`. - -#### `'static` lifetime elision - -[Unstable] Both constant and static declarations of reference types have -*implicit* `'static` lifetimes unless an explicit lifetime is specified. As -such, the constant declarations involving `'static` above may be written -without the lifetimes. Returning to our previous example: - -```rust -# #![feature(static_in_const)] -const BIT1: u32 = 1 << 0; -const BIT2: u32 = 1 << 1; - -const BITS: [u32; 2] = [BIT1, BIT2]; -const STRING: &str = "bitstring"; - -struct BitsNStrings<'a> { - mybits: [u32; 2], - mystring: &'a str, -} - -const BITS_N_STRINGS: BitsNStrings = BitsNStrings { - mybits: BITS, - mystring: STRING, -}; -``` - -Note that if the `static` or `const` items include function or closure -references, which themselves include references, the compiler will first try the -standard elision rules ([see discussion in the nomicon][elision-nomicon]). If it -is unable to resolve the lifetimes by its usual rules, it will default to using -the `'static` lifetime. By way of example: - -[elision-nomicon]: ../nomicon/lifetime-elision.html - -```rust,ignore -// Resolved as `fn<'a>(&'a str) -> &'a str`. -const RESOLVED_SINGLE: fn(&str) -> &str = .. - -// Resolved as `Fn<'a, 'b, 'c>(&'a Foo, &'b Bar, &'c Baz) -> usize`. -const RESOLVED_MULTIPLE: Fn(&Foo, &Bar, &Baz) -> usize = .. - -// There is insufficient information to bound the return reference lifetime -// relative to the argument lifetimes, so the signature is resolved as -// `Fn(&'static Foo, &'static Bar) -> &'static Baz`. -const RESOLVED_STATIC: Fn(&Foo, &Bar) -> &Baz = .. -``` - -### Traits - -A _trait_ describes an abstract interface that types can -implement. This interface consists of associated items, which come in -three varieties: - -- functions -- constants -- types - -Associated functions whose first parameter is named `self` are called -methods and may be invoked using `.` notation (e.g., `x.foo()`). - -All traits define an implicit type parameter `Self` that refers to -"the type that is implementing this interface". Traits may also -contain additional type parameters. These type parameters (including -`Self`) may be constrained by other traits and so forth as usual. - -Trait bounds on `Self` are considered "supertraits". These are -required to be acyclic. Supertraits are somewhat different from other -constraints in that they affect what methods are available in the -vtable when the trait is used as a [trait object]. - -Traits are implemented for specific types through separate -[implementations]. - -Consider the following trait: - -```rust -# type Surface = i32; -# type BoundingBox = i32; -trait Shape { - fn draw(&self, Surface); - fn bounding_box(&self) -> BoundingBox; -} -``` - -This defines a trait with two methods. All values that have -[implementations] of this trait in scope can have their -`draw` and `bounding_box` methods called, using `value.bounding_box()` -[syntax]. - -[trait object]: types.html#trait-objects -[implementations]: #implementations -[syntax]: expressions.html#method-call-expressions - -Traits can include default implementations of methods, as in: - -```rust -trait Foo { - fn bar(&self); - fn baz(&self) { println!("We called baz."); } -} -``` - -Here the `baz` method has a default implementation, so types that implement -`Foo` need only implement `bar`. It is also possible for implementing types -to override a method that has a default implementation. - -Type parameters can be specified for a trait to make it generic. These appear -after the trait name, using the same syntax used in [generic -functions](#generic-functions). - -```rust -trait Seq { - fn len(&self) -> u32; - fn elt_at(&self, n: u32) -> T; - fn iter(&self, F) where F: Fn(T); -} -``` - -It is also possible to define associated types for a trait. Consider the -following example of a `Container` trait. Notice how the type is available -for use in the method signatures: - -```rust -trait Container { - type E; - fn empty() -> Self; - fn insert(&mut self, Self::E); -} -``` - -In order for a type to implement this trait, it must not only provide -implementations for every method, but it must specify the type `E`. Here's -an implementation of `Container` for the standard library type `Vec`: - -```rust -# trait Container { -# type E; -# fn empty() -> Self; -# fn insert(&mut self, Self::E); -# } -impl Container for Vec { - type E = T; - fn empty() -> Vec { Vec::new() } - fn insert(&mut self, x: T) { self.push(x); } -} -``` - -Generic functions may use traits as _bounds_ on their type parameters. This -will have two effects: - -- Only types that have the trait may instantiate the parameter. -- Within the generic function, the methods of the trait can be - called on values that have the parameter's type. - -For example: - -```rust -# type Surface = i32; -# trait Shape { fn draw(&self, Surface); } -fn draw_twice(surface: Surface, sh: T) { - sh.draw(surface); - sh.draw(surface); -} -``` - -Traits also define a [trait object] with the same -name as the trait. Values of this type are created by coercing from a -pointer of some specific type to a pointer of trait type. For example, -`&T` could be coerced to `&Shape` if `T: Shape` holds (and similarly -for `Box`). This coercion can either be implicit or -[explicit]. Here is an example of an explicit -coercion: - -[trait object]: types.html#trait-objects -[explicit]: expressions.html#type-cast-expressions - -```rust -trait Shape { } -impl Shape for i32 { } -let mycircle = 0i32; -let myshape: Box = Box::new(mycircle) as Box; -``` - -The resulting value is a box containing the value that was cast, along with -information that identifies the methods of the implementation that was used. -Values with a trait type can have [methods called] on -them, for any method in the trait, and can be used to instantiate type -parameters that are bounded by the trait. - -[methods called]: expressions.html#method-call-expressions - -Trait methods may be static, which means that they lack a `self` argument. -This means that they can only be called with function call syntax (`f(x)`) and -not method call syntax (`obj.f()`). The way to refer to the name of a static -method is to qualify it with the trait name, treating the trait name like a -module. For example: - -```rust -trait Num { - fn from_i32(n: i32) -> Self; -} -impl Num for f64 { - fn from_i32(n: i32) -> f64 { n as f64 } -} -let x: f64 = Num::from_i32(42); -``` - -Traits may inherit from other traits. Consider the following example: - -```rust -trait Shape { fn area(&self) -> f64; } -trait Circle : Shape { fn radius(&self) -> f64; } -``` - -The syntax `Circle : Shape` means that types that implement `Circle` must also -have an implementation for `Shape`. Multiple supertraits are separated by `+`, -`trait Circle : Shape + PartialEq { }`. In an implementation of `Circle` for a -given type `T`, methods can refer to `Shape` methods, since the typechecker -checks that any type with an implementation of `Circle` also has an -implementation of `Shape`: - -```rust -struct Foo; - -trait Shape { fn area(&self) -> f64; } -trait Circle : Shape { fn radius(&self) -> f64; } -impl Shape for Foo { - fn area(&self) -> f64 { - 0.0 - } -} -impl Circle for Foo { - fn radius(&self) -> f64 { - println!("calling area: {}", self.area()); - - 0.0 - } -} - -let c = Foo; -c.radius(); -``` - -In type-parameterized functions, methods of the supertrait may be called on -values of subtrait-bound type parameters. Referring to the previous example of -`trait Circle : Shape`: - -```rust -# trait Shape { fn area(&self) -> f64; } -# trait Circle : Shape { fn radius(&self) -> f64; } -fn radius_times_area(c: T) -> f64 { - // `c` is both a Circle and a Shape - c.radius() * c.area() -} -``` - -Likewise, supertrait methods may also be called on trait objects. - -```rust,ignore -# trait Shape { fn area(&self) -> f64; } -# trait Circle : Shape { fn radius(&self) -> f64; } -# impl Shape for i32 { fn area(&self) -> f64 { 0.0 } } -# impl Circle for i32 { fn radius(&self) -> f64 { 0.0 } } -# let mycircle = 0i32; -let mycircle = Box::new(mycircle) as Box; -let nonsense = mycircle.radius() * mycircle.area(); -``` - -### Implementations - -An _implementation_ is an item that implements a [trait](#traits) for a -specific type. - -Implementations are defined with the keyword `impl`. - -```rust -# #[derive(Copy, Clone)] -# struct Point {x: f64, y: f64}; -# type Surface = i32; -# struct BoundingBox {x: f64, y: f64, width: f64, height: f64}; -# trait Shape { fn draw(&self, Surface); fn bounding_box(&self) -> BoundingBox; } -# fn do_draw_circle(s: Surface, c: Circle) { } -struct Circle { - radius: f64, - center: Point, -} - -impl Copy for Circle {} - -impl Clone for Circle { - fn clone(&self) -> Circle { *self } -} - -impl Shape for Circle { - fn draw(&self, s: Surface) { do_draw_circle(s, *self); } - fn bounding_box(&self) -> BoundingBox { - let r = self.radius; - BoundingBox { - x: self.center.x - r, - y: self.center.y - r, - width: 2.0 * r, - height: 2.0 * r, - } - } -} -``` - -It is possible to define an implementation without referring to a trait. The -methods in such an implementation can only be used as direct calls on the values -of the type that the implementation targets. In such an implementation, the -trait type and `for` after `impl` are omitted. Such implementations are limited -to nominal types (enums, structs, trait objects), and the implementation must -appear in the same crate as the `self` type: - -```rust -struct Point {x: i32, y: i32} - -impl Point { - fn log(&self) { - println!("Point is at ({}, {})", self.x, self.y); - } -} - -let my_point = Point {x: 10, y:11}; -my_point.log(); -``` - -When a trait _is_ specified in an `impl`, all methods declared as part of the -trait must be implemented, with matching types and type parameter counts. - -An implementation can take type parameters, which can be different from the -type parameters taken by the trait it implements. Implementation parameters -are written after the `impl` keyword. - -```rust -# trait Seq { fn dummy(&self, _: T) { } } -impl Seq for Vec { - /* ... */ -} -impl Seq for u32 { - /* Treat the integer as a sequence of bits */ -} -``` - -### External blocks - -External blocks form the basis for Rust's foreign function interface. -Declarations in an external block describe symbols in external, non-Rust -libraries. - -Functions within external blocks are declared in the same way as other Rust -functions, with the exception that they may not have a body and are instead -terminated by a semicolon. - -Functions within external blocks may be called by Rust code, just like -functions defined in Rust. The Rust compiler automatically translates between -the Rust ABI and the foreign ABI. - -Functions within external blocks may be variadic by specifying `...` after one -or more named arguments in the argument list: - -```rust,ignore -extern { - fn foo(x: i32, ...); -} -``` - -A number of [attributes] control the behavior of external blocks. - -[attributes]: attributes.html#ffi-attributes - -By default external blocks assume that the library they are calling uses the -standard C ABI on the specific platform. Other ABIs may be specified using an -`abi` string, as shown here: - -```rust,ignore -// Interface to the Windows API -extern "stdcall" { } -``` - -There are three ABI strings which are cross-platform, and which all compilers -are guaranteed to support: - -* `extern "Rust"` -- The default ABI when you write a normal `fn foo()` in any - Rust code. -* `extern "C"` -- This is the same as `extern fn foo()`; whatever the default - your C compiler supports. -* `extern "system"` -- Usually the same as `extern "C"`, except on Win32, in - which case it's `"stdcall"`, or what you should use to link to the Windows API - itself - -There are also some platform-specific ABI strings: - -* `extern "cdecl"` -- The default for x86\_32 C code. -* `extern "stdcall"` -- The default for the Win32 API on x86\_32. -* `extern "win64"` -- The default for C code on x86\_64 Windows. -* `extern "sysv64"` -- The default for C code on non-Windows x86\_64. -* `extern "aapcs"` -- The default for ARM. -* `extern "fastcall"` -- The `fastcall` ABI -- corresponds to MSVC's - `__fastcall` and GCC and clang's `__attribute__((fastcall))` -* `extern "vectorcall"` -- The `vectorcall` ABI -- corresponds to MSVC's - `__vectorcall` and clang's `__attribute__((vectorcall))` - -Finally, there are some rustc-specific ABI strings: - -* `extern "rust-intrinsic"` -- The ABI of rustc intrinsics. -* `extern "rust-call"` -- The ABI of the Fn::call trait functions. -* `extern "platform-intrinsic"` -- Specific platform intrinsics -- like, for - example, `sqrt` -- have this ABI. You should never have to deal with it. - -The `link` attribute allows the name of the library to be specified. When -specified the compiler will attempt to link against the native library of the -specified name. - -```rust,ignore -#[link(name = "crypto")] -extern { } -``` - -The type of a function declared in an extern block is `extern "abi" fn(A1, ..., -An) -> R`, where `A1...An` are the declared types of its arguments and `R` is -the declared return type. - -It is valid to add the `link` attribute on an empty extern block. You can use -this to satisfy the linking requirements of extern blocks elsewhere in your code -(including upstream crates) instead of adding the attribute to each extern block. diff --git a/src/doc/reference/src/lexical-structure.md b/src/doc/reference/src/lexical-structure.md deleted file mode 100644 index 5e1388e0d5a2b..0000000000000 --- a/src/doc/reference/src/lexical-structure.md +++ /dev/null @@ -1 +0,0 @@ -# Lexical structure diff --git a/src/doc/reference/src/linkage.md b/src/doc/reference/src/linkage.md deleted file mode 100644 index 28297a8ec1f36..0000000000000 --- a/src/doc/reference/src/linkage.md +++ /dev/null @@ -1,127 +0,0 @@ -# Linkage - -The Rust compiler supports various methods to link crates together both -statically and dynamically. This section will explore the various methods to -link Rust crates together, and more information about native libraries can be -found in the [FFI section of the book][ffi]. - -[ffi]: ../book/ffi.html - -In one session of compilation, the compiler can generate multiple artifacts -through the usage of either command line flags or the `crate_type` attribute. -If one or more command line flags are specified, all `crate_type` attributes will -be ignored in favor of only building the artifacts specified by command line. - -* `--crate-type=bin`, `#[crate_type = "bin"]` - A runnable executable will be - produced. This requires that there is a `main` function in the crate which - will be run when the program begins executing. This will link in all Rust and - native dependencies, producing a distributable binary. - -* `--crate-type=lib`, `#[crate_type = "lib"]` - A Rust library will be produced. - This is an ambiguous concept as to what exactly is produced because a library - can manifest itself in several forms. The purpose of this generic `lib` option - is to generate the "compiler recommended" style of library. The output library - will always be usable by rustc, but the actual type of library may change from - time-to-time. The remaining output types are all different flavors of - libraries, and the `lib` type can be seen as an alias for one of them (but the - actual one is compiler-defined). - -* `--crate-type=dylib`, `#[crate_type = "dylib"]` - A dynamic Rust library will - be produced. This is different from the `lib` output type in that this forces - dynamic library generation. The resulting dynamic library can be used as a - dependency for other libraries and/or executables. This output type will - create `*.so` files on linux, `*.dylib` files on osx, and `*.dll` files on - windows. - -* `--crate-type=staticlib`, `#[crate_type = "staticlib"]` - A static system - library will be produced. This is different from other library outputs in that - the Rust compiler will never attempt to link to `staticlib` outputs. The - purpose of this output type is to create a static library containing all of - the local crate's code along with all upstream dependencies. The static - library is actually a `*.a` archive on linux and osx and a `*.lib` file on - windows. This format is recommended for use in situations such as linking - Rust code into an existing non-Rust application because it will not have - dynamic dependencies on other Rust code. - -* `--crate-type=cdylib`, `#[crate_type = "cdylib"]` - A dynamic system - library will be produced. This is used when compiling Rust code as - a dynamic library to be loaded from another language. This output type will - create `*.so` files on Linux, `*.dylib` files on macOS, and `*.dll` files on - Windows. - -* `--crate-type=rlib`, `#[crate_type = "rlib"]` - A "Rust library" file will be - produced. This is used as an intermediate artifact and can be thought of as a - "static Rust library". These `rlib` files, unlike `staticlib` files, are - interpreted by the Rust compiler in future linkage. This essentially means - that `rustc` will look for metadata in `rlib` files like it looks for metadata - in dynamic libraries. This form of output is used to produce statically linked - executables as well as `staticlib` outputs. - -* `--crate-type=proc-macro`, `#[crate_type = "proc-macro"]` - The output - produced is not specified, but if a `-L` path is provided to it then the - compiler will recognize the output artifacts as a macro and it can be loaded - for a program. If a crate is compiled with the `proc-macro` crate type it - will forbid exporting any items in the crate other than those functions - tagged `#[proc_macro_derive]` and those functions must also be placed at the - crate root. Finally, the compiler will automatically set the - `cfg(proc_macro)` annotation whenever any crate type of a compilation is the - `proc-macro` crate type. - -Note that these outputs are stackable in the sense that if multiple are -specified, then the compiler will produce each form of output at once without -having to recompile. However, this only applies for outputs specified by the -same method. If only `crate_type` attributes are specified, then they will all -be built, but if one or more `--crate-type` command line flags are specified, -then only those outputs will be built. - -With all these different kinds of outputs, if crate A depends on crate B, then -the compiler could find B in various different forms throughout the system. The -only forms looked for by the compiler, however, are the `rlib` format and the -dynamic library format. With these two options for a dependent library, the -compiler must at some point make a choice between these two formats. With this -in mind, the compiler follows these rules when determining what format of -dependencies will be used: - -1. If a static library is being produced, all upstream dependencies are - required to be available in `rlib` formats. This requirement stems from the - reason that a dynamic library cannot be converted into a static format. - - Note that it is impossible to link in native dynamic dependencies to a static - library, and in this case warnings will be printed about all unlinked native - dynamic dependencies. - -2. If an `rlib` file is being produced, then there are no restrictions on what - format the upstream dependencies are available in. It is simply required that - all upstream dependencies be available for reading metadata from. - - The reason for this is that `rlib` files do not contain any of their upstream - dependencies. It wouldn't be very efficient for all `rlib` files to contain a - copy of `libstd.rlib`! - -3. If an executable is being produced and the `-C prefer-dynamic` flag is not - specified, then dependencies are first attempted to be found in the `rlib` - format. If some dependencies are not available in an rlib format, then - dynamic linking is attempted (see below). - -4. If a dynamic library or an executable that is being dynamically linked is - being produced, then the compiler will attempt to reconcile the available - dependencies in either the rlib or dylib format to create a final product. - - A major goal of the compiler is to ensure that a library never appears more - than once in any artifact. For example, if dynamic libraries B and C were - each statically linked to library A, then a crate could not link to B and C - together because there would be two copies of A. The compiler allows mixing - the rlib and dylib formats, but this restriction must be satisfied. - - The compiler currently implements no method of hinting what format a library - should be linked with. When dynamically linking, the compiler will attempt to - maximize dynamic dependencies while still allowing some dependencies to be - linked in via an rlib. - - For most situations, having all libraries available as a dylib is recommended - if dynamically linking. For other situations, the compiler will emit a - warning if it is unable to determine which formats to link each library with. - -In general, `--crate-type=bin` or `--crate-type=lib` should be sufficient for -all compilation needs, and the other options are just available if more -fine-grained control is desired over the output format of a Rust crate. diff --git a/src/doc/reference/src/macros-by-example.md b/src/doc/reference/src/macros-by-example.md deleted file mode 100644 index a007b232e4c97..0000000000000 --- a/src/doc/reference/src/macros-by-example.md +++ /dev/null @@ -1,88 +0,0 @@ -# Macros By Example - -`macro_rules` allows users to define syntax extension in a declarative way. We -call such extensions "macros by example" or simply "macros". - -Currently, macros can expand to expressions, statements, items, or patterns. - -(A `sep_token` is any token other than `*` and `+`. A `non_special_token` is -any token other than a delimiter or `$`.) - -The macro expander looks up macro invocations by name, and tries each macro -rule in turn. It transcribes the first successful match. Matching and -transcription are closely related to each other, and we will describe them -together. - -The macro expander matches and transcribes every token that does not begin with -a `$` literally, including delimiters. For parsing reasons, delimiters must be -balanced, but they are otherwise not special. - -In the matcher, `$` _name_ `:` _designator_ matches the nonterminal in the Rust -syntax named by _designator_. Valid designators are: - -* `item`: an [item] -* `block`: a [block] -* `stmt`: a [statement] -* `pat`: a [pattern] -* `expr`: an [expression] -* `ty`: a [type] -* `ident`: an [identifier] -* `path`: a [path] -* `tt`: a token tree (a single [token] by matching `()`, `[]`, or `{}`) -* `meta`: the contents of an [attribute] - -[item]: items.html -[block]: expressions.html#block-expressions -[statement]: statements.html -[pattern]: expressions.html#match-expressions -[expression]: expressions.html -[type]: types.html -[identifier]: identifiers.html -[path]: paths.html -[token]: tokens.html -[attribute]: attributes.html - -In the transcriber, the -designator is already known, and so only the name of a matched nonterminal comes -after the dollar sign. - -In both the matcher and transcriber, the Kleene star-like operator indicates -repetition. The Kleene star operator consists of `$` and parentheses, optionally -followed by a separator token, followed by `*` or `+`. `*` means zero or more -repetitions, `+` means at least one repetition. The parentheses are not matched or -transcribed. On the matcher side, a name is bound to _all_ of the names it -matches, in a structure that mimics the structure of the repetition encountered -on a successful match. The job of the transcriber is to sort that structure -out. - -The rules for transcription of these repetitions are called "Macro By Example". -Essentially, one "layer" of repetition is discharged at a time, and all of them -must be discharged by the time a name is transcribed. Therefore, `( $( $i:ident -),* ) => ( $i )` is an invalid macro, but `( $( $i:ident ),* ) => ( $( $i:ident -),* )` is acceptable (if trivial). - -When Macro By Example encounters a repetition, it examines all of the `$` -_name_ s that occur in its body. At the "current layer", they all must repeat -the same number of times, so ` ( $( $i:ident ),* ; $( $j:ident ),* ) => ( $( -($i,$j) ),* )` is valid if given the argument `(a,b,c ; d,e,f)`, but not -`(a,b,c ; d,e)`. The repetition walks through the choices at that layer in -lockstep, so the former input transcribes to `(a,d), (b,e), (c,f)`. - -Nested repetitions are allowed. - -### Parsing limitations - -The parser used by the macro system is reasonably powerful, but the parsing of -Rust syntax is restricted in two ways: - -1. Macro definitions are required to include suitable separators after parsing - expressions and other bits of the Rust grammar. This implies that - a macro definition like `$i:expr [ , ]` is not legal, because `[` could be part - of an expression. A macro definition like `$i:expr,` or `$i:expr;` would be legal, - however, because `,` and `;` are legal separators. See [RFC 550] for more information. -2. The parser must have eliminated all ambiguity by the time it reaches a `$` - _name_ `:` _designator_. This requirement most often affects name-designator - pairs when they occur at the beginning of, or immediately after, a `$(...)*`; - requiring a distinctive token in front can solve the problem. - -[RFC 550]: https://github.com/rust-lang/rfcs/blob/master/text/0550-macro-future-proofing.md diff --git a/src/doc/reference/src/macros.md b/src/doc/reference/src/macros.md deleted file mode 100644 index d64c40dcad835..0000000000000 --- a/src/doc/reference/src/macros.md +++ /dev/null @@ -1,17 +0,0 @@ -# Macros - -A number of minor features of Rust are not central enough to have their own -syntax, and yet are not implementable as functions. Instead, they are given -names, and invoked through a consistent syntax: `some_extension!(...)`. - -Users of `rustc` can define new macros in two ways: - -* [Macros] define new syntax in a higher-level, - declarative way. -* [Procedural Macros] can be used to implement custom derive. - -And one unstable way: [compiler plugins]. - -[Macros]: ../book/macros.html -[Procedural Macros]: ../book/procedural-macros.html -[compiler plugins]: ../unstable-book/plugin.html diff --git a/src/doc/reference/src/memory-allocation-and-lifetime.md b/src/doc/reference/src/memory-allocation-and-lifetime.md deleted file mode 100644 index 24addb1dd39d4..0000000000000 --- a/src/doc/reference/src/memory-allocation-and-lifetime.md +++ /dev/null @@ -1,13 +0,0 @@ -# Memory allocation and lifetime - -The _items_ of a program are those functions, modules and types that have their -value calculated at compile-time and stored uniquely in the memory image of the -rust process. Items are neither dynamically allocated nor freed. - -The _heap_ is a general term that describes boxes. The lifetime of an -allocation in the heap depends on the lifetime of the box values pointing to -it. Since box values may themselves be passed in and out of frames, or stored -in the heap, heap allocations may outlive the frame they are allocated within. -An allocation in the heap is guaranteed to reside at a single location in the -heap for the whole lifetime of the allocation - it will never be relocated as -a result of moving a box value. diff --git a/src/doc/reference/src/memory-model.md b/src/doc/reference/src/memory-model.md deleted file mode 100644 index aa57ae6ae9bea..0000000000000 --- a/src/doc/reference/src/memory-model.md +++ /dev/null @@ -1,10 +0,0 @@ -# Memory model - -A Rust program's memory consists of a static set of *items* and a *heap*. -Immutable portions of the heap may be safely shared between threads, mutable -portions may not be safely shared, but several mechanisms for effectively-safe -sharing of mutable values, built on unsafe code but enforcing a safe locking -discipline, exist in the standard library. - -Allocations in the stack consist of *variables*, and allocations in the heap -consist of *boxes*. diff --git a/src/doc/reference/src/memory-ownership.md b/src/doc/reference/src/memory-ownership.md deleted file mode 100644 index aed07ef2961a5..0000000000000 --- a/src/doc/reference/src/memory-ownership.md +++ /dev/null @@ -1,4 +0,0 @@ -## Memory ownership - -When a stack frame is exited, its local allocations are all released, and its -references to boxes are dropped. diff --git a/src/doc/reference/src/notation.md b/src/doc/reference/src/notation.md deleted file mode 100644 index 642bff440ad93..0000000000000 --- a/src/doc/reference/src/notation.md +++ /dev/null @@ -1 +0,0 @@ -# Notation diff --git a/src/doc/reference/src/paths.md b/src/doc/reference/src/paths.md deleted file mode 100644 index e9fd07e5664b9..0000000000000 --- a/src/doc/reference/src/paths.md +++ /dev/null @@ -1,105 +0,0 @@ -# Paths - -A _path_ is a sequence of one or more path components _logically_ separated by -a namespace qualifier (`::`). If a path consists of only one component, it may -refer to either an [item] or a [variable] in a local control -scope. If a path has multiple components, it refers to an item. - -[item]: items.html -[variable]: variables.html - -Every item has a _canonical path_ within its crate, but the path naming an item -is only meaningful within a given crate. There is no global namespace across -crates; an item's canonical path merely identifies it within the crate. - -Two examples of simple paths consisting of only identifier components: - -```{.ignore} -x; -x::y::z; -``` - -Path components are usually [identifiers], but they may -also include angle-bracket-enclosed lists of type arguments. In -[expression] context, the type argument list is given -after a `::` namespace qualifier in order to disambiguate it from a -relational expression involving the less-than symbol (`<`). In type -expression context, the final namespace qualifier is omitted. - -[identifiers]: identifiers.html -[expression]: expressions.html - -Two examples of paths with type arguments: - -```rust -# struct HashMap(K,V); -# fn f() { -# fn id(t: T) -> T { t } -type T = HashMap; // Type arguments used in a type expression -let x = id::(10); // Type arguments used in a call expression -# } -``` - -Paths can be denoted with various leading qualifiers to change the meaning of -how it is resolved: - -* Paths starting with `::` are considered to be global paths where the - components of the path start being resolved from the crate root. Each - identifier in the path must resolve to an item. - -```rust -mod a { - pub fn foo() {} -} -mod b { - pub fn foo() { - ::a::foo(); // call a's foo function - } -} -# fn main() {} -``` - -* Paths starting with the keyword `super` begin resolution relative to the - parent module. Each further identifier must resolve to an item. - -```rust -mod a { - pub fn foo() {} -} -mod b { - pub fn foo() { - super::a::foo(); // call a's foo function - } -} -# fn main() {} -``` - -* Paths starting with the keyword `self` begin resolution relative to the - current module. Each further identifier must resolve to an item. - -```rust -fn foo() {} -fn bar() { - self::foo(); -} -# fn main() {} -``` - -Additionally keyword `super` may be repeated several times after the first -`super` or `self` to refer to ancestor modules. - -```rust -mod a { - fn foo() {} - - mod b { - mod c { - fn foo() { - super::super::foo(); // call a's foo function - self::super::super::foo(); // call a's foo function - } - } - } -} -# fn main() {} -``` diff --git a/src/doc/reference/src/procedural-macros.md b/src/doc/reference/src/procedural-macros.md deleted file mode 100644 index b1fd35653d9e7..0000000000000 --- a/src/doc/reference/src/procedural-macros.md +++ /dev/null @@ -1,23 +0,0 @@ -## Procedural Macros - -"Procedural macros" are the second way to implement a macro. For now, the only -thing they can be used for is to implement derive on your own types. See -[the book][procedural macros] for a tutorial. - -[procedural macros]: ../book/procedural-macros.html - -Procedural macros involve a few different parts of the language and its -standard libraries. First is the `proc_macro` crate, included with Rust, -that defines an interface for building a procedural macro. The -`#[proc_macro_derive(Foo)]` attribute is used to mark the deriving -function. This function must have the type signature: - -```rust,ignore -use proc_macro::TokenStream; - -#[proc_macro_derive(Hello)] -pub fn hello_world(input: TokenStream) -> TokenStream -``` - -Finally, procedural macros must be in their own crate, with the `proc-macro` -crate type. diff --git a/src/doc/reference/src/special-traits.md b/src/doc/reference/src/special-traits.md deleted file mode 100644 index ae3eebe392d1d..0000000000000 --- a/src/doc/reference/src/special-traits.md +++ /dev/null @@ -1,3 +0,0 @@ -# Special traits - -Several traits define special evaluation behavior. diff --git a/src/doc/reference/src/statements-and-expressions.md b/src/doc/reference/src/statements-and-expressions.md deleted file mode 100644 index bb59108f17f32..0000000000000 --- a/src/doc/reference/src/statements-and-expressions.md +++ /dev/null @@ -1,11 +0,0 @@ -# Statements and expressions - -Rust is _primarily_ an expression language. This means that most forms of -value-producing or effect-causing evaluation are directed by the uniform syntax -category of _expressions_. Each kind of expression can typically _nest_ within -each other kind of expression, and rules for evaluation of expressions involve -specifying both the value produced by the expression and the order in which its -sub-expressions are themselves evaluated. - -In contrast, statements in Rust serve _mostly_ to contain and explicitly -sequence expression evaluation. diff --git a/src/doc/reference/src/statements.md b/src/doc/reference/src/statements.md deleted file mode 100644 index ebb7d8bfa7cd0..0000000000000 --- a/src/doc/reference/src/statements.md +++ /dev/null @@ -1,42 +0,0 @@ -# Statements - -A _statement_ is a component of a block, which is in turn a component of an -outer [expression](expressions.html) or [function](items.html#functions). - -Rust has two kinds of statement: [declaration -statements](#declaration-statements) and [expression -statements](#expression-statements). - -## Declaration statements - -A _declaration statement_ is one that introduces one or more *names* into the -enclosing statement block. The declared names may denote new variables or new -items. - -### Item declarations - -An _item declaration statement_ has a syntactic form identical to an -[item](items.html) declaration within a module. Declaring an item — a -function, enumeration, struct, type, static, trait, implementation or module -— locally within a statement block is simply a way of restricting its -scope to a narrow region containing all of its uses; it is otherwise identical -in meaning to declaring the item outside the statement block. - -> **Note**: there is no implicit capture of the function's dynamic environment when -> declaring a function-local item. - -### `let` statements - -A _`let` statement_ introduces a new set of variables, given by a pattern. The -pattern may be followed by a type annotation, and/or an initializer expression. -When no type annotation is given, the compiler will infer the type, or signal -an error if insufficient type information is available for definite inference. -Any variables introduced by a variable declaration are visible from the point of -declaration until the end of the enclosing block scope. - -## Expression statements - -An _expression statement_ is one that evaluates an [expression](expressions.html) -and ignores its result. The type of an expression statement `e;` is always -`()`, regardless of the type of `e`. As a rule, an expression statement's -purpose is to trigger the effects of evaluating its expression. diff --git a/src/doc/reference/src/string-table-productions.md b/src/doc/reference/src/string-table-productions.md deleted file mode 100644 index 4e9742e3bbb80..0000000000000 --- a/src/doc/reference/src/string-table-productions.md +++ /dev/null @@ -1,18 +0,0 @@ -# String table productions - -Some rules in the grammar — notably [unary -operators], [binary operators], and [keywords][keywords] — are -given in a simplified form: as a listing of a table of unquoted, printable -whitespace-separated strings. These cases form a subset of the rules regarding -the [token][tokens] rule, and are assumed to be the result of a -lexical-analysis phase feeding the parser, driven by a DFA, operating over the -disjunction of all such string table entries. - -When such a string enclosed in double-quotes (`"`) occurs inside the grammar, -it is an implicit reference to a single member of such a string table -production. See [tokens] for more information. - -[binary operators]: expressions.html#binary-operator-expressions -[keywords]: ../grammar.html#keywords -[tokens]: tokens.html -[unary operators]: expressions.html#unary-operator-expressions \ No newline at end of file diff --git a/src/doc/reference/src/subtyping.md b/src/doc/reference/src/subtyping.md deleted file mode 100644 index a43b041a69338..0000000000000 --- a/src/doc/reference/src/subtyping.md +++ /dev/null @@ -1,19 +0,0 @@ -# Subtyping - -Subtyping is implicit and can occur at any stage in type checking or -inference. Subtyping in Rust is very restricted and occurs only due to -variance with respect to lifetimes and between types with higher ranked -lifetimes. If we were to erase lifetimes from types, then the only subtyping -would be due to type equality. - -Consider the following example: string literals always have `'static` -lifetime. Nevertheless, we can assign `s` to `t`: - -``` -fn bar<'a>() { - let s: &'static str = "hi"; - let t: &'a str = s; -} -``` -Since `'static` "lives longer" than `'a`, `&'static str` is a subtype of -`&'a str`. diff --git a/src/doc/reference/src/the-copy-trait.md b/src/doc/reference/src/the-copy-trait.md deleted file mode 100644 index d593165e48d58..0000000000000 --- a/src/doc/reference/src/the-copy-trait.md +++ /dev/null @@ -1,4 +0,0 @@ -# The `Copy` trait - -The `Copy` trait changes the semantics of a type implementing it. Values whose -type implements `Copy` are copied rather than moved upon assignment. diff --git a/src/doc/reference/src/the-deref-trait.md b/src/doc/reference/src/the-deref-trait.md deleted file mode 100644 index a4d84ab83ea67..0000000000000 --- a/src/doc/reference/src/the-deref-trait.md +++ /dev/null @@ -1,7 +0,0 @@ -# The `Deref` trait - -The `Deref` trait allows a type to implicitly implement all the methods -of the type `U`. When attempting to resolve a method call, the compiler will search -the top-level type for the implementation of the called method. If no such method is -found, `.deref()` is called and the compiler continues to search for the method -implementation in the returned type `U`. diff --git a/src/doc/reference/src/the-drop-trait.md b/src/doc/reference/src/the-drop-trait.md deleted file mode 100644 index 42bf6eb0f2014..0000000000000 --- a/src/doc/reference/src/the-drop-trait.md +++ /dev/null @@ -1,4 +0,0 @@ -# The `Drop` trait - -The `Drop` trait provides a destructor, to be run whenever a value of this type -is to be destroyed. diff --git a/src/doc/reference/src/the-send-trait.md b/src/doc/reference/src/the-send-trait.md deleted file mode 100644 index 9ec669289a567..0000000000000 --- a/src/doc/reference/src/the-send-trait.md +++ /dev/null @@ -1,4 +0,0 @@ -# The `Send` trait - -The `Send` trait indicates that a value of this type is safe to send from one -thread to another. diff --git a/src/doc/reference/src/the-sized-trait.md b/src/doc/reference/src/the-sized-trait.md deleted file mode 100644 index a2aa17c95f295..0000000000000 --- a/src/doc/reference/src/the-sized-trait.md +++ /dev/null @@ -1,3 +0,0 @@ -# The `Sized` trait - -The `Sized` trait indicates that the size of this type is known at compile-time. diff --git a/src/doc/reference/src/the-sync-trait.md b/src/doc/reference/src/the-sync-trait.md deleted file mode 100644 index fd9365134b292..0000000000000 --- a/src/doc/reference/src/the-sync-trait.md +++ /dev/null @@ -1,4 +0,0 @@ -# The `Sync` trait - -The `Sync` trait indicates that a value of this type is safe to share between -multiple threads. diff --git a/src/doc/reference/src/theme/book.css b/src/doc/reference/src/theme/book.css deleted file mode 100644 index ee92e2f8710a9..0000000000000 --- a/src/doc/reference/src/theme/book.css +++ /dev/null @@ -1,798 +0,0 @@ -html, -body { - font-family: "Open Sans", sans-serif; - color: #333; -} -.left { - float: left; -} -.right { - float: right; -} -.hidden { - display: none; -} -h2, -h3 { - margin-top: 2.5em; -} -h4, -h5 { - margin-top: 2em; -} -.header + .header h3, -.header + .header h4, -.header + .header h5 { - margin-top: 1em; -} -table { - margin: 0 auto; - border-collapse: collapse; -} -table td { - padding: 3px 20px; - border: 1px solid; -} -table thead td { - font-weight: 700; -} -.sidebar { - position: absolute; - left: 0; - top: 0; - bottom: 0; - width: 300px; - overflow-y: auto; - padding: 10px 10px; - font-size: 0.875em; - -webkit-box-sizing: border-box; - -moz-box-sizing: border-box; - box-sizing: border-box; - -webkit-overflow-scrolling: touch; - -webkit-transition: left 0.5s; - -moz-transition: left 0.5s; - -o-transition: left 0.5s; - -ms-transition: left 0.5s; - transition: left 0.5s; -} -@media only screen and (max-width: 1060px) { - .sidebar { - left: -300px; - } -} -.sidebar code { - line-height: 2em; -} -.sidebar-hidden .sidebar { - left: -300px; -} -.sidebar-visible .sidebar { - left: 0; -} -.chapter { - list-style: none outside none; - padding-left: 0; - line-height: 1.9em; -} -.chapter li a { - padding: 5px 0; - text-decoration: none; -} -.chapter li a:hover { - text-decoration: none; -} -.chapter .spacer { - width: 100%; - height: 3px; - margin: 10px 0px; -} -.section { - list-style: none outside none; - padding-left: 20px; - line-height: 2.5em; -} -.section li { - -o-text-overflow: ellipsis; - text-overflow: ellipsis; - overflow: hidden; - white-space: nowrap; -} -.page-wrapper { - position: absolute; - overflow-y: auto; - left: 315px; - right: 0; - top: 0; - bottom: 0; - -webkit-box-sizing: border-box; - -moz-box-sizing: border-box; - box-sizing: border-box; - -webkit-overflow-scrolling: touch; - min-height: 100%; - -webkit-transition: left 0.5s; - -moz-transition: left 0.5s; - -o-transition: left 0.5s; - -ms-transition: left 0.5s; - transition: left 0.5s; -} -@media only screen and (max-width: 1060px) { - .page-wrapper { - left: 15px; - padding-right: 15px; - } -} -.sidebar-hidden .page-wrapper { - left: 15px; -} -.sidebar-visible .page-wrapper { - left: 315px; -} -.page { - position: absolute; - top: 0; - right: 0; - left: 0; - bottom: 0; - padding-right: 15px; - overflow-y: auto; -} -.content { - margin-left: auto; - margin-right: auto; - max-width: 750px; - padding-bottom: 50px; -} -.content a { - text-decoration: none; -} -.content a:hover { - text-decoration: underline; -} -.content img { - max-width: 100%; -} -.menu-bar { - position: relative; - height: 50px; -} -.menu-bar i { - position: relative; - margin: 0 10px; - z-index: 10; - line-height: 50px; - -webkit-transition: color 0.5s; - -moz-transition: color 0.5s; - -o-transition: color 0.5s; - -ms-transition: color 0.5s; - transition: color 0.5s; -} -.menu-bar i:hover { - cursor: pointer; -} -.menu-bar .left-buttons { - float: left; -} -.menu-bar .right-buttons { - float: right; -} -.menu-title { - display: inline-block; - font-weight: 200; - font-size: 20px; - line-height: 50px; - position: absolute; - top: 0; - left: 0; - right: 0; - bottom: 0; - text-align: center; - margin: 0; - opacity: 0; - -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; - filter: alpha(opacity=0); - -webkit-transition: opacity 0.5s ease-in-out; - -moz-transition: opacity 0.5s ease-in-out; - -o-transition: opacity 0.5s ease-in-out; - -ms-transition: opacity 0.5s ease-in-out; - transition: opacity 0.5s ease-in-out; -} -.menu-bar:hover .menu-title { - opacity: 1; - -ms-filter: none; - filter: none; -} -.nav-chapters { - font-size: 2.5em; - text-align: center; - text-decoration: none; - position: absolute; - top: 50px /* Height of menu-bar */; - bottom: 0; - margin: 0; - max-width: 150px; - min-width: 90px; - display: -webkit-box; - display: -moz-box; - display: -webkit-flex; - display: -ms-flexbox; - display: box; - display: flex; - -webkit-box-pack: center; - -moz-box-pack: center; - -o-box-pack: center; - -ms-flex-pack: center; - -webkit-justify-content: center; - justify-content: center; - -ms-flex-line-pack: center; - -webkit-align-content: center; - align-content: center; - -webkit-box-orient: vertical; - -moz-box-orient: vertical; - -o-box-orient: vertical; - -webkit-flex-direction: column; - -ms-flex-direction: column; - flex-direction: column; - -webkit-transition: color 0.5s; - -moz-transition: color 0.5s; - -o-transition: color 0.5s; - -ms-transition: color 0.5s; - transition: color 0.5s; -} -.mobile-nav-chapters { - display: none; -} -.nav-chapters:hover { - text-decoration: none; -} -.previous { - left: 0; -} -.next { - right: 15px; -} -.theme-popup { - position: relative; - left: 10px; - z-index: 1000; - -webkit-border-radius: 4px; - border-radius: 4px; - font-size: 0.7em; -} -.theme-popup .theme { - margin: 0; - padding: 2px 10px; - line-height: 25px; - white-space: nowrap; -} -.theme-popup .theme:hover:first-child { - border-top-left-radius: inherit; - border-top-right-radius: inherit; -} -.theme-popup .theme:hover:last-child { - border-bottom-left-radius: inherit; - border-bottom-right-radius: inherit; -} - -@media only screen and (max-width: 1250px) { - .nav-chapters { - display: none; - } - .mobile-nav-chapters { - font-size: 2.5em; - text-align: center; - text-decoration: none; - max-width: 150px; - min-width: 90px; - -webkit-box-pack: center; - -moz-box-pack: center; - -o-box-pack: center; - -ms-flex-pack: center; - -webkit-justify-content: center; - justify-content: center; - -ms-flex-line-pack: center; - -webkit-align-content: center; - align-content: center; - position: relative; - display: inline-block; - margin-bottom: 50px; - -webkit-border-radius: 5px; - border-radius: 5px; - } - .next { - float: right; - } - .previous { - float: left; - } -} -.light { - color: #333; - background-color: #fff; -/* Inline code */ -} -.light .content .header:link, -.light .content .header:visited { - color: #333; - pointer: cursor; -} -.light .content .header:link:hover, -.light .content .header:visited:hover { - text-decoration: none; -} -.light .sidebar { - background-color: #fafafa; - color: #364149; -} -.light .chapter li { - color: #aaa; -} -.light .chapter li a { - color: #364149; -} -.light .chapter li .active, -.light .chapter li a:hover { -/* Animate color change */ - color: #008cff; -} -.light .chapter .spacer { - background-color: #f4f4f4; -} -.light .menu-bar, -.light .menu-bar:visited, -.light .nav-chapters, -.light .nav-chapters:visited, -.light .mobile-nav-chapters, -.light .mobile-nav-chapters:visited { - color: #ccc; -} -.light .menu-bar i:hover, -.light .nav-chapters:hover, -.light .mobile-nav-chapters i:hover { - color: #333; -} -.light .mobile-nav-chapters i:hover { - color: #364149; -} -.light .mobile-nav-chapters { - background-color: #fafafa; -} -.light .content a:link, -.light a:visited { - color: #4183c4; -} -.light .theme-popup { - color: #333; - background: #fafafa; - border: 1px solid #ccc; -} -.light .theme-popup .theme:hover { - background-color: #e6e6e6; -} -.light .theme-popup .default { - color: #ccc; -} -.light blockquote { - margin: 20px 0; - padding: 0 20px; - color: #333; - background-color: #f2f7f9; - border-top: 0.1em solid #e1edf1; - border-bottom: 0.1em solid #e1edf1; -} -.light table td { - border-color: #f2f2f2; -} -.light table tbody tr:nth-child(2n) { - background: #f7f7f7; -} -.light table thead { - background: #ccc; -} -.light table thead td { - border: none; -} -.light table thead tr { - border: 1px #ccc solid; -} -.light :not(pre) > .hljs { - display: inline-block; - vertical-align: middle; - padding: 0.1em 0.3em; - -webkit-border-radius: 3px; - border-radius: 3px; -} -.light pre { - position: relative; -} -.light pre > .buttons { - position: absolute; - right: 5px; - top: 5px; - color: #364149; - cursor: pointer; -} -.light pre > .buttons :hover { - color: #008cff; -} -.light pre > .buttons i { - margin-left: 8px; -} -.light pre > .result { - margin-top: 10px; -} -.coal { - color: #98a3ad; - background-color: #141617; -/* Inline code */ -} -.coal .content .header:link, -.coal .content .header:visited { - color: #98a3ad; - pointer: cursor; -} -.coal .content .header:link:hover, -.coal .content .header:visited:hover { - text-decoration: none; -} -.coal .sidebar { - background-color: #292c2f; - color: #a1adb8; -} -.coal .chapter li { - color: #505254; -} -.coal .chapter li a { - color: #a1adb8; -} -.coal .chapter li .active, -.coal .chapter li a:hover { -/* Animate color change */ - color: #3473ad; -} -.coal .chapter .spacer { - background-color: #393939; -} -.coal .menu-bar, -.coal .menu-bar:visited, -.coal .nav-chapters, -.coal .nav-chapters:visited, -.coal .mobile-nav-chapters, -.coal .mobile-nav-chapters:visited { - color: #43484d; -} -.coal .menu-bar i:hover, -.coal .nav-chapters:hover, -.coal .mobile-nav-chapters i:hover { - color: #b3c0cc; -} -.coal .mobile-nav-chapters i:hover { - color: #a1adb8; -} -.coal .mobile-nav-chapters { - background-color: #292c2f; -} -.coal .content a:link, -.coal a:visited { - color: #2b79a2; -} -.coal .theme-popup { - color: #98a3ad; - background: #141617; - border: 1px solid #43484d; -} -.coal .theme-popup .theme:hover { - background-color: #1f2124; -} -.coal .theme-popup .default { - color: #43484d; -} -.coal blockquote { - margin: 20px 0; - padding: 0 20px; - color: #98a3ad; - background-color: #242637; - border-top: 0.1em solid #2c2f44; - border-bottom: 0.1em solid #2c2f44; -} -.coal table td { - border-color: #1f2223; -} -.coal table tbody tr:nth-child(2n) { - background: #1b1d1e; -} -.coal table thead { - background: #3f4649; -} -.coal table thead td { - border: none; -} -.coal table thead tr { - border: 1px #3f4649 solid; -} -.coal :not(pre) > .hljs { - display: inline-block; - vertical-align: middle; - padding: 0.1em 0.3em; - -webkit-border-radius: 3px; - border-radius: 3px; -} -.coal pre { - position: relative; -} -.coal pre > .buttons { - position: absolute; - right: 5px; - top: 5px; - color: #a1adb8; - cursor: pointer; -} -.coal pre > .buttons :hover { - color: #3473ad; -} -.coal pre > .buttons i { - margin-left: 8px; -} -.coal pre > .result { - margin-top: 10px; -} -.navy { - color: #bcbdd0; - background-color: #161923; -/* Inline code */ -} -.navy .content .header:link, -.navy .content .header:visited { - color: #bcbdd0; - pointer: cursor; -} -.navy .content .header:link:hover, -.navy .content .header:visited:hover { - text-decoration: none; -} -.navy .sidebar { - background-color: #282d3f; - color: #c8c9db; -} -.navy .chapter li { - color: #505274; -} -.navy .chapter li a { - color: #c8c9db; -} -.navy .chapter li .active, -.navy .chapter li a:hover { -/* Animate color change */ - color: #2b79a2; -} -.navy .chapter .spacer { - background-color: #2d334f; -} -.navy .menu-bar, -.navy .menu-bar:visited, -.navy .nav-chapters, -.navy .nav-chapters:visited, -.navy .mobile-nav-chapters, -.navy .mobile-nav-chapters:visited { - color: #737480; -} -.navy .menu-bar i:hover, -.navy .nav-chapters:hover, -.navy .mobile-nav-chapters i:hover { - color: #b7b9cc; -} -.navy .mobile-nav-chapters i:hover { - color: #c8c9db; -} -.navy .mobile-nav-chapters { - background-color: #282d3f; -} -.navy .content a:link, -.navy a:visited { - color: #2b79a2; -} -.navy .theme-popup { - color: #bcbdd0; - background: #161923; - border: 1px solid #737480; -} -.navy .theme-popup .theme:hover { - background-color: #282e40; -} -.navy .theme-popup .default { - color: #737480; -} -.navy blockquote { - margin: 20px 0; - padding: 0 20px; - color: #bcbdd0; - background-color: #262933; - border-top: 0.1em solid #2f333f; - border-bottom: 0.1em solid #2f333f; -} -.navy table td { - border-color: #1f2331; -} -.navy table tbody tr:nth-child(2n) { - background: #1b1f2b; -} -.navy table thead { - background: #39415b; -} -.navy table thead td { - border: none; -} -.navy table thead tr { - border: 1px #39415b solid; -} -.navy :not(pre) > .hljs { - display: inline-block; - vertical-align: middle; - padding: 0.1em 0.3em; - -webkit-border-radius: 3px; - border-radius: 3px; -} -.navy pre { - position: relative; -} -.navy pre > .buttons { - position: absolute; - right: 5px; - top: 5px; - color: #c8c9db; - cursor: pointer; -} -.navy pre > .buttons :hover { - color: #2b79a2; -} -.navy pre > .buttons i { - margin-left: 8px; -} -.navy pre > .result { - margin-top: 10px; -} -.rust { - color: #262625; - background-color: #e1e1db; -/* Inline code */ -} -.rust .content .header:link, -.rust .content .header:visited { - color: #262625; - pointer: cursor; -} -.rust .content .header:link:hover, -.rust .content .header:visited:hover { - text-decoration: none; -} -.rust .sidebar { - background-color: #3b2e2a; - color: #c8c9db; -} -.rust .chapter li { - color: #505254; -} -.rust .chapter li a { - color: #c8c9db; -} -.rust .chapter li .active, -.rust .chapter li a:hover { -/* Animate color change */ - color: #e69f67; -} -.rust .chapter .spacer { - background-color: #45373a; -} -.rust .menu-bar, -.rust .menu-bar:visited, -.rust .nav-chapters, -.rust .nav-chapters:visited, -.rust .mobile-nav-chapters, -.rust .mobile-nav-chapters:visited { - color: #737480; -} -.rust .menu-bar i:hover, -.rust .nav-chapters:hover, -.rust .mobile-nav-chapters i:hover { - color: #262625; -} -.rust .mobile-nav-chapters i:hover { - color: #c8c9db; -} -.rust .mobile-nav-chapters { - background-color: #3b2e2a; -} -.rust .content a:link, -.rust a:visited { - color: #2b79a2; -} -.rust .theme-popup { - color: #262625; - background: #e1e1db; - border: 1px solid #b38f6b; -} -.rust .theme-popup .theme:hover { - background-color: #99908a; -} -.rust .theme-popup .default { - color: #737480; -} -.rust blockquote { - margin: 20px 0; - padding: 0 20px; - color: #262625; - background-color: #c1c1bb; - border-top: 0.1em solid #b8b8b1; - border-bottom: 0.1em solid #b8b8b1; -} -.rust table td { - border-color: #d7d7cf; -} -.rust table tbody tr:nth-child(2n) { - background: #dbdbd4; -} -.rust table thead { - background: #b3a497; -} -.rust table thead td { - border: none; -} -.rust table thead tr { - border: 1px #b3a497 solid; -} -.rust :not(pre) > .hljs { - display: inline-block; - vertical-align: middle; - padding: 0.1em 0.3em; - -webkit-border-radius: 3px; - border-radius: 3px; -} -.rust pre { - position: relative; -} -.rust pre > .buttons { - position: absolute; - right: 5px; - top: 5px; - color: #c8c9db; - cursor: pointer; -} -.rust pre > .buttons :hover { - color: #e69f67; -} -.rust pre > .buttons i { - margin-left: 8px; -} -.rust pre > .result { - margin-top: 10px; -} - -@media print { - #sidebar { - display: none; - } - #page-wrapper { - left: 0; - overflow-y: initial; - } - #content { - max-width: none; - margin: 0; - padding: 0; - } - #menu-bar { - display: none; - } - .page { - overflow-y: initial; - } - .nav-chapters { - display: none; - } - .mobile-nav-chapters { - display: none; - } -} - -div.footnote-definition p { - display: inline; -} diff --git a/src/doc/reference/src/tokens.md b/src/doc/reference/src/tokens.md deleted file mode 100644 index ca6cde8bd2855..0000000000000 --- a/src/doc/reference/src/tokens.md +++ /dev/null @@ -1,326 +0,0 @@ -# Tokens - -Tokens are primitive productions in the grammar defined by regular -(non-recursive) languages. "Simple" tokens are given in [string table -production] form, and occur in the rest of the -grammar as double-quoted strings. Other tokens have exact rules given. - -[string table production]: string-table-productions.html - -## Literals - -A literal is an expression consisting of a single token, rather than a sequence -of tokens, that immediately and directly denotes the value it evaluates to, -rather than referring to it by name or some other evaluation rule. A literal is -a form of constant expression, so is evaluated (primarily) at compile time. - -### Examples - -#### Characters and strings - -| | Example | `#` sets | Characters | Escapes | -|----------------------------------------------|-----------------|------------|-------------|---------------------| -| [Character](#character-literals) | `'H'` | `N/A` | All Unicode | [Quote](#quote-escapes) & [Byte](#byte-escapes) & [Unicode](#unicode-escapes) | -| [String](#string-literals) | `"hello"` | `N/A` | All Unicode | [Quote](#quote-escapes) & [Byte](#byte-escapes) & [Unicode](#unicode-escapes) | -| [Raw](#raw-string-literals) | `r#"hello"#` | `0...` | All Unicode | `N/A` | -| [Byte](#byte-literals) | `b'H'` | `N/A` | All ASCII | [Quote](#quote-escapes) & [Byte](#byte-escapes) | -| [Byte string](#byte-string-literals) | `b"hello"` | `N/A` | All ASCII | [Quote](#quote-escapes) & [Byte](#byte-escapes) | -| [Raw byte string](#raw-byte-string-literals) | `br#"hello"#` | `0...` | All ASCII | `N/A` | - -#### Byte escapes - -| | Name | -|---|------| -| `\x7F` | 8-bit character code (exactly 2 digits) | -| `\n` | Newline | -| `\r` | Carriage return | -| `\t` | Tab | -| `\\` | Backslash | -| `\0` | Null | - -#### Unicode escapes - -| | Name | -|---|------| -| `\u{7FFF}` | 24-bit Unicode character code (up to 6 digits) | - -#### Quote escapes - -| | Name | -|---|------| -| `\'` | Single quote | -| `\"` | Double quote | - -#### Numbers - -| [Number literals](#number-literals)`*` | Example | Exponentiation | Suffixes | -|----------------------------------------|---------|----------------|----------| -| Decimal integer | `98_222` | `N/A` | Integer suffixes | -| Hex integer | `0xff` | `N/A` | Integer suffixes | -| Octal integer | `0o77` | `N/A` | Integer suffixes | -| Binary integer | `0b1111_0000` | `N/A` | Integer suffixes | -| Floating-point | `123.0E+77` | `Optional` | Floating-point suffixes | - -`*` All number literals allow `_` as a visual separator: `1_234.0E+18f64` - -#### Suffixes - -| Integer | Floating-point | -|---------|----------------| -| `u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `u64`, `i64`, `isize`, `usize` | `f32`, `f64` | - -### Character and string literals - -#### Character literals - -A _character literal_ is a single Unicode character enclosed within two -`U+0027` (single-quote) characters, with the exception of `U+0027` itself, -which must be _escaped_ by a preceding `U+005C` character (`\`). - -#### String literals - -A _string literal_ is a sequence of any Unicode characters enclosed within two -`U+0022` (double-quote) characters, with the exception of `U+0022` itself, -which must be _escaped_ by a preceding `U+005C` character (`\`). - -Line-break characters are allowed in string literals. Normally they represent -themselves (i.e. no translation), but as a special exception, when an unescaped -`U+005C` character (`\`) occurs immediately before the newline (`U+000A`), the -`U+005C` character, the newline, and all whitespace at the beginning of the -next line are ignored. Thus `a` and `b` are equal: - -```rust -let a = "foobar"; -let b = "foo\ - bar"; - -assert_eq!(a,b); -``` - -#### Character escapes - -Some additional _escapes_ are available in either character or non-raw string -literals. An escape starts with a `U+005C` (`\`) and continues with one of the -following forms: - -* An _8-bit code point escape_ starts with `U+0078` (`x`) and is - followed by exactly two _hex digits_. It denotes the Unicode code point - equal to the provided hex value. -* A _24-bit code point escape_ starts with `U+0075` (`u`) and is followed - by up to six _hex digits_ surrounded by braces `U+007B` (`{`) and `U+007D` - (`}`). It denotes the Unicode code point equal to the provided hex value. -* A _whitespace escape_ is one of the characters `U+006E` (`n`), `U+0072` - (`r`), or `U+0074` (`t`), denoting the Unicode values `U+000A` (LF), - `U+000D` (CR) or `U+0009` (HT) respectively. -* The _null escape_ is the character `U+0030` (`0`) and denotes the Unicode - value `U+0000` (NUL). -* The _backslash escape_ is the character `U+005C` (`\`) which must be - escaped in order to denote *itself*. - -#### Raw string literals - -Raw string literals do not process any escapes. They start with the character -`U+0072` (`r`), followed by zero or more of the character `U+0023` (`#`) and a -`U+0022` (double-quote) character. The _raw string body_ can contain any sequence -of Unicode characters and is terminated only by another `U+0022` (double-quote) -character, followed by the same number of `U+0023` (`#`) characters that preceded -the opening `U+0022` (double-quote) character. - -All Unicode characters contained in the raw string body represent themselves, -the characters `U+0022` (double-quote) (except when followed by at least as -many `U+0023` (`#`) characters as were used to start the raw string literal) or -`U+005C` (`\`) do not have any special meaning. - -Examples for string literals: - -``` -"foo"; r"foo"; // foo -"\"foo\""; r#""foo""#; // "foo" - -"foo #\"# bar"; -r##"foo #"# bar"##; // foo #"# bar - -"\x52"; "R"; r"R"; // R -"\\x52"; r"\x52"; // \x52 -``` - -### Byte and byte string literals - -#### Byte literals - -A _byte literal_ is a single ASCII character (in the `U+0000` to `U+007F` -range) or a single _escape_ preceded by the characters `U+0062` (`b`) and -`U+0027` (single-quote), and followed by the character `U+0027`. If the character -`U+0027` is present within the literal, it must be _escaped_ by a preceding -`U+005C` (`\`) character. It is equivalent to a `u8` unsigned 8-bit integer -_number literal_. - -#### Byte string literals - -A non-raw _byte string literal_ is a sequence of ASCII characters and _escapes_, -preceded by the characters `U+0062` (`b`) and `U+0022` (double-quote), and -followed by the character `U+0022`. If the character `U+0022` is present within -the literal, it must be _escaped_ by a preceding `U+005C` (`\`) character. -Alternatively, a byte string literal can be a _raw byte string literal_, defined -below. A byte string literal of length `n` is equivalent to a `&'static [u8; n]` borrowed fixed-sized array -of unsigned 8-bit integers. - -Some additional _escapes_ are available in either byte or non-raw byte string -literals. An escape starts with a `U+005C` (`\`) and continues with one of the -following forms: - -* A _byte escape_ escape starts with `U+0078` (`x`) and is - followed by exactly two _hex digits_. It denotes the byte - equal to the provided hex value. -* A _whitespace escape_ is one of the characters `U+006E` (`n`), `U+0072` - (`r`), or `U+0074` (`t`), denoting the bytes values `0x0A` (ASCII LF), - `0x0D` (ASCII CR) or `0x09` (ASCII HT) respectively. -* The _null escape_ is the character `U+0030` (`0`) and denotes the byte - value `0x00` (ASCII NUL). -* The _backslash escape_ is the character `U+005C` (`\`) which must be - escaped in order to denote its ASCII encoding `0x5C`. - -#### Raw byte string literals - -Raw byte string literals do not process any escapes. They start with the -character `U+0062` (`b`), followed by `U+0072` (`r`), followed by zero or more -of the character `U+0023` (`#`), and a `U+0022` (double-quote) character. The -_raw string body_ can contain any sequence of ASCII characters and is terminated -only by another `U+0022` (double-quote) character, followed by the same number of -`U+0023` (`#`) characters that preceded the opening `U+0022` (double-quote) -character. A raw byte string literal can not contain any non-ASCII byte. - -All characters contained in the raw string body represent their ASCII encoding, -the characters `U+0022` (double-quote) (except when followed by at least as -many `U+0023` (`#`) characters as were used to start the raw string literal) or -`U+005C` (`\`) do not have any special meaning. - -Examples for byte string literals: - -``` -b"foo"; br"foo"; // foo -b"\"foo\""; br#""foo""#; // "foo" - -b"foo #\"# bar"; -br##"foo #"# bar"##; // foo #"# bar - -b"\x52"; b"R"; br"R"; // R -b"\\x52"; br"\x52"; // \x52 -``` - -### Number literals - -A _number literal_ is either an _integer literal_ or a _floating-point -literal_. The grammar for recognizing the two kinds of literals is mixed. - -#### Integer literals - -An _integer literal_ has one of four forms: - -* A _decimal literal_ starts with a *decimal digit* and continues with any - mixture of *decimal digits* and _underscores_. -* A _hex literal_ starts with the character sequence `U+0030` `U+0078` - (`0x`) and continues as any mixture of hex digits and underscores. -* An _octal literal_ starts with the character sequence `U+0030` `U+006F` - (`0o`) and continues as any mixture of octal digits and underscores. -* A _binary literal_ starts with the character sequence `U+0030` `U+0062` - (`0b`) and continues as any mixture of binary digits and underscores. - -Like any literal, an integer literal may be followed (immediately, -without any spaces) by an _integer suffix_, which forcibly sets the -type of the literal. The integer suffix must be the name of one of the -integral types: `u8`, `i8`, `u16`, `i16`, `u32`, `i32`, `u64`, `i64`, -`isize`, or `usize`. - -The type of an _unsuffixed_ integer literal is determined by type inference: - -* If an integer type can be _uniquely_ determined from the surrounding - program context, the unsuffixed integer literal has that type. - -* If the program context under-constrains the type, it defaults to the - signed 32-bit integer `i32`. - -* If the program context over-constrains the type, it is considered a - static type error. - -Examples of integer literals of various forms: - -``` -123i32; // type i32 -123u32; // type u32 -123_u32; // type u32 -0xff_u8; // type u8 -0o70_i16; // type i16 -0b1111_1111_1001_0000_i32; // type i32 -0usize; // type usize -``` - -Note that the Rust syntax considers `-1i8` as an application of the [unary minus -operator] to an integer literal `1i8`, rather than -a single integer literal. - -[unary minus operator]: expressions.html#unary-operator-expressions - -#### Floating-point literals - -A _floating-point literal_ has one of two forms: - -* A _decimal literal_ followed by a period character `U+002E` (`.`). This is - optionally followed by another decimal literal, with an optional _exponent_. -* A single _decimal literal_ followed by an _exponent_. - -Like integer literals, a floating-point literal may be followed by a -suffix, so long as the pre-suffix part does not end with `U+002E` (`.`). -The suffix forcibly sets the type of the literal. There are two valid -_floating-point suffixes_, `f32` and `f64` (the 32-bit and 64-bit floating point -types), which explicitly determine the type of the literal. - -The type of an _unsuffixed_ floating-point literal is determined by -type inference: - -* If a floating-point type can be _uniquely_ determined from the - surrounding program context, the unsuffixed floating-point literal - has that type. - -* If the program context under-constrains the type, it defaults to `f64`. - -* If the program context over-constrains the type, it is considered a - static type error. - -Examples of floating-point literals of various forms: - -``` -123.0f64; // type f64 -0.1f64; // type f64 -0.1f32; // type f32 -12E+99_f64; // type f64 -let x: f64 = 2.; // type f64 -``` - -This last example is different because it is not possible to use the suffix -syntax with a floating point literal ending in a period. `2.f64` would attempt -to call a method named `f64` on `2`. - -The representation semantics of floating-point numbers are described in -["Machine Types"]. - -["Machine Types"]: types.html#machine-types - -### Boolean literals - -The two values of the boolean type are written `true` and `false`. - -## Symbols - -Symbols are a general class of printable [tokens] that play structural -roles in a variety of grammar productions. They are a -set of remaining miscellaneous printable tokens that do not -otherwise appear as [unary operators], [binary -operators], or [keywords]. -They are catalogued in [the Symbols section][symbols] of the Grammar document. - -[unary operators]: expressions.html#unary-operator-expressions -[binary operators]: expressions.html#binary-operator-expressions -[tokens]: #tokens -[symbols]: ../grammar.html#symbols -[keywords]: ../grammar.html#keywords \ No newline at end of file diff --git a/src/doc/reference/src/type-coercions.md b/src/doc/reference/src/type-coercions.md deleted file mode 100644 index 6301e5e83d748..0000000000000 --- a/src/doc/reference/src/type-coercions.md +++ /dev/null @@ -1,145 +0,0 @@ -# Type coercions - -Coercions are defined in [RFC 401]. A coercion is implicit and has no syntax. - -[RFC 401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md - -## Coercion sites - -A coercion can only occur at certain coercion sites in a program; these are -typically places where the desired type is explicit or can be derived by -propagation from explicit types (without type inference). Possible coercion -sites are: - -* `let` statements where an explicit type is given. - - For example, `42` is coerced to have type `i8` in the following: - - ```rust - let _: i8 = 42; - ``` - -* `static` and `const` statements (similar to `let` statements). - -* Arguments for function calls - - The value being coerced is the actual parameter, and it is coerced to - the type of the formal parameter. - - For example, `42` is coerced to have type `i8` in the following: - - ```rust - fn bar(_: i8) { } - - fn main() { - bar(42); - } - ``` - -* Instantiations of struct or variant fields - - For example, `42` is coerced to have type `i8` in the following: - - ```rust - struct Foo { x: i8 } - - fn main() { - Foo { x: 42 }; - } - ``` - -* Function results, either the final line of a block if it is not - semicolon-terminated or any expression in a `return` statement - - For example, `42` is coerced to have type `i8` in the following: - - ```rust - fn foo() -> i8 { - 42 - } - ``` - -If the expression in one of these coercion sites is a coercion-propagating -expression, then the relevant sub-expressions in that expression are also -coercion sites. Propagation recurses from these new coercion sites. -Propagating expressions and their relevant sub-expressions are: - -* Array literals, where the array has type `[U; n]`. Each sub-expression in -the array literal is a coercion site for coercion to type `U`. - -* Array literals with repeating syntax, where the array has type `[U; n]`. The -repeated sub-expression is a coercion site for coercion to type `U`. - -* Tuples, where a tuple is a coercion site to type `(U_0, U_1, ..., U_n)`. -Each sub-expression is a coercion site to the respective type, e.g. the -zeroth sub-expression is a coercion site to type `U_0`. - -* Parenthesized sub-expressions (`(e)`): if the expression has type `U`, then -the sub-expression is a coercion site to `U`. - -* Blocks: if a block has type `U`, then the last expression in the block (if -it is not semicolon-terminated) is a coercion site to `U`. This includes -blocks which are part of control flow statements, such as `if`/`else`, if -the block has a known type. - -## Coercion types - -Coercion is allowed between the following types: - -* `T` to `U` if `T` is a subtype of `U` (*reflexive case*) - -* `T_1` to `T_3` where `T_1` coerces to `T_2` and `T_2` coerces to `T_3` -(*transitive case*) - - Note that this is not fully supported yet - -* `&mut T` to `&T` - -* `*mut T` to `*const T` - -* `&T` to `*const T` - -* `&mut T` to `*mut T` - -* `&T` to `&U` if `T` implements `Deref`. For example: - - ```rust - use std::ops::Deref; - - struct CharContainer { - value: char, - } - - impl Deref for CharContainer { - type Target = char; - - fn deref<'a>(&'a self) -> &'a char { - &self.value - } - } - - fn foo(arg: &char) {} - - fn main() { - let x = &mut CharContainer { value: 'y' }; - foo(x); //&mut CharContainer is coerced to &char. - } - ``` - -* `&mut T` to `&mut U` if `T` implements `DerefMut`. - -* TyCtor(`T`) to TyCtor(coerce_inner(`T`)), where TyCtor(`T`) is one of - - `&T` - - `&mut T` - - `*const T` - - `*mut T` - - `Box` - - and where - - coerce_inner(`[T, ..n]`) = `[T]` - - coerce_inner(`T`) = `U` where `T` is a concrete type which implements the - trait `U`. - - In the future, coerce_inner will be recursively extended to tuples and - structs. In addition, coercions from sub-traits to super-traits will be - added. See [RFC 401] for more details. diff --git a/src/doc/reference/src/type-system.md b/src/doc/reference/src/type-system.md deleted file mode 100644 index bed7f128e5704..0000000000000 --- a/src/doc/reference/src/type-system.md +++ /dev/null @@ -1 +0,0 @@ -# Type system diff --git a/src/doc/reference/src/types.md b/src/doc/reference/src/types.md deleted file mode 100644 index 2ddcba177e35d..0000000000000 --- a/src/doc/reference/src/types.md +++ /dev/null @@ -1,398 +0,0 @@ -# Types - -Every variable, item and value in a Rust program has a type. The _type_ of a -*value* defines the interpretation of the memory holding it. - -Built-in types and type-constructors are tightly integrated into the language, -in nontrivial ways that are not possible to emulate in user-defined types. -User-defined types have limited capabilities. - -## Primitive types - -The primitive types are the following: - -* The boolean type `bool` with values `true` and `false`. -* The machine types (integer and floating-point). -* The machine-dependent integer types. -* Arrays -* Tuples -* Slices -* Function pointers - -### Machine types - -The machine types are the following: - -* The unsigned word types `u8`, `u16`, `u32` and `u64`, with values drawn from - the integer intervals [0, 2^8 - 1], [0, 2^16 - 1], [0, 2^32 - 1] and - [0, 2^64 - 1] respectively. - -* The signed two's complement word types `i8`, `i16`, `i32` and `i64`, with - values drawn from the integer intervals [-(2^(7)), 2^7 - 1], - [-(2^(15)), 2^15 - 1], [-(2^(31)), 2^31 - 1], [-(2^(63)), 2^63 - 1] - respectively. - -* The IEEE 754-2008 `binary32` and `binary64` floating-point types: `f32` and - `f64`, respectively. - -### Machine-dependent integer types - -The `usize` type is an unsigned integer type with the same number of bits as the -platform's pointer type. It can represent every memory address in the process. - -The `isize` type is a signed integer type with the same number of bits as the -platform's pointer type. The theoretical upper bound on object and array size -is the maximum `isize` value. This ensures that `isize` can be used to calculate -differences between pointers into an object or array and can address every byte -within an object along with one byte past the end. - -## Textual types - -The types `char` and `str` hold textual data. - -A value of type `char` is a [Unicode scalar value]( -http://www.unicode.org/glossary/#unicode_scalar_value) (i.e. a code point that -is not a surrogate), represented as a 32-bit unsigned word in the 0x0000 to -0xD7FF or 0xE000 to 0x10FFFF range. A `[char]` array is effectively an UCS-4 / -UTF-32 string. - -A value of type `str` is a Unicode string, represented as an array of 8-bit -unsigned bytes holding a sequence of UTF-8 code points. Since `str` is of -unknown size, it is not a _first-class_ type, but can only be instantiated -through a pointer type, such as `&str`. - -## Tuple types - -A tuple *type* is a heterogeneous product of other types, called the *elements* -of the tuple. It has no nominal name and is instead structurally typed. - -Tuple types and values are denoted by listing the types or values of their -elements, respectively, in a parenthesized, comma-separated list. - -Because tuple elements don't have a name, they can only be accessed by -pattern-matching or by using `N` directly as a field to access the -`N`th element. - -An example of a tuple type and its use: - -``` -type Pair<'a> = (i32, &'a str); -let p: Pair<'static> = (10, "ten"); -let (a, b) = p; - -assert_eq!(a, 10); -assert_eq!(b, "ten"); -assert_eq!(p.0, 10); -assert_eq!(p.1, "ten"); -``` - -For historical reasons and convenience, the tuple type with no elements (`()`) -is often called ‘unit’ or ‘the unit type’. - -## Array, and Slice types - -Rust has two different types for a list of items: - -* `[T; N]`, an 'array' -* `&[T]`, a 'slice' - -An array has a fixed size, and can be allocated on either the stack or the -heap. - -A slice is a 'view' into an array. It doesn't own the data it points -to, it borrows it. - -Examples: - -```{rust} -// A stack-allocated array -let array: [i32; 3] = [1, 2, 3]; - -// A heap-allocated array -let vector: Vec = vec![1, 2, 3]; - -// A slice into an array -let slice: &[i32] = &vector[..]; -``` - -As you can see, the `vec!` macro allows you to create a `Vec` easily. The -`vec!` macro is also part of the standard library, rather than the language. - -All in-bounds elements of arrays and slices are always initialized, and access -to an array or slice is always bounds-checked. - -## Struct types - -A `struct` *type* is a heterogeneous product of other types, called the -*fields* of the type.[^structtype] - -[^structtype]: `struct` types are analogous to `struct` types in C, - the *record* types of the ML family, - or the *struct* types of the Lisp family. - -New instances of a `struct` can be constructed with a [struct -expression](expressions.html#struct-expressions). - -The memory layout of a `struct` is undefined by default to allow for compiler -optimizations like field reordering, but it can be fixed with the -`#[repr(...)]` attribute. In either case, fields may be given in any order in -a corresponding struct *expression*; the resulting `struct` value will always -have the same memory layout. - -The fields of a `struct` may be qualified by [visibility -modifiers](visibility-and-privacy.html), to allow access to data in a -struct outside a module. - -A _tuple struct_ type is just like a struct type, except that the fields are -anonymous. - -A _unit-like struct_ type is like a struct type, except that it has no -fields. The one value constructed by the associated [struct -expression](expressions.html#struct-expressions) is the only value that inhabits such a -type. - -## Enumerated types - -An *enumerated type* is a nominal, heterogeneous disjoint union type, denoted -by the name of an [`enum` item](items.html#enumerations). [^enumtype] - -[^enumtype]: The `enum` type is analogous to a `data` constructor declaration in - ML, or a *pick ADT* in Limbo. - -An [`enum` item](items.html#enumerations) declares both the type and a number of *variant -constructors*, each of which is independently named and takes an optional tuple -of arguments. - -New instances of an `enum` can be constructed by calling one of the variant -constructors, in a [call expression](expressions.html#call-expressions). - -Any `enum` value consumes as much memory as the largest variant constructor for -its corresponding `enum` type. - -Enum types cannot be denoted *structurally* as types, but must be denoted by -named reference to an [`enum` item](items.html#enumerations). - -## Recursive types - -Nominal types — [enumerations](#enumerated-types) and -[structs](#struct-types) — may be recursive. That is, each `enum` -constructor or `struct` field may refer, directly or indirectly, to the -enclosing `enum` or `struct` type itself. Such recursion has restrictions: - -* Recursive types must include a nominal type in the recursion - (not mere [type definitions](../grammar.html#type-definitions), - or other structural types such as [arrays](#array-and-slice-types) or [tuples](#tuple-types)). -* A recursive `enum` item must have at least one non-recursive constructor - (in order to give the recursion a basis case). -* The size of a recursive type must be finite; - in other words the recursive fields of the type must be [pointer types](#pointer-types). -* Recursive type definitions can cross module boundaries, but not module *visibility* boundaries, - or crate boundaries (in order to simplify the module system and type checker). - -An example of a *recursive* type and its use: - -``` -enum List { - Nil, - Cons(T, Box>) -} - -let a: List = List::Cons(7, Box::new(List::Cons(13, Box::new(List::Nil)))); -``` - -## Pointer types - -All pointers in Rust are explicit first-class values. They can be copied, -stored into data structs, and returned from functions. There are two -varieties of pointer in Rust: - -* References (`&`) - : These point to memory _owned by some other value_. - A reference type is written `&type`, - or `&'a type` when you need to specify an explicit lifetime. - Copying a reference is a "shallow" operation: - it involves only copying the pointer itself. - Releasing a reference has no effect on the value it points to, - but a reference of a temporary value will keep it alive during the scope - of the reference itself. - -* Raw pointers (`*`) - : Raw pointers are pointers without safety or liveness guarantees. - Raw pointers are written as `*const T` or `*mut T`, - for example `*const i32` means a raw pointer to a 32-bit integer. - Copying or dropping a raw pointer has no effect on the lifecycle of any - other value. Dereferencing a raw pointer or converting it to any other - pointer type is an [`unsafe` operation](unsafe-functions.html). - Raw pointers are generally discouraged in Rust code; - they exist to support interoperability with foreign code, - and writing performance-critical or low-level functions. - -The standard library contains additional 'smart pointer' types beyond references -and raw pointers. - -## Function types - -The function type constructor `fn` forms new function types. A function type -consists of a possibly-empty set of function-type modifiers (such as `unsafe` -or `extern`), a sequence of input types and an output type. - -An example of a `fn` type: - -``` -fn add(x: i32, y: i32) -> i32 { - x + y -} - -let mut x = add(5,7); - -type Binop = fn(i32, i32) -> i32; -let bo: Binop = add; -x = bo(5,7); -``` - -### Function types for specific items - -Internal to the compiler, there are also function types that are specific to a particular -function item. In the following snippet, for example, the internal types of the functions -`foo` and `bar` are different, despite the fact that they have the same signature: - -``` -fn foo() { } -fn bar() { } -``` - -The types of `foo` and `bar` can both be implicitly coerced to the fn -pointer type `fn()`. There is currently no syntax for unique fn types, -though the compiler will emit a type like `fn() {foo}` in error -messages to indicate "the unique fn type for the function `foo`". - -## Closure types - -A [lambda expression](expressions.html#lambda-expressions) produces a closure -value with a unique, anonymous type that cannot be written out. - -Depending on the requirements of the closure, its type implements one or -more of the closure traits: - -* `FnOnce` - : The closure can be called once. A closure called as `FnOnce` - can move out values from its environment. - -* `FnMut` - : The closure can be called multiple times as mutable. A closure called as - `FnMut` can mutate values from its environment. `FnMut` inherits from - `FnOnce` (i.e. anything implementing `FnMut` also implements `FnOnce`). - -* `Fn` - : The closure can be called multiple times through a shared reference. - A closure called as `Fn` can neither move out from nor mutate values - from its environment. `Fn` inherits from `FnMut`, which itself - inherits from `FnOnce`. - - -## Trait objects - -In Rust, a type like `&SomeTrait` or `Box` is called a _trait object_. -Each instance of a trait object includes: - - - a pointer to an instance of a type `T` that implements `SomeTrait` - - a _virtual method table_, often just called a _vtable_, which contains, for - each method of `SomeTrait` that `T` implements, a pointer to `T`'s - implementation (i.e. a function pointer). - -The purpose of trait objects is to permit "late binding" of methods. Calling a -method on a trait object results in virtual dispatch at runtime: that is, a -function pointer is loaded from the trait object vtable and invoked indirectly. -The actual implementation for each vtable entry can vary on an object-by-object -basis. - -Note that for a trait object to be instantiated, the trait must be -_object-safe_. Object safety rules are defined in [RFC 255]. - -[RFC 255]: https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md - -Given a pointer-typed expression `E` of type `&T` or `Box`, where `T` -implements trait `R`, casting `E` to the corresponding pointer type `&R` or -`Box` results in a value of the _trait object_ `R`. This result is -represented as a pair of pointers: the vtable pointer for the `T` -implementation of `R`, and the pointer value of `E`. - -An example of a trait object: - -``` -trait Printable { - fn stringify(&self) -> String; -} - -impl Printable for i32 { - fn stringify(&self) -> String { self.to_string() } -} - -fn print(a: Box) { - println!("{}", a.stringify()); -} - -fn main() { - print(Box::new(10) as Box); -} -``` - -In this example, the trait `Printable` occurs as a trait object in both the -type signature of `print`, and the cast expression in `main`. - -### Type parameters - -Within the body of an item that has type parameter declarations, the names of -its type parameters are types: - -```ignore -fn to_vec(xs: &[A]) -> Vec { - if xs.is_empty() { - return vec![]; - } - let first: A = xs[0].clone(); - let mut rest: Vec = to_vec(&xs[1..]); - rest.insert(0, first); - rest -} -``` - -Here, `first` has type `A`, referring to `to_vec`'s `A` type parameter; and `rest` -has type `Vec`, a vector with element type `A`. - -## Self types - -The special type `Self` has a meaning within traits and impls. In a trait definition, it refers -to an implicit type parameter representing the "implementing" type. In an impl, -it is an alias for the implementing type. For example, in: - -``` -pub trait From { - fn from(T) -> Self; -} - -impl From for String { - fn from(x: i32) -> Self { - x.to_string() - } -} -``` - -The notation `Self` in the impl refers to the implementing type: `String`. In another -example: - -``` -trait Printable { - fn make_string(&self) -> String; -} - -impl Printable for String { - fn make_string(&self) -> String { - (*self).clone() - } -} -``` - -The notation `&self` is a shorthand for `self: &Self`. In this case, -in the impl, `Self` refers to the value of type `String` that is the -receiver for a call to the method `make_string`. diff --git a/src/doc/reference/src/unicode-productions.md b/src/doc/reference/src/unicode-productions.md deleted file mode 100644 index f9d6d1d59732d..0000000000000 --- a/src/doc/reference/src/unicode-productions.md +++ /dev/null @@ -1,9 +0,0 @@ -# Unicode productions - -A few productions in Rust's grammar permit Unicode code points outside the -ASCII range. We define these productions in terms of character properties -specified in the Unicode standard, rather than in terms of ASCII-range code -points. The grammar has a [Special Unicode Productions][unicodeproductions] -section that lists these productions. - -[unicodeproductions]: ../grammar.html#special-unicode-productions diff --git a/src/doc/reference/src/unsafe-blocks.md b/src/doc/reference/src/unsafe-blocks.md deleted file mode 100644 index 754278445d51e..0000000000000 --- a/src/doc/reference/src/unsafe-blocks.md +++ /dev/null @@ -1,22 +0,0 @@ -# Unsafe blocks - -A block of code can be prefixed with the `unsafe` keyword, to permit calling -`unsafe` functions or dereferencing raw pointers within a safe function. - -When a programmer has sufficient conviction that a sequence of potentially -unsafe operations is actually safe, they can encapsulate that sequence (taken -as a whole) within an `unsafe` block. The compiler will consider uses of such -code safe, in the surrounding context. - -Unsafe blocks are used to wrap foreign libraries, make direct use of hardware -or implement features not directly present in the language. For example, Rust -provides the language features necessary to implement memory-safe concurrency -in the language but the implementation of threads and message passing is in the -standard library. - -Rust's type system is a conservative approximation of the dynamic safety -requirements, so in some cases there is a performance cost to using safe code. -For example, a doubly-linked list is not a tree structure and can only be -represented with reference-counted pointers in safe code. By using `unsafe` -blocks to represent the reverse links as raw pointers, it can be implemented -with only boxes. diff --git a/src/doc/reference/src/unsafe-functions.md b/src/doc/reference/src/unsafe-functions.md deleted file mode 100644 index 7a5064c08f41a..0000000000000 --- a/src/doc/reference/src/unsafe-functions.md +++ /dev/null @@ -1,5 +0,0 @@ -# Unsafe functions - -Unsafe functions are functions that are not safe in all contexts and/or for all -possible inputs. Such a function must be prefixed with the keyword `unsafe` and -can only be called from an `unsafe` block or another `unsafe` function. diff --git a/src/doc/reference/src/unsafety.md b/src/doc/reference/src/unsafety.md deleted file mode 100644 index abb7a9eec5848..0000000000000 --- a/src/doc/reference/src/unsafety.md +++ /dev/null @@ -1,11 +0,0 @@ -# Unsafety - -Unsafe operations are those that potentially violate the memory-safety -guarantees of Rust's static semantics. - -The following language level features cannot be used in the safe subset of -Rust: - -- Dereferencing a [raw pointer](types.html#pointer-types). -- Reading or writing a [mutable static variable](items.html#mutable-statics). -- Calling an unsafe function (including an intrinsic or foreign function). diff --git a/src/doc/reference/src/variables.md b/src/doc/reference/src/variables.md deleted file mode 100644 index ce3d226d0238b..0000000000000 --- a/src/doc/reference/src/variables.md +++ /dev/null @@ -1,31 +0,0 @@ -# Variables - -A _variable_ is a component of a stack frame, either a named function parameter, -an anonymous [temporary](expressions.html#lvalues-rvalues-and-temporaries), or a named local -variable. - -A _local variable_ (or *stack-local* allocation) holds a value directly, -allocated within the stack's memory. The value is a part of the stack frame. - -Local variables are immutable unless declared otherwise like: `let mut x = ...`. - -Function parameters are immutable unless declared with `mut`. The `mut` keyword -applies only to the following parameter (so `|mut x, y|` and `fn f(mut x: -Box, y: Box)` declare one mutable variable `x` and one immutable -variable `y`). - -Methods that take either `self` or `Box` can optionally place them in a -mutable variable by prefixing them with `mut` (similar to regular arguments): - -``` -trait Changer: Sized { - fn change(mut self) {} - fn modify(mut self: Box) {} -} -``` - -Local variables are not initialized when allocated; the entire frame worth of -local variables are allocated at once, on frame-entry, in an uninitialized -state. Subsequent statements within a function may or may not initialize the -local variables. Local variables can be used only after they have been -initialized; this is enforced by the compiler. diff --git a/src/doc/reference/src/visibility-and-privacy.md b/src/doc/reference/src/visibility-and-privacy.md deleted file mode 100644 index 50d3e7507d0ed..0000000000000 --- a/src/doc/reference/src/visibility-and-privacy.md +++ /dev/null @@ -1,160 +0,0 @@ -# Visibility and Privacy - -These two terms are often used interchangeably, and what they are attempting to -convey is the answer to the question "Can this item be used at this location?" - -Rust's name resolution operates on a global hierarchy of namespaces. Each level -in the hierarchy can be thought of as some item. The items are one of those -mentioned above, but also include external crates. Declaring or defining a new -module can be thought of as inserting a new tree into the hierarchy at the -location of the definition. - -To control whether interfaces can be used across modules, Rust checks each use -of an item to see whether it should be allowed or not. This is where privacy -warnings are generated, or otherwise "you used a private item of another module -and weren't allowed to." - -By default, everything in Rust is *private*, with two exceptions: Associated -items in a `pub` Trait are public by default; Enum variants -in a `pub` enum are also public by default. When an item is declared as `pub`, -it can be thought of as being accessible to the outside world. For example: - -``` -# fn main() {} -// Declare a private struct -struct Foo; - -// Declare a public struct with a private field -pub struct Bar { - field: i32, -} - -// Declare a public enum with two public variants -pub enum State { - PubliclyAccessibleState, - PubliclyAccessibleState2, -} -``` - -With the notion of an item being either public or private, Rust allows item -accesses in two cases: - -1. If an item is public, then it can be used externally through any of its - public ancestors. -2. If an item is private, it may be accessed by the current module and its - descendants. - -These two cases are surprisingly powerful for creating module hierarchies -exposing public APIs while hiding internal implementation details. To help -explain, here's a few use cases and what they would entail: - -* A library developer needs to expose functionality to crates which link - against their library. As a consequence of the first case, this means that - anything which is usable externally must be `pub` from the root down to the - destination item. Any private item in the chain will disallow external - accesses. - -* A crate needs a global available "helper module" to itself, but it doesn't - want to expose the helper module as a public API. To accomplish this, the - root of the crate's hierarchy would have a private module which then - internally has a "public API". Because the entire crate is a descendant of - the root, then the entire local crate can access this private module through - the second case. - -* When writing unit tests for a module, it's often a common idiom to have an - immediate child of the module to-be-tested named `mod test`. This module - could access any items of the parent module through the second case, meaning - that internal implementation details could also be seamlessly tested from the - child module. - -In the second case, it mentions that a private item "can be accessed" by the -current module and its descendants, but the exact meaning of accessing an item -depends on what the item is. Accessing a module, for example, would mean -looking inside of it (to import more items). On the other hand, accessing a -function would mean that it is invoked. Additionally, path expressions and -import statements are considered to access an item in the sense that the -import/expression is only valid if the destination is in the current visibility -scope. - -Here's an example of a program which exemplifies the three cases outlined -above: - -``` -// This module is private, meaning that no external crate can access this -// module. Because it is private at the root of this current crate, however, any -// module in the crate may access any publicly visible item in this module. -mod crate_helper_module { - - // This function can be used by anything in the current crate - pub fn crate_helper() {} - - // This function *cannot* be used by anything else in the crate. It is not - // publicly visible outside of the `crate_helper_module`, so only this - // current module and its descendants may access it. - fn implementation_detail() {} -} - -// This function is "public to the root" meaning that it's available to external -// crates linking against this one. -pub fn public_api() {} - -// Similarly to 'public_api', this module is public so external crates may look -// inside of it. -pub mod submodule { - use crate_helper_module; - - pub fn my_method() { - // Any item in the local crate may invoke the helper module's public - // interface through a combination of the two rules above. - crate_helper_module::crate_helper(); - } - - // This function is hidden to any module which is not a descendant of - // `submodule` - fn my_implementation() {} - - #[cfg(test)] - mod test { - - #[test] - fn test_my_implementation() { - // Because this module is a descendant of `submodule`, it's allowed - // to access private items inside of `submodule` without a privacy - // violation. - super::my_implementation(); - } - } -} - -# fn main() {} -``` - -For a Rust program to pass the privacy checking pass, all paths must be valid -accesses given the two rules above. This includes all use statements, -expressions, types, etc. - -## Re-exporting and Visibility - -Rust allows publicly re-exporting items through a `pub use` directive. Because -this is a public directive, this allows the item to be used in the current -module through the rules above. It essentially allows public access into the -re-exported item. For example, this program is valid: - -``` -pub use self::implementation::api; - -mod implementation { - pub mod api { - pub fn f() {} - } -} - -# fn main() {} -``` - -This means that any external crate referencing `implementation::api::f` would -receive a privacy violation, while the path `api::f` would be allowed. - -When re-exporting a private item, it can be thought of as allowing the "privacy -chain" being short-circuited through the reexport instead of passing through -the namespace hierarchy as it normally would. diff --git a/src/doc/reference/src/whitespace.md b/src/doc/reference/src/whitespace.md deleted file mode 100644 index 2fd162bcb2da8..0000000000000 --- a/src/doc/reference/src/whitespace.md +++ /dev/null @@ -1,22 +0,0 @@ -# Whitespace - -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+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. - -A Rust program has identical meaning if each whitespace element is replaced -with any other legal whitespace element, such as a single space character. From 846f59f831a6ec2a9ff7c6cedb6653ae329704f8 Mon Sep 17 00:00:00 2001 From: steveklabnik Date: Thu, 2 Mar 2017 18:47:06 -0500 Subject: [PATCH 17/29] import reference submodule --- .gitmodules | 3 +++ src/doc/reference | 1 + 2 files changed, 4 insertions(+) create mode 160000 src/doc/reference diff --git a/.gitmodules b/.gitmodules index 2beff77267efa..d2c96ac901fc4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -21,3 +21,6 @@ [submodule "src/tools/cargo"] path = src/tools/cargo url = https://github.com/rust-lang/cargo +[submodule "reference"] + path = src/doc/reference + url = https://github.com/rust-lang-nursery/reference.git diff --git a/src/doc/reference b/src/doc/reference new file mode 160000 index 0000000000000..2d23ea601f017 --- /dev/null +++ b/src/doc/reference @@ -0,0 +1 @@ +Subproject commit 2d23ea601f017c106a2303094ee1c57ba856d246 From f2187093f8d19c39071502c9e95bacabd5febd88 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 23 Feb 2017 18:49:54 +0300 Subject: [PATCH 18/29] Add/remove `rerun-if-changed` when necessary --- src/Cargo.lock | 1 + src/liballoc_jemalloc/build.rs | 2 -- src/libcompiler_builtins/Cargo.toml | 1 + src/libcompiler_builtins/build.rs | 4 ++++ src/libflate/build.rs | 1 + src/librustc_asan/build.rs | 2 -- src/librustc_llvm/build.rs | 4 +--- src/librustc_lsan/build.rs | 2 -- src/librustc_msan/build.rs | 2 -- src/librustc_tsan/build.rs | 2 -- src/librustdoc/build.rs | 5 ++++- src/libstd/build.rs | 2 -- src/libunwind/build.rs | 1 + 13 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index c463f2bb747cc..e7fa572fa3ad3 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -221,6 +221,7 @@ dependencies = [ name = "compiler_builtins" version = "0.0.0" dependencies = [ + "build_helper 0.1.0", "core 0.0.0", "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/src/liballoc_jemalloc/build.rs b/src/liballoc_jemalloc/build.rs index a3402bf399427..cd2e8e4ec20fa 100644 --- a/src/liballoc_jemalloc/build.rs +++ b/src/liballoc_jemalloc/build.rs @@ -21,8 +21,6 @@ use std::process::Command; use build_helper::{run, rerun_if_changed_anything_in_dir, up_to_date}; fn main() { - println!("cargo:rerun-if-changed=build.rs"); - // FIXME: This is a hack to support building targets that don't // support jemalloc alongside hosts that do. The jemalloc build is // controlled by a feature of the std crate, and if that feature diff --git a/src/libcompiler_builtins/Cargo.toml b/src/libcompiler_builtins/Cargo.toml index 1a549ae823ac5..3f844b3f09e3a 100644 --- a/src/libcompiler_builtins/Cargo.toml +++ b/src/libcompiler_builtins/Cargo.toml @@ -15,4 +15,5 @@ doc = false core = { path = "../libcore" } [build-dependencies] +build_helper = { path = "../build_helper" } gcc = "0.3.27" diff --git a/src/libcompiler_builtins/build.rs b/src/libcompiler_builtins/build.rs index 16ecf88256670..564404ca71d61 100644 --- a/src/libcompiler_builtins/build.rs +++ b/src/libcompiler_builtins/build.rs @@ -33,6 +33,7 @@ //! error (if any) and then we just add it to the list. Overall, that cost is //! far far less than working with compiler-rt's build system over time. +extern crate build_helper; extern crate gcc; use std::collections::BTreeMap; @@ -404,5 +405,8 @@ fn main() { cfg.file(Path::new("../compiler-rt/lib/builtins").join(src)); } + // Can't reuse `sources` list becuse it doesn't contain header files. + build_helper::rerun_if_changed_anything_in_dir(Path::new("../compiler-rt")); + cfg.compile("libcompiler-rt.a"); } diff --git a/src/libflate/build.rs b/src/libflate/build.rs index 12016980a2c65..78d2ef1e37d2a 100644 --- a/src/libflate/build.rs +++ b/src/libflate/build.rs @@ -11,6 +11,7 @@ extern crate gcc; fn main() { + println!("cargo:rerun-if-changed=../rt/miniz.c"); gcc::Config::new() .file("../rt/miniz.c") .compile("libminiz.a"); diff --git a/src/librustc_asan/build.rs b/src/librustc_asan/build.rs index 015be14bd495a..3e95e0dbebefa 100644 --- a/src/librustc_asan/build.rs +++ b/src/librustc_asan/build.rs @@ -34,6 +34,4 @@ fn main() { .unwrap()) .join("../compiler-rt")); } - - println!("cargo:rerun-if-changed=build.rs"); } diff --git a/src/librustc_llvm/build.rs b/src/librustc_llvm/build.rs index c74a9308e4eba..b74bccb70593f 100644 --- a/src/librustc_llvm/build.rs +++ b/src/librustc_llvm/build.rs @@ -144,9 +144,7 @@ fn main() { cfg.flag("-DLLVM_RUSTLLVM"); } - println!("cargo:rerun-if-changed=../rustllvm/PassWrapper.cpp"); - println!("cargo:rerun-if-changed=../rustllvm/RustWrapper.cpp"); - println!("cargo:rerun-if-changed=../rustllvm/ArchiveWrapper.cpp"); + build_helper::rerun_if_changed_anything_in_dir(Path::new("../rustllvm")); cfg.file("../rustllvm/PassWrapper.cpp") .file("../rustllvm/RustWrapper.cpp") .file("../rustllvm/ArchiveWrapper.cpp") diff --git a/src/librustc_lsan/build.rs b/src/librustc_lsan/build.rs index 5773777d1f81b..ec968f51184fe 100644 --- a/src/librustc_lsan/build.rs +++ b/src/librustc_lsan/build.rs @@ -34,6 +34,4 @@ fn main() { .unwrap()) .join("../compiler-rt")); } - - println!("cargo:rerun-if-changed=build.rs"); } diff --git a/src/librustc_msan/build.rs b/src/librustc_msan/build.rs index 7a4c8f7073933..466fa641deabb 100644 --- a/src/librustc_msan/build.rs +++ b/src/librustc_msan/build.rs @@ -34,6 +34,4 @@ fn main() { .unwrap()) .join("../compiler-rt")); } - - println!("cargo:rerun-if-changed=build.rs"); } diff --git a/src/librustc_tsan/build.rs b/src/librustc_tsan/build.rs index 84326ae8a7106..95ce237b26735 100644 --- a/src/librustc_tsan/build.rs +++ b/src/librustc_tsan/build.rs @@ -34,6 +34,4 @@ fn main() { .unwrap()) .join("../compiler-rt")); } - - println!("cargo:rerun-if-changed=build.rs"); } diff --git a/src/librustdoc/build.rs b/src/librustdoc/build.rs index fcb7af11dce2f..9fa6406c1d8b6 100644 --- a/src/librustdoc/build.rs +++ b/src/librustdoc/build.rs @@ -8,9 +8,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +extern crate build_helper; extern crate gcc; fn main() { + let src_dir = std::path::Path::new("../rt/hoedown/src"); + build_helper::rerun_if_changed_anything_in_dir(src_dir); let mut cfg = gcc::Config::new(); cfg.file("../rt/hoedown/src/autolink.c") .file("../rt/hoedown/src/buffer.c") @@ -21,6 +24,6 @@ fn main() { .file("../rt/hoedown/src/html_smartypants.c") .file("../rt/hoedown/src/stack.c") .file("../rt/hoedown/src/version.c") - .include("../rt/hoedown/src") + .include(src_dir) .compile("libhoedown.a"); } diff --git a/src/libstd/build.rs b/src/libstd/build.rs index 038dea77f3ead..834e3d092112a 100644 --- a/src/libstd/build.rs +++ b/src/libstd/build.rs @@ -21,8 +21,6 @@ use std::process::Command; use build_helper::{run, rerun_if_changed_anything_in_dir, up_to_date}; fn main() { - println!("cargo:rerun-if-changed=build.rs"); - let target = env::var("TARGET").expect("TARGET was not set"); let host = env::var("HOST").expect("HOST was not set"); if cfg!(feature = "backtrace") && !target.contains("apple") && !target.contains("msvc") && diff --git a/src/libunwind/build.rs b/src/libunwind/build.rs index ea0d76978339d..ed3d5212bf256 100644 --- a/src/libunwind/build.rs +++ b/src/libunwind/build.rs @@ -11,6 +11,7 @@ use std::env; fn main() { + println!("cargo:rerun-if-changed=build.rs"); let target = env::var("TARGET").expect("TARGET was not set"); if target.contains("linux") { From a7c8afd28d45018f3c3af9dec569c36bd4dea10a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 27 Feb 2017 23:39:16 +0300 Subject: [PATCH 19/29] Support combination MSVC + Ninja --- src/Cargo.lock | 14 +++++++------- src/bootstrap/lib.rs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index e7fa572fa3ad3..aa4d65de0ac57 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -80,7 +80,7 @@ name = "bootstrap" version = "0.0.0" dependencies = [ "build_helper 0.1.0", - "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "cmake 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "filetime 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", @@ -202,7 +202,7 @@ dependencies = [ [[package]] name = "cmake" -version = "0.1.20" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", @@ -894,7 +894,7 @@ version = "0.0.0" dependencies = [ "alloc_system 0.0.0", "build_helper 0.1.0", - "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "cmake 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "core 0.0.0", ] @@ -1037,7 +1037,7 @@ version = "0.0.0" dependencies = [ "alloc_system 0.0.0", "build_helper 0.1.0", - "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "cmake 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "core 0.0.0", ] @@ -1081,7 +1081,7 @@ version = "0.0.0" dependencies = [ "alloc_system 0.0.0", "build_helper 0.1.0", - "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "cmake 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "core 0.0.0", ] @@ -1173,7 +1173,7 @@ version = "0.0.0" dependencies = [ "alloc_system 0.0.0", "build_helper 0.1.0", - "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "cmake 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "core 0.0.0", ] @@ -1573,7 +1573,7 @@ dependencies = [ "checksum bufstream 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b48dbe2ff0e98fa2f03377d204a9637d3c9816cd431bfe05a8abbd0ea11d074" "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" "checksum clap 2.20.5 (registry+https://github.com/rust-lang/crates.io-index)" = "7db281b0520e97fbd15cd615dcd8f8bcad0c26f5f7d5effe705f090f39e9a758" -"checksum cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)" = "a3a6805df695087e7c1bcd9a82e03ad6fb864c8e67ac41b1348229ce5b7f0407" +"checksum cmake 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "e1acc68a3f714627af38f9f5d09706a28584ba60dfe2cca68f40bf779f941b25" "checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97" "checksum curl 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c90e1240ef340dd4027ade439e5c7c2064dd9dc652682117bd50d1486a3add7b" "checksum curl-sys 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)" = "c0d909dc402ae80b6f7b0118c039203436061b9d9a3ca5d2c2546d93e0a61aaa" diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 5215b281fac0b..98b68d870d375 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -740,7 +740,7 @@ impl Build { } else { let base = self.llvm_out(&self.config.build).join("build"); let exe = exe("FileCheck", target); - if self.config.build.contains("msvc") { + if !self.config.ninja && self.config.build.contains("msvc") { base.join("Release/bin").join(exe) } else { base.join("bin").join(exe) From aeadc81ddcf25df677f75d781aa2ec9d732bb6f4 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 3 Mar 2017 02:15:56 +0300 Subject: [PATCH 20/29] Build compiler-rt and sanitizers only once --- src/build_helper/lib.rs | 37 +++++++++++++++++++++++++++- src/liballoc_jemalloc/build.rs | 40 ++++++++++++------------------- src/libcompiler_builtins/build.rs | 16 +++++++++---- src/librustc_asan/build.rs | 21 +++++++++------- src/librustc_lsan/build.rs | 21 +++++++++------- src/librustc_msan/build.rs | 21 +++++++++------- src/librustc_tsan/build.rs | 21 +++++++++------- src/libstd/build.rs | 32 ++++++++++--------------- 8 files changed, 123 insertions(+), 86 deletions(-) diff --git a/src/build_helper/lib.rs b/src/build_helper/lib.rs index 08f1f31c2d74b..2aac5ba6a1092 100644 --- a/src/build_helper/lib.rs +++ b/src/build_helper/lib.rs @@ -12,7 +12,7 @@ extern crate filetime; -use std::fs; +use std::{fs, env}; use std::process::{Command, Stdio}; use std::path::{Path, PathBuf}; @@ -166,6 +166,41 @@ pub fn up_to_date(src: &Path, dst: &Path) -> bool { } } +pub struct NativeLibBoilerplate { + pub skip_build: bool, + pub src_dir: PathBuf, + pub out_dir: PathBuf, + pub timestamp: PathBuf, +} + +pub fn native_lib_boilerplate(src_name: &str, + out_name: &str, + link_name: &str, + timestamp_name: &str, + search_subdir: &str) + -> NativeLibBoilerplate { + let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + let src_dir = current_dir.join("..").join(src_name); + rerun_if_changed_anything_in_dir(&src_dir); + + let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap()); + let out_dir = PathBuf::from(out_dir).join(out_name); + let _ = fs::create_dir_all(&out_dir); + println!("cargo:rustc-link-lib=static={}", link_name); + println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display()); + + let timestamp = out_dir.join(timestamp_name); + let skip_build = up_to_date(Path::new("build.rs"), ×tamp) && + up_to_date(&src_dir, ×tamp); + + NativeLibBoilerplate { + skip_build: skip_build, + src_dir: src_dir, + out_dir: out_dir, + timestamp: timestamp, + } +} + fn dir_up_to_date(src: &Path, threshold: &FileTime) -> bool { t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| { let meta = t!(e.metadata()); diff --git a/src/liballoc_jemalloc/build.rs b/src/liballoc_jemalloc/build.rs index cd2e8e4ec20fa..cc1e74ccbbf66 100644 --- a/src/liballoc_jemalloc/build.rs +++ b/src/liballoc_jemalloc/build.rs @@ -15,10 +15,10 @@ extern crate build_helper; extern crate gcc; use std::env; -use std::fs::{self, File}; -use std::path::{Path, PathBuf}; +use std::fs::File; +use std::path::PathBuf; use std::process::Command; -use build_helper::{run, rerun_if_changed_anything_in_dir, up_to_date}; +use build_helper::{run, native_lib_boilerplate}; fn main() { // FIXME: This is a hack to support building targets that don't @@ -59,20 +59,10 @@ fn main() { return; } - let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap()); - let build_dir = PathBuf::from(build_dir).join("jemalloc"); - let _ = fs::create_dir_all(&build_dir); - - if target.contains("windows") { - println!("cargo:rustc-link-lib=static=jemalloc"); - } else { - println!("cargo:rustc-link-lib=static=jemalloc_pic"); - } - println!("cargo:rustc-link-search=native={}/lib", build_dir.display()); - let src_dir = env::current_dir().unwrap().join("../jemalloc"); - rerun_if_changed_anything_in_dir(&src_dir); - let timestamp = build_dir.join("rustbuild.timestamp"); - if up_to_date(&Path::new("build.rs"), ×tamp) && up_to_date(&src_dir, ×tamp) { + let link_name = if target.contains("windows") { "jemalloc" } else { "jemalloc_pic" }; + let native = native_lib_boilerplate("jemalloc", "jemalloc", link_name, + "rustbuild.timestamp", "lib"); + if native.skip_build { return } @@ -86,12 +76,12 @@ fn main() { .join(" "); let mut cmd = Command::new("sh"); - cmd.arg(src_dir.join("configure") - .to_str() - .unwrap() - .replace("C:\\", "/c/") - .replace("\\", "/")) - .current_dir(&build_dir) + cmd.arg(native.src_dir.join("configure") + .to_str() + .unwrap() + .replace("C:\\", "/c/") + .replace("\\", "/")) + .current_dir(&native.out_dir) .env("CC", compiler.path()) .env("EXTRA_CFLAGS", cflags.clone()) // jemalloc generates Makefile deps using GCC's "-MM" flag. This means @@ -164,7 +154,7 @@ fn main() { run(&mut cmd); let mut make = Command::new(build_helper::make(&host)); - make.current_dir(&build_dir) + make.current_dir(&native.out_dir) .arg("build_lib_static"); // mingw make seems... buggy? unclear... @@ -186,5 +176,5 @@ fn main() { .compile("libpthread_atfork_dummy.a"); } - t!(File::create(×tamp)); + t!(File::create(&native.timestamp)); } diff --git a/src/libcompiler_builtins/build.rs b/src/libcompiler_builtins/build.rs index 564404ca71d61..ff5111a15be1f 100644 --- a/src/libcompiler_builtins/build.rs +++ b/src/libcompiler_builtins/build.rs @@ -39,6 +39,7 @@ extern crate gcc; use std::collections::BTreeMap; use std::env; use std::path::Path; +use build_helper::native_lib_boilerplate; struct Sources { // SYMBOL -> PATH TO SOURCE @@ -80,7 +81,17 @@ fn main() { return; } + // Can't reuse `sources` list for the freshness check becuse it doesn't contain header files. + // Use the produced library itself as a timestamp. + let out_name = "libcompiler-rt.a"; + let native = native_lib_boilerplate("compiler-rt", "compiler-rt", "compiler-rt", + out_name, "."); + if native.skip_build { + return + } + let cfg = &mut gcc::Config::new(); + cfg.out_dir(native.out_dir); if target.contains("msvc") { // Don't pull in extra libraries on MSVC @@ -405,8 +416,5 @@ fn main() { cfg.file(Path::new("../compiler-rt/lib/builtins").join(src)); } - // Can't reuse `sources` list becuse it doesn't contain header files. - build_helper::rerun_if_changed_anything_in_dir(Path::new("../compiler-rt")); - - cfg.compile("libcompiler-rt.a"); + cfg.compile(out_name); } diff --git a/src/librustc_asan/build.rs b/src/librustc_asan/build.rs index 3e95e0dbebefa..4772d1457750d 100644 --- a/src/librustc_asan/build.rs +++ b/src/librustc_asan/build.rs @@ -8,30 +8,33 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[macro_use] extern crate build_helper; extern crate cmake; -use std::path::PathBuf; use std::env; +use std::fs::File; +use build_helper::native_lib_boilerplate; use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let dst = Config::new("../compiler-rt") + let native = native_lib_boilerplate("compiler-rt", "asan", "clang_rt.asan-x86_64", + "rustbuild.timestamp", "build/lib/linux"); + if native.skip_build { + return + } + + Config::new(&native.src_dir) .define("COMPILER_RT_BUILD_SANITIZERS", "ON") .define("COMPILER_RT_BUILD_BUILTINS", "OFF") .define("COMPILER_RT_BUILD_XRAY", "OFF") .define("LLVM_CONFIG_PATH", llvm_config) + .out_dir(&native.out_dir) .build_target("asan") .build(); - println!("cargo:rustc-link-search=native={}", - dst.join("build/lib/linux").display()); - println!("cargo:rustc-link-lib=static=clang_rt.asan-x86_64"); - - build_helper::rerun_if_changed_anything_in_dir(&PathBuf::from(env::var("CARGO_MANIFEST_DIR") - .unwrap()) - .join("../compiler-rt")); + t!(File::create(&native.timestamp)); } } diff --git a/src/librustc_lsan/build.rs b/src/librustc_lsan/build.rs index ec968f51184fe..b71493db49ac9 100644 --- a/src/librustc_lsan/build.rs +++ b/src/librustc_lsan/build.rs @@ -8,30 +8,33 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[macro_use] extern crate build_helper; extern crate cmake; -use std::path::PathBuf; use std::env; +use std::fs::File; +use build_helper::native_lib_boilerplate; use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let dst = Config::new("../compiler-rt") + let native = native_lib_boilerplate("compiler-rt", "lsan", "clang_rt.lsan-x86_64", + "rustbuild.timestamp", "build/lib/linux"); + if native.skip_build { + return + } + + Config::new(&native.src_dir) .define("COMPILER_RT_BUILD_SANITIZERS", "ON") .define("COMPILER_RT_BUILD_BUILTINS", "OFF") .define("COMPILER_RT_BUILD_XRAY", "OFF") .define("LLVM_CONFIG_PATH", llvm_config) + .out_dir(&native.out_dir) .build_target("lsan") .build(); - println!("cargo:rustc-link-search=native={}", - dst.join("build/lib/linux").display()); - println!("cargo:rustc-link-lib=static=clang_rt.lsan-x86_64"); - - build_helper::rerun_if_changed_anything_in_dir(&PathBuf::from(env::var("CARGO_MANIFEST_DIR") - .unwrap()) - .join("../compiler-rt")); + t!(File::create(&native.timestamp)); } } diff --git a/src/librustc_msan/build.rs b/src/librustc_msan/build.rs index 466fa641deabb..07c4e807e7b08 100644 --- a/src/librustc_msan/build.rs +++ b/src/librustc_msan/build.rs @@ -8,30 +8,33 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[macro_use] extern crate build_helper; extern crate cmake; -use std::path::PathBuf; use std::env; +use std::fs::File; +use build_helper::native_lib_boilerplate; use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let dst = Config::new("../compiler-rt") + let native = native_lib_boilerplate("compiler-rt", "msan", "clang_rt.msan-x86_64", + "rustbuild.timestamp", "build/lib/linux"); + if native.skip_build { + return + } + + Config::new(&native.src_dir) .define("COMPILER_RT_BUILD_SANITIZERS", "ON") .define("COMPILER_RT_BUILD_BUILTINS", "OFF") .define("COMPILER_RT_BUILD_XRAY", "OFF") .define("LLVM_CONFIG_PATH", llvm_config) + .out_dir(&native.out_dir) .build_target("msan") .build(); - println!("cargo:rustc-link-search=native={}", - dst.join("build/lib/linux").display()); - println!("cargo:rustc-link-lib=static=clang_rt.msan-x86_64"); - - build_helper::rerun_if_changed_anything_in_dir(&PathBuf::from(env::var("CARGO_MANIFEST_DIR") - .unwrap()) - .join("../compiler-rt")); + t!(File::create(&native.timestamp)); } } diff --git a/src/librustc_tsan/build.rs b/src/librustc_tsan/build.rs index 95ce237b26735..3bd30fd203c8c 100644 --- a/src/librustc_tsan/build.rs +++ b/src/librustc_tsan/build.rs @@ -8,30 +8,33 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[macro_use] extern crate build_helper; extern crate cmake; -use std::path::PathBuf; use std::env; +use std::fs::File; +use build_helper::native_lib_boilerplate; use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let dst = Config::new("../compiler-rt") + let native = native_lib_boilerplate("compiler-rt", "tsan", "clang_rt.tsan-x86_64", + "rustbuild.timestamp", "build/lib/linux"); + if native.skip_build { + return + } + + Config::new(&native.src_dir) .define("COMPILER_RT_BUILD_SANITIZERS", "ON") .define("COMPILER_RT_BUILD_BUILTINS", "OFF") .define("COMPILER_RT_BUILD_XRAY", "OFF") .define("LLVM_CONFIG_PATH", llvm_config) + .out_dir(&native.out_dir) .build_target("tsan") .build(); - println!("cargo:rustc-link-search=native={}", - dst.join("build/lib/linux").display()); - println!("cargo:rustc-link-lib=static=clang_rt.tsan-x86_64"); - - build_helper::rerun_if_changed_anything_in_dir(&PathBuf::from(env::var("CARGO_MANIFEST_DIR") - .unwrap()) - .join("../compiler-rt")); + t!(File::create(&native.timestamp)); } } diff --git a/src/libstd/build.rs b/src/libstd/build.rs index 834e3d092112a..ef1d3c84f2a8d 100644 --- a/src/libstd/build.rs +++ b/src/libstd/build.rs @@ -15,10 +15,9 @@ extern crate build_helper; extern crate gcc; use std::env; -use std::fs::{self, File}; -use std::path::{Path, PathBuf}; +use std::fs::File; use std::process::Command; -use build_helper::{run, rerun_if_changed_anything_in_dir, up_to_date}; +use build_helper::{run, native_lib_boilerplate}; fn main() { let target = env::var("TARGET").expect("TARGET was not set"); @@ -68,16 +67,9 @@ fn main() { } fn build_libbacktrace(host: &str, target: &str) { - let build_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap()); - let build_dir = PathBuf::from(build_dir).join("libbacktrace"); - let _ = fs::create_dir_all(&build_dir); - - println!("cargo:rustc-link-lib=static=backtrace"); - println!("cargo:rustc-link-search=native={}/.libs", build_dir.display()); - let src_dir = env::current_dir().unwrap().join("../libbacktrace"); - rerun_if_changed_anything_in_dir(&src_dir); - let timestamp = build_dir.join("rustbuild.timestamp"); - if up_to_date(&Path::new("build.rs"), ×tamp) && up_to_date(&src_dir, ×tamp) { + let native = native_lib_boilerplate("libbacktrace", "libbacktrace", "backtrace", + "rustbuild.timestamp", ".libs"); + if native.skip_build { return } @@ -88,10 +80,10 @@ fn build_libbacktrace(host: &str, target: &str) { .collect::>().join(" "); cflags.push_str(" -fvisibility=hidden"); run(Command::new("sh") - .current_dir(&build_dir) - .arg(src_dir.join("configure").to_str().unwrap() - .replace("C:\\", "/c/") - .replace("\\", "/")) + .current_dir(&native.out_dir) + .arg(native.src_dir.join("configure").to_str().unwrap() + .replace("C:\\", "/c/") + .replace("\\", "/")) .arg("--with-pic") .arg("--disable-multilib") .arg("--disable-shared") @@ -104,9 +96,9 @@ fn build_libbacktrace(host: &str, target: &str) { .env("CFLAGS", cflags)); run(Command::new(build_helper::make(host)) - .current_dir(&build_dir) - .arg(format!("INCDIR={}", src_dir.display())) + .current_dir(&native.out_dir) + .arg(format!("INCDIR={}", native.src_dir.display())) .arg("-j").arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set"))); - t!(File::create(×tamp)); + t!(File::create(&native.timestamp)); } From 3b454665ea360619ceb45aac5b9eae6d5994dc64 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 3 Mar 2017 03:09:35 +0300 Subject: [PATCH 21/29] run-make on MSVC: Do not generate object files in the source directory --- src/test/run-make/cdylib/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/run-make/cdylib/Makefile b/src/test/run-make/cdylib/Makefile index ae3b82537db52..47ec762b3e94c 100644 --- a/src/test/run-make/cdylib/Makefile +++ b/src/test/run-make/cdylib/Makefile @@ -8,7 +8,7 @@ all: $(call RUN_BINFILE,foo) ifdef IS_MSVC $(call RUN_BINFILE,foo): $(call DYLIB,foo) - $(CC) $(CFLAGS) foo.c $(TMPDIR)/foo.dll.lib -Fe:`cygpath -w $@` + $(CC) $(CFLAGS) foo.c $(TMPDIR)/foo.dll.lib $(call OUT_EXE,foo) else $(call RUN_BINFILE,foo): $(call DYLIB,foo) $(CC) $(CFLAGS) foo.c -lfoo -o $(call RUN_BINFILE,foo) -L $(TMPDIR) From 11adac350b63ab3371a50b10184238ebc3a2be7f Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 3 Mar 2017 05:27:07 +0300 Subject: [PATCH 22/29] bootstrap.py: Report build status Move some code from x.py to bootstrap.py --- src/bootstrap/bootstrap.py | 21 +++++++++++++++------ x.py | 14 ++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py index c1ee0c29ac981..7dd53f41a214a 100644 --- a/src/bootstrap/bootstrap.py +++ b/src/bootstrap/bootstrap.py @@ -8,6 +8,7 @@ # option. This file may not be copied, modified, or distributed # except according to those terms. +from __future__ import print_function import argparse import contextlib import datetime @@ -501,7 +502,7 @@ def build_triple(self): return "{}-{}".format(cputype, ostype) -def main(): +def bootstrap(): parser = argparse.ArgumentParser(description='Build rust') parser.add_argument('--config') parser.add_argument('--clean', action='store_true') @@ -564,8 +565,6 @@ def main(): rb._rustc_channel, rb._rustc_date = data['rustc'].split('-', 1) rb._cargo_rev = data['cargo'] - start_time = time() - # Fetch/build the bootstrap rb.build = rb.build_triple() rb.download_stage0() @@ -582,9 +581,19 @@ def main(): env["BOOTSTRAP_PARENT_ID"] = str(os.getpid()) rb.run(args, env) - end_time = time() - - print("Build completed in %s" % format_build_time(end_time - start_time)) +def main(): + start_time = time() + try: + bootstrap() + print("Build completed successfully in %s" % format_build_time(time() - start_time)) + except (SystemExit, KeyboardInterrupt) as e: + if hasattr(e, 'code') and isinstance(e.code, int): + exit_code = e.code + else: + exit_code = 1 + print(e) + print("Build completed unsuccessfully in %s" % format_build_time(time() - start_time)) + sys.exit(exit_code) if __name__ == '__main__': main() diff --git a/x.py b/x.py index d281a6abc93e3..8f528889d6020 100755 --- a/x.py +++ b/x.py @@ -9,14 +9,12 @@ # option. This file may not be copied, modified, or distributed # except according to those terms. -import sys +# This file is only a "symlink" to boostrap.py, all logic should go there. + import os -dir = os.path.dirname(__file__) -sys.path.append(os.path.abspath(os.path.join(dir, "src", "bootstrap"))) +import sys +rust_dir = os.path.dirname(os.path.abspath(__file__)) +sys.path.append(os.path.join(rust_dir, "src", "bootstrap")) import bootstrap - -try: - bootstrap.main() -except KeyboardInterrupt: - sys.exit() +bootstrap.main() From e2f6185294e5e2c3412776a33ab977af20d4cdb4 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 3 Mar 2017 13:28:22 +0300 Subject: [PATCH 23/29] Separate "ui-fulldeps" tests from "ui" tests --- src/bootstrap/step.rs | 6 ++++-- .../{ui => ui-fulldeps}/custom-derive/auxiliary/plugin.rs | 0 src/test/{ui => ui-fulldeps}/custom-derive/issue-36935.rs | 0 .../{ui => ui-fulldeps}/custom-derive/issue-36935.stderr | 0 4 files changed, 4 insertions(+), 2 deletions(-) rename src/test/{ui => ui-fulldeps}/custom-derive/auxiliary/plugin.rs (100%) rename src/test/{ui => ui-fulldeps}/custom-derive/issue-36935.rs (100%) rename src/test/{ui => ui-fulldeps}/custom-derive/issue-36935.stderr (100%) diff --git a/src/bootstrap/step.rs b/src/bootstrap/step.rs index 7e4771dd7dc2f..a5c0d11d21985 100644 --- a/src/bootstrap/step.rs +++ b/src/bootstrap/step.rs @@ -312,6 +312,7 @@ pub fn build_rules<'a>(build: &'a Build) -> Rules { }); }; + suite("check-ui", "src/test/ui", "ui", "ui"); suite("check-rpass", "src/test/run-pass", "run-pass", "run-pass"); suite("check-cfail", "src/test/compile-fail", "compile-fail", "compile-fail"); suite("check-pfail", "src/test/parse-fail", "parse-fail", "parse-fail"); @@ -372,7 +373,7 @@ pub fn build_rules<'a>(build: &'a Build) -> Rules { }); }; - suite("check-ui", "src/test/ui", "ui", "ui"); + suite("check-ui-full", "src/test/ui-fulldeps", "ui", "ui-fulldeps"); suite("check-rpass-full", "src/test/run-pass-fulldeps", "run-pass", "run-pass-fulldeps"); suite("check-rfail-full", "src/test/run-fail-fulldeps", @@ -1530,7 +1531,8 @@ mod tests { assert!(plan.iter().all(|s| s.host == "A")); assert!(plan.iter().all(|s| s.target == "C")); - assert!(!plan.iter().any(|s| s.name.contains("-ui"))); + assert!(plan.iter().any(|s| s.name.contains("-ui"))); + assert!(!plan.iter().any(|s| s.name.contains("ui-full"))); assert!(plan.iter().any(|s| s.name.contains("cfail"))); assert!(!plan.iter().any(|s| s.name.contains("cfail-full"))); assert!(plan.iter().any(|s| s.name.contains("codegen-units"))); diff --git a/src/test/ui/custom-derive/auxiliary/plugin.rs b/src/test/ui-fulldeps/custom-derive/auxiliary/plugin.rs similarity index 100% rename from src/test/ui/custom-derive/auxiliary/plugin.rs rename to src/test/ui-fulldeps/custom-derive/auxiliary/plugin.rs diff --git a/src/test/ui/custom-derive/issue-36935.rs b/src/test/ui-fulldeps/custom-derive/issue-36935.rs similarity index 100% rename from src/test/ui/custom-derive/issue-36935.rs rename to src/test/ui-fulldeps/custom-derive/issue-36935.rs diff --git a/src/test/ui/custom-derive/issue-36935.stderr b/src/test/ui-fulldeps/custom-derive/issue-36935.stderr similarity index 100% rename from src/test/ui/custom-derive/issue-36935.stderr rename to src/test/ui-fulldeps/custom-derive/issue-36935.stderr From c652a4fb566ac4bec1d62c66769dd055ad239df6 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 3 Mar 2017 17:18:44 +0300 Subject: [PATCH 24/29] Do not purge LLVM build directory on rebuild Add some comments --- src/bootstrap/native.rs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index 90e1530308f4a..483f45fdd6218 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -41,28 +41,32 @@ pub fn llvm(build: &Build, target: &str) { } } - // If the cleaning trigger is newer than our built artifacts (or if the - // artifacts are missing) then we keep going, otherwise we bail out. - let dst = build.llvm_out(target); - let stamp = build.src.join("src/rustllvm/llvm-auto-clean-trigger"); - let mut stamp_contents = String::new(); - t!(t!(File::open(&stamp)).read_to_string(&mut stamp_contents)); - let done_stamp = dst.join("llvm-finished-building"); + let clean_trigger = build.src.join("src/rustllvm/llvm-auto-clean-trigger"); + let mut clean_trigger_contents = String::new(); + t!(t!(File::open(&clean_trigger)).read_to_string(&mut clean_trigger_contents)); + + let out_dir = build.llvm_out(target); + let done_stamp = out_dir.join("llvm-finished-building"); if done_stamp.exists() { let mut done_contents = String::new(); t!(t!(File::open(&done_stamp)).read_to_string(&mut done_contents)); - if done_contents == stamp_contents { + + // LLVM was already built previously. + // We don't track changes in LLVM sources, so we need to choose between reusing + // what was built previously, or cleaning the directory and doing a fresh build. + // The choice depends on contents of the clean-trigger file. + // If the contents are the same as during the previous build, then no action is required. + // If the contents differ from the previous build, then cleaning is triggered. + if done_contents == clean_trigger_contents { return + } else { + t!(fs::remove_dir_all(&out_dir)); } } - drop(fs::remove_dir_all(&dst)); println!("Building LLVM for {}", target); - let _time = util::timeit(); - let _ = fs::remove_dir_all(&dst.join("build")); - t!(fs::create_dir_all(&dst.join("build"))); - let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"}; + t!(fs::create_dir_all(&out_dir)); // http://llvm.org/docs/CMake.html let mut cfg = cmake::Config::new(build.src.join("src/llvm")); @@ -82,9 +86,11 @@ pub fn llvm(build: &Build, target: &str) { None => "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX", }; + let assertions = if build.config.llvm_assertions {"ON"} else {"OFF"}; + cfg.target(target) .host(&build.config.build) - .out_dir(&dst) + .out_dir(&out_dir) .profile(profile) .define("LLVM_ENABLE_ASSERTIONS", assertions) .define("LLVM_TARGETS_TO_BUILD", llvm_targets) @@ -142,7 +148,7 @@ pub fn llvm(build: &Build, target: &str) { // tools and libs on all platforms. cfg.build(); - t!(t!(File::create(&done_stamp)).write_all(stamp_contents.as_bytes())); + t!(t!(File::create(&done_stamp)).write_all(clean_trigger_contents.as_bytes())); } fn check_llvm_version(build: &Build, llvm_config: &Path) { From 428f063fcdc35e048ff79d059a8963334ba2281c Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 3 Mar 2017 20:11:04 +0300 Subject: [PATCH 25/29] Automate timestamp creation and build skipping for native libraries Add comments --- src/Cargo.lock | 4 ++-- src/build_helper/lib.rs | 33 +++++++++++++++++++------------ src/liballoc_jemalloc/build.rs | 13 ++++-------- src/libcompiler_builtins/build.rs | 15 ++++++-------- src/librustc_asan/build.rs | 14 +++++-------- src/librustc_lsan/build.rs | 14 +++++-------- src/librustc_msan/build.rs | 14 +++++-------- src/librustc_tsan/build.rs | 14 +++++-------- src/libstd/build.rs | 15 ++++---------- 9 files changed, 56 insertions(+), 80 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index aa4d65de0ac57..f4174693a5771 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -487,7 +487,7 @@ name = "libgit2-sys" version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "cmake 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "curl-sys 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)", "gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", @@ -502,7 +502,7 @@ name = "libssh2-sys" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "cmake 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", "libz-sys 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/build_helper/lib.rs b/src/build_helper/lib.rs index 2aac5ba6a1092..dffaebbd92914 100644 --- a/src/build_helper/lib.rs +++ b/src/build_helper/lib.rs @@ -13,6 +13,7 @@ extern crate filetime; use std::{fs, env}; +use std::fs::File; use std::process::{Command, Stdio}; use std::path::{Path, PathBuf}; @@ -166,19 +167,29 @@ pub fn up_to_date(src: &Path, dst: &Path) -> bool { } } +#[must_use] pub struct NativeLibBoilerplate { - pub skip_build: bool, pub src_dir: PathBuf, pub out_dir: PathBuf, - pub timestamp: PathBuf, } +impl Drop for NativeLibBoilerplate { + fn drop(&mut self) { + t!(File::create(self.out_dir.join("rustbuild.timestamp"))); + } +} + +// Perform standard preparations for native libraries that are build only once for all stages. +// Emit rerun-if-changed and linking attributes for Cargo, check if any source files are +// updated, calculate paths used later in actual build with CMake/make or C/C++ compiler. +// If Err is returned, then everything is up-to-date and further build actions can be skipped. +// Timestamps are created automatically when the result of `native_lib_boilerplate` goes out +// of scope, so all the build actions should be completed until then. pub fn native_lib_boilerplate(src_name: &str, out_name: &str, link_name: &str, - timestamp_name: &str, search_subdir: &str) - -> NativeLibBoilerplate { + -> Result { let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let src_dir = current_dir.join("..").join(src_name); rerun_if_changed_anything_in_dir(&src_dir); @@ -189,15 +200,11 @@ pub fn native_lib_boilerplate(src_name: &str, println!("cargo:rustc-link-lib=static={}", link_name); println!("cargo:rustc-link-search=native={}", out_dir.join(search_subdir).display()); - let timestamp = out_dir.join(timestamp_name); - let skip_build = up_to_date(Path::new("build.rs"), ×tamp) && - up_to_date(&src_dir, ×tamp); - - NativeLibBoilerplate { - skip_build: skip_build, - src_dir: src_dir, - out_dir: out_dir, - timestamp: timestamp, + let timestamp = out_dir.join("rustbuild.timestamp"); + if !up_to_date(Path::new("build.rs"), ×tamp) || !up_to_date(&src_dir, ×tamp) { + Ok(NativeLibBoilerplate { src_dir: src_dir, out_dir: out_dir }) + } else { + Err(()) } } diff --git a/src/liballoc_jemalloc/build.rs b/src/liballoc_jemalloc/build.rs index cc1e74ccbbf66..ae040a2390659 100644 --- a/src/liballoc_jemalloc/build.rs +++ b/src/liballoc_jemalloc/build.rs @@ -10,12 +10,10 @@ #![deny(warnings)] -#[macro_use] extern crate build_helper; extern crate gcc; use std::env; -use std::fs::File; use std::path::PathBuf; use std::process::Command; use build_helper::{run, native_lib_boilerplate}; @@ -60,11 +58,10 @@ fn main() { } let link_name = if target.contains("windows") { "jemalloc" } else { "jemalloc_pic" }; - let native = native_lib_boilerplate("jemalloc", "jemalloc", link_name, - "rustbuild.timestamp", "lib"); - if native.skip_build { - return - } + let native = match native_lib_boilerplate("jemalloc", "jemalloc", link_name, "lib") { + Ok(native) => native, + _ => return, + }; let compiler = gcc::Config::new().get_compiler(); // only msvc returns None for ar so unwrap is okay @@ -175,6 +172,4 @@ fn main() { .file("pthread_atfork_dummy.c") .compile("libpthread_atfork_dummy.a"); } - - t!(File::create(&native.timestamp)); } diff --git a/src/libcompiler_builtins/build.rs b/src/libcompiler_builtins/build.rs index ff5111a15be1f..bcd3a92dd4305 100644 --- a/src/libcompiler_builtins/build.rs +++ b/src/libcompiler_builtins/build.rs @@ -82,16 +82,13 @@ fn main() { } // Can't reuse `sources` list for the freshness check becuse it doesn't contain header files. - // Use the produced library itself as a timestamp. - let out_name = "libcompiler-rt.a"; - let native = native_lib_boilerplate("compiler-rt", "compiler-rt", "compiler-rt", - out_name, "."); - if native.skip_build { - return - } + let native = match native_lib_boilerplate("compiler-rt", "compiler-rt", "compiler-rt", ".") { + Ok(native) => native, + _ => return, + }; let cfg = &mut gcc::Config::new(); - cfg.out_dir(native.out_dir); + cfg.out_dir(&native.out_dir); if target.contains("msvc") { // Don't pull in extra libraries on MSVC @@ -416,5 +413,5 @@ fn main() { cfg.file(Path::new("../compiler-rt/lib/builtins").join(src)); } - cfg.compile(out_name); + cfg.compile("libcompiler-rt.a"); } diff --git a/src/librustc_asan/build.rs b/src/librustc_asan/build.rs index 4772d1457750d..2df2e001e6ff2 100644 --- a/src/librustc_asan/build.rs +++ b/src/librustc_asan/build.rs @@ -8,23 +8,21 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[macro_use] extern crate build_helper; extern crate cmake; use std::env; -use std::fs::File; use build_helper::native_lib_boilerplate; use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let native = native_lib_boilerplate("compiler-rt", "asan", "clang_rt.asan-x86_64", - "rustbuild.timestamp", "build/lib/linux"); - if native.skip_build { - return - } + let native = match native_lib_boilerplate("compiler-rt", "asan", "clang_rt.asan-x86_64", + "build/lib/linux") { + Ok(native) => native, + _ => return, + }; Config::new(&native.src_dir) .define("COMPILER_RT_BUILD_SANITIZERS", "ON") @@ -34,7 +32,5 @@ fn main() { .out_dir(&native.out_dir) .build_target("asan") .build(); - - t!(File::create(&native.timestamp)); } } diff --git a/src/librustc_lsan/build.rs b/src/librustc_lsan/build.rs index b71493db49ac9..005163f41026c 100644 --- a/src/librustc_lsan/build.rs +++ b/src/librustc_lsan/build.rs @@ -8,23 +8,21 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[macro_use] extern crate build_helper; extern crate cmake; use std::env; -use std::fs::File; use build_helper::native_lib_boilerplate; use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let native = native_lib_boilerplate("compiler-rt", "lsan", "clang_rt.lsan-x86_64", - "rustbuild.timestamp", "build/lib/linux"); - if native.skip_build { - return - } + let native = match native_lib_boilerplate("compiler-rt", "lsan", "clang_rt.lsan-x86_64", + "build/lib/linux") { + Ok(native) => native, + _ => return, + }; Config::new(&native.src_dir) .define("COMPILER_RT_BUILD_SANITIZERS", "ON") @@ -34,7 +32,5 @@ fn main() { .out_dir(&native.out_dir) .build_target("lsan") .build(); - - t!(File::create(&native.timestamp)); } } diff --git a/src/librustc_msan/build.rs b/src/librustc_msan/build.rs index 07c4e807e7b08..c438b5250463b 100644 --- a/src/librustc_msan/build.rs +++ b/src/librustc_msan/build.rs @@ -8,23 +8,21 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[macro_use] extern crate build_helper; extern crate cmake; use std::env; -use std::fs::File; use build_helper::native_lib_boilerplate; use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let native = native_lib_boilerplate("compiler-rt", "msan", "clang_rt.msan-x86_64", - "rustbuild.timestamp", "build/lib/linux"); - if native.skip_build { - return - } + let native = match native_lib_boilerplate("compiler-rt", "msan", "clang_rt.msan-x86_64", + "build/lib/linux") { + Ok(native) => native, + _ => return, + }; Config::new(&native.src_dir) .define("COMPILER_RT_BUILD_SANITIZERS", "ON") @@ -34,7 +32,5 @@ fn main() { .out_dir(&native.out_dir) .build_target("msan") .build(); - - t!(File::create(&native.timestamp)); } } diff --git a/src/librustc_tsan/build.rs b/src/librustc_tsan/build.rs index 3bd30fd203c8c..055b344d2e947 100644 --- a/src/librustc_tsan/build.rs +++ b/src/librustc_tsan/build.rs @@ -8,23 +8,21 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[macro_use] extern crate build_helper; extern crate cmake; use std::env; -use std::fs::File; use build_helper::native_lib_boilerplate; use cmake::Config; fn main() { if let Some(llvm_config) = env::var_os("LLVM_CONFIG") { - let native = native_lib_boilerplate("compiler-rt", "tsan", "clang_rt.tsan-x86_64", - "rustbuild.timestamp", "build/lib/linux"); - if native.skip_build { - return - } + let native = match native_lib_boilerplate("compiler-rt", "tsan", "clang_rt.tsan-x86_64", + "build/lib/linux") { + Ok(native) => native, + _ => return, + }; Config::new(&native.src_dir) .define("COMPILER_RT_BUILD_SANITIZERS", "ON") @@ -34,7 +32,5 @@ fn main() { .out_dir(&native.out_dir) .build_target("tsan") .build(); - - t!(File::create(&native.timestamp)); } } diff --git a/src/libstd/build.rs b/src/libstd/build.rs index ef1d3c84f2a8d..9fb83ad75980a 100644 --- a/src/libstd/build.rs +++ b/src/libstd/build.rs @@ -10,12 +10,10 @@ #![deny(warnings)] -#[macro_use] extern crate build_helper; extern crate gcc; use std::env; -use std::fs::File; use std::process::Command; use build_helper::{run, native_lib_boilerplate}; @@ -24,7 +22,7 @@ fn main() { let host = env::var("HOST").expect("HOST was not set"); if cfg!(feature = "backtrace") && !target.contains("apple") && !target.contains("msvc") && !target.contains("emscripten") && !target.contains("fuchsia") && !target.contains("redox") { - build_libbacktrace(&host, &target); + let _ = build_libbacktrace(&host, &target); } if target.contains("linux") { @@ -66,12 +64,8 @@ fn main() { } } -fn build_libbacktrace(host: &str, target: &str) { - let native = native_lib_boilerplate("libbacktrace", "libbacktrace", "backtrace", - "rustbuild.timestamp", ".libs"); - if native.skip_build { - return - } +fn build_libbacktrace(host: &str, target: &str) -> Result<(), ()> { + let native = native_lib_boilerplate("libbacktrace", "libbacktrace", "backtrace", ".libs")?; let compiler = gcc::Config::new().get_compiler(); // only msvc returns None for ar so unwrap is okay @@ -99,6 +93,5 @@ fn build_libbacktrace(host: &str, target: &str) { .current_dir(&native.out_dir) .arg(format!("INCDIR={}", native.src_dir.display())) .arg("-j").arg(env::var("NUM_JOBS").expect("NUM_JOBS was not set"))); - - t!(File::create(&native.timestamp)); + Ok(()) } From 69899b7f27b1a9511abea9d2187def6967d7d7f7 Mon Sep 17 00:00:00 2001 From: Mark Simulacrum Date: Sat, 4 Mar 2017 12:37:45 -0700 Subject: [PATCH 26/29] Inline function to avoid naming confusion. --- src/libsyntax/parse/parser.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6e3724b5fd87b..6c566dab1d606 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1032,13 +1032,6 @@ impl<'a> Parser<'a> { self.check_unknown_macro_variable(); } - /// Advance the parser by one token and return the bumped token. - pub fn bump_and_get(&mut self) -> token::Token { - let old_token = mem::replace(&mut self.token, token::Underscore); - self.bump(); - old_token - } - /// Advance the parser using provided token as a next one. Use this when /// consuming a part of a token. For example a single `<` from `<<`. pub fn bump_with(&mut self, @@ -2663,7 +2656,12 @@ impl<'a> Parser<'a> { })); }, token::CloseDelim(_) | token::Eof => unreachable!(), - _ => Ok(TokenTree::Token(self.span, self.bump_and_get())), + _ => { + let token = mem::replace(&mut self.token, token::Underscore); + let res = Ok(TokenTree::Token(self.span, token)); + self.bump(); + res + } } } From 255d59f88430cbd9aaa5c584fcd3d4a8dd90a345 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sat, 4 Mar 2017 11:46:14 +0900 Subject: [PATCH 27/29] Add compile fail test for stmt_expr_attributes --- .../feature-gate-stmt_expr_attributes.rs | 14 ++++++++++++++ src/tools/tidy/src/features.rs | 1 - 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/feature-gate-stmt_expr_attributes.rs diff --git a/src/test/compile-fail/feature-gate-stmt_expr_attributes.rs b/src/test/compile-fail/feature-gate-stmt_expr_attributes.rs new file mode 100644 index 0000000000000..831d8862e109c --- /dev/null +++ b/src/test/compile-fail/feature-gate-stmt_expr_attributes.rs @@ -0,0 +1,14 @@ +// Copyright 2015 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. + +const X: i32 = #[allow(dead_code)] 8; +//~^ ERROR attributes on non-item statements and expressions are experimental. (see issue #15701) + +fn main() {} diff --git a/src/tools/tidy/src/features.rs b/src/tools/tidy/src/features.rs index d3c4378f9e75d..93593b97555a0 100644 --- a/src/tools/tidy/src/features.rs +++ b/src/tools/tidy/src/features.rs @@ -167,7 +167,6 @@ pub fn check(path: &Path, bad: &mut bool) { // FIXME get this whitelist empty. let whitelist = vec![ - "stmt_expr_attributes", "cfg_target_thread_local", "unwind_attributes", ]; From 75eface16d55984c402ef6e6f131801a70939660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 9 Feb 2017 17:54:56 -0800 Subject: [PATCH 28/29] Clean up "pattern doesn't bind x" messages Group "missing variable bind" spans in `or` matches and clarify wording for the two possible cases: when a variable from the first pattern is not in any of the subsequent patterns, and when a variable in any of the other patterns is not in the first one. Before: ``` error[E0408]: variable `a` from pattern #1 is not bound in pattern #2 --> file.rs:10:23 | 10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } | ^^^^^^^^^^^ pattern doesn't bind `a` error[E0408]: variable `b` from pattern #2 is not bound in pattern #1 --> file.rs:10:32 | 10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } | ^ pattern doesn't bind `b` error[E0408]: variable `a` from pattern #1 is not bound in pattern #3 --> file.rs:10:37 | 10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } | ^^^^^^^^ pattern doesn't bind `a` error[E0408]: variable `d` from pattern #1 is not bound in pattern #3 --> file.rs:10:37 | 10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } | ^^^^^^^^ pattern doesn't bind `d` error[E0408]: variable `c` from pattern #3 is not bound in pattern #1 --> file.rs:10:43 | 10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } | ^ pattern doesn't bind `c` error[E0408]: variable `d` from pattern #1 is not bound in pattern #4 --> file.rs:10:48 | 10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } | ^^^^^^^^ pattern doesn't bind `d` error: aborting due to 6 previous errors ``` After: ``` error[E0408]: variable `a` is not bound in all patterns --> file.rs:20:37 | 20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { intln!("{:?}", a); } | - ^^^^^^^^^^^ ^^^^^^^^ - variable t in all patterns | | | | | | | pattern doesn't bind `a` | | pattern doesn't bind `a` | variable not in all patterns error[E0408]: variable `d` is not bound in all patterns --> file.rs:20:37 | 20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { intln!("{:?}", a); } | - - ^^^^^^^^ ^^^^^^^^ pattern esn't bind `d` | | | | | | | pattern doesn't bind `d` | | variable not in all patterns | variable not in all patterns error[E0408]: variable `b` is not bound in all patterns --> file.rs:20:37 | 20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { intln!("{:?}", a); } | ^^^^^^^^^^^ - ^^^^^^^^ ^^^^^^^^ pattern esn't bind `b` | | | | | | | pattern doesn't bind `b` | | variable not in all patterns | pattern doesn't bind `b` error[E0408]: variable `c` is not bound in all patterns --> file.rs:20:48 | 20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { intln!("{:?}", a); } | ^^^^^^^^^^^ ^^^^^^^^^^^ - ^^^^^^^^ pattern esn't bind `c` | | | | | | | variable not in all tterns | | pattern doesn't bind `c` | pattern doesn't bind `c` error: aborting due to 4 previous errors ``` * Have only one presentation for binding consistency errors * Point to same binding in multiple patterns when possible * Check inconsistent bindings in all arms * Simplify wording of diagnostic message * Sort emition and spans of binding errors for deterministic output --- src/librustc_resolve/lib.rs | 140 +++++++++++++----- src/test/compile-fail/E0408.rs | 3 +- src/test/compile-fail/issue-2848.rs | 3 +- src/test/compile-fail/issue-2849.rs | 2 +- .../resolve-inconsistent-binding-mode.rs | 6 +- .../resolve-inconsistent-names.rs | 6 +- src/test/ui/mismatched_types/E0409.stderr | 2 +- src/test/ui/span/issue-39698.rs | 22 +++ src/test/ui/span/issue-39698.stderr | 42 ++++++ 9 files changed, 176 insertions(+), 50 deletions(-) create mode 100644 src/test/ui/span/issue-39698.rs create mode 100644 src/test/ui/span/issue-39698.stderr diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 093994ba8257d..adb0eee652bd0 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -97,6 +97,31 @@ enum AssocSuggestion { AssocItem, } +#[derive(Eq)] +struct BindingError { + name: Name, + origin: FxHashSet, + target: FxHashSet, +} + +impl PartialOrd for BindingError { + fn partial_cmp(&self, other: &BindingError) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for BindingError { + fn eq(&self, other: &BindingError) -> bool { + self.name == other.name + } +} + +impl Ord for BindingError { + fn cmp(&self, other: &BindingError) -> cmp::Ordering { + self.name.cmp(&other.name) + } +} + enum ResolutionError<'a> { /// error E0401: can't use type parameters from outer function TypeParametersFromOuterFunction, @@ -110,10 +135,10 @@ enum ResolutionError<'a> { TypeNotMemberOfTrait(Name, &'a str), /// error E0438: const is not a member of trait ConstNotMemberOfTrait(Name, &'a str), - /// error E0408: variable `{}` from pattern #{} is not bound in pattern #{} - VariableNotBoundInPattern(Name, usize, usize), - /// error E0409: variable is bound with different mode in pattern #{} than in pattern #1 - VariableBoundWithDifferentMode(Name, usize, Span), + /// error E0408: variable `{}` is not bound in all patterns + VariableNotBoundInPattern(&'a BindingError), + /// error E0409: variable `{}` is bound in inconsistent ways within the same match arm + VariableBoundWithDifferentMode(Name, Span), /// error E0415: identifier is bound more than once in this parameter list IdentifierBoundMoreThanOnceInParameterList(&'a str), /// error E0416: identifier is bound more than once in the same pattern @@ -207,27 +232,30 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver, err.span_label(span, &format!("not a member of trait `{}`", trait_)); err } - ResolutionError::VariableNotBoundInPattern(variable_name, from, to) => { - let mut err = struct_span_err!(resolver.session, - span, - E0408, - "variable `{}` from pattern #{} is not bound in pattern #{}", - variable_name, - from, - to); - err.span_label(span, &format!("pattern doesn't bind `{}`", variable_name)); + ResolutionError::VariableNotBoundInPattern(binding_error) => { + let mut target_sp = binding_error.target.iter().map(|x| *x).collect::>(); + target_sp.sort(); + let msp = MultiSpan::from_spans(target_sp.clone()); + let msg = format!("variable `{}` is not bound in all patterns", binding_error.name); + let mut err = resolver.session.struct_span_err_with_code(msp, &msg, "E0408"); + for sp in target_sp { + err.span_label(sp, &format!("pattern doesn't bind `{}`", binding_error.name)); + } + let mut origin_sp = binding_error.origin.iter().map(|x| *x).collect::>(); + origin_sp.sort(); + for sp in origin_sp { + err.span_label(sp, &"variable not in all patterns"); + } err } ResolutionError::VariableBoundWithDifferentMode(variable_name, - pattern_number, first_binding_span) => { let mut err = struct_span_err!(resolver.session, span, E0409, - "variable `{}` is bound with different mode in pattern #{} than in \ - pattern #1", - variable_name, - pattern_number); + "variable `{}` is bound in inconsistent \ + ways within the same match arm", + variable_name); err.span_label(span, &format!("bound in different ways")); err.span_label(first_binding_span, &format!("first binding")); err @@ -335,7 +363,7 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver, } } -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] struct BindingInfo { span: Span, binding_mode: BindingMode, @@ -1904,36 +1932,66 @@ impl<'a> Resolver<'a> { if arm.pats.is_empty() { return; } - let map_0 = self.binding_mode_map(&arm.pats[0]); + + let mut missing_vars = FxHashMap(); + let mut inconsistent_vars = FxHashMap(); for (i, p) in arm.pats.iter().enumerate() { let map_i = self.binding_mode_map(&p); - for (&key, &binding_0) in &map_0 { - match map_i.get(&key) { - None => { - let error = ResolutionError::VariableNotBoundInPattern(key.name, 1, i + 1); - resolve_error(self, p.span, error); + for (j, q) in arm.pats.iter().enumerate() { + if i == j { + continue; + } + + let map_j = self.binding_mode_map(&q); + for (&key, &binding_i) in &map_i { + if map_j.len() == 0 { // Account for missing bindings when + let binding_error = missing_vars // map_j has none. + .entry(key.name) + .or_insert(BindingError { + name: key.name, + origin: FxHashSet(), + target: FxHashSet(), + }); + binding_error.origin.insert(binding_i.span); + binding_error.target.insert(q.span); } - Some(binding_i) => { - if binding_0.binding_mode != binding_i.binding_mode { - resolve_error(self, - binding_i.span, - ResolutionError::VariableBoundWithDifferentMode( - key.name, - i + 1, - binding_0.span)); + for (&key_j, &binding_j) in &map_j { + match map_i.get(&key_j) { + None => { // missing binding + let binding_error = missing_vars + .entry(key_j.name) + .or_insert(BindingError { + name: key_j.name, + origin: FxHashSet(), + target: FxHashSet(), + }); + binding_error.origin.insert(binding_j.span); + binding_error.target.insert(p.span); + } + Some(binding_i) => { // check consistent binding + if binding_i.binding_mode != binding_j.binding_mode { + inconsistent_vars + .entry(key.name) + .or_insert((binding_j.span, binding_i.span)); + } + } } } } } - - for (&key, &binding) in &map_i { - if !map_0.contains_key(&key) { - resolve_error(self, - binding.span, - ResolutionError::VariableNotBoundInPattern(key.name, i + 1, 1)); - } - } + } + let mut missing_vars = missing_vars.iter().collect::>(); + missing_vars.sort(); + for (_, v) in missing_vars { + resolve_error(self, + *v.origin.iter().next().unwrap(), + ResolutionError::VariableNotBoundInPattern(v)); + } + let mut inconsistent_vars = inconsistent_vars.iter().collect::>(); + inconsistent_vars.sort(); + for (name, v) in inconsistent_vars { + resolve_error(self, v.0, ResolutionError::VariableBoundWithDifferentMode(*name, v.1)); } } diff --git a/src/test/compile-fail/E0408.rs b/src/test/compile-fail/E0408.rs index d75f612482772..ce77a537e263d 100644 --- a/src/test/compile-fail/E0408.rs +++ b/src/test/compile-fail/E0408.rs @@ -12,7 +12,8 @@ fn main() { let x = Some(0); match x { - Some(y) | None => {} //~ ERROR variable `y` from pattern #1 is not bound in pattern #2 + Some(y) | None => {} //~ ERROR variable `y` is not bound in all patterns _ => () //~| NOTE pattern doesn't bind `y` + //~| NOTE variable not in all patterns } } diff --git a/src/test/compile-fail/issue-2848.rs b/src/test/compile-fail/issue-2848.rs index f5e0c545bb524..38bd7adefd91f 100644 --- a/src/test/compile-fail/issue-2848.rs +++ b/src/test/compile-fail/issue-2848.rs @@ -19,7 +19,8 @@ mod bar { fn main() { use bar::foo::{alpha, charlie}; match alpha { - alpha | beta => {} //~ ERROR variable `beta` from pattern #2 is not bound in pattern #1 + alpha | beta => {} //~ ERROR variable `beta` is not bound in all patterns charlie => {} //~| NOTE pattern doesn't bind `beta` + //~| NOTE variable not in all patterns } } diff --git a/src/test/compile-fail/issue-2849.rs b/src/test/compile-fail/issue-2849.rs index 48f4cac9711a8..203b28bd5e417 100644 --- a/src/test/compile-fail/issue-2849.rs +++ b/src/test/compile-fail/issue-2849.rs @@ -13,6 +13,6 @@ enum foo { alpha, beta(isize) } fn main() { match foo::alpha { foo::alpha | foo::beta(i) => {} - //~^ ERROR variable `i` from pattern #2 is not bound in pattern #1 + //~^ ERROR variable `i` is not bound in all patterns } } diff --git a/src/test/compile-fail/resolve-inconsistent-binding-mode.rs b/src/test/compile-fail/resolve-inconsistent-binding-mode.rs index 284c08ef09b28..63d33a9e5fa6f 100644 --- a/src/test/compile-fail/resolve-inconsistent-binding-mode.rs +++ b/src/test/compile-fail/resolve-inconsistent-binding-mode.rs @@ -15,7 +15,7 @@ enum opts { fn matcher1(x: opts) { match x { opts::a(ref i) | opts::b(i) => {} - //~^ ERROR variable `i` is bound with different mode in pattern #2 than in pattern #1 + //~^ ERROR variable `i` is bound in inconsistent ways within the same match arm //~^^ ERROR mismatched types opts::c(_) => {} } @@ -24,7 +24,7 @@ fn matcher1(x: opts) { fn matcher2(x: opts) { match x { opts::a(ref i) | opts::b(i) => {} - //~^ ERROR variable `i` is bound with different mode in pattern #2 than in pattern #1 + //~^ ERROR variable `i` is bound in inconsistent ways within the same match arm //~^^ ERROR mismatched types opts::c(_) => {} } @@ -33,7 +33,7 @@ fn matcher2(x: opts) { fn matcher4(x: opts) { match x { opts::a(ref mut i) | opts::b(ref i) => {} - //~^ ERROR variable `i` is bound with different mode in pattern #2 than in pattern #1 + //~^ ERROR variable `i` is bound in inconsistent ways within the same match arm //~^^ ERROR mismatched types opts::c(_) => {} } diff --git a/src/test/compile-fail/resolve-inconsistent-names.rs b/src/test/compile-fail/resolve-inconsistent-names.rs index 1e2541502ace8..7fee5aedb06ed 100644 --- a/src/test/compile-fail/resolve-inconsistent-names.rs +++ b/src/test/compile-fail/resolve-inconsistent-names.rs @@ -11,9 +11,11 @@ fn main() { let y = 1; match y { - a | b => {} //~ ERROR variable `a` from pattern #1 is not bound in pattern #2 - //~^ ERROR variable `b` from pattern #2 is not bound in pattern #1 + a | b => {} //~ ERROR variable `a` is not bound in all patterns + //~^ ERROR variable `b` is not bound in all patterns //~| NOTE pattern doesn't bind `a` //~| NOTE pattern doesn't bind `b` + //~| NOTE variable not in all patterns + //~| NOTE variable not in all patterns } } diff --git a/src/test/ui/mismatched_types/E0409.stderr b/src/test/ui/mismatched_types/E0409.stderr index 251e247fa28ba..45a42b1c271f8 100644 --- a/src/test/ui/mismatched_types/E0409.stderr +++ b/src/test/ui/mismatched_types/E0409.stderr @@ -1,4 +1,4 @@ -error[E0409]: variable `y` is bound with different mode in pattern #2 than in pattern #1 +error[E0409]: variable `y` is bound in inconsistent ways within the same match arm --> $DIR/E0409.rs:15:23 | 15 | (0, ref y) | (y, 0) => {} //~ ERROR E0409 diff --git a/src/test/ui/span/issue-39698.rs b/src/test/ui/span/issue-39698.rs new file mode 100644 index 0000000000000..17b3f1c5a885e --- /dev/null +++ b/src/test/ui/span/issue-39698.rs @@ -0,0 +1,22 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +enum T { + T1(i32, i32), + T2(i32, i32), + T3(i32), + T4(i32), +} + +fn main() { + match T::T1(123, 456) { + T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } + } +} diff --git a/src/test/ui/span/issue-39698.stderr b/src/test/ui/span/issue-39698.stderr new file mode 100644 index 0000000000000..97d802f839831 --- /dev/null +++ b/src/test/ui/span/issue-39698.stderr @@ -0,0 +1,42 @@ +error[E0408]: variable `a` is not bound in all patterns + --> $DIR/issue-39698.rs:20:23 + | +20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } + | - ^^^^^^^^^^^ ^^^^^^^^ - variable not in all patterns + | | | | + | | | pattern doesn't bind `a` + | | pattern doesn't bind `a` + | variable not in all patterns + +error[E0408]: variable `d` is not bound in all patterns + --> $DIR/issue-39698.rs:20:37 + | +20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } + | - - ^^^^^^^^ ^^^^^^^^ pattern doesn't bind `d` + | | | | + | | | pattern doesn't bind `d` + | | variable not in all patterns + | variable not in all patterns + +error[E0408]: variable `b` is not bound in all patterns + --> $DIR/issue-39698.rs:20:9 + | +20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } + | ^^^^^^^^^^^ - ^^^^^^^^ ^^^^^^^^ pattern doesn't bind `b` + | | | | + | | | pattern doesn't bind `b` + | | variable not in all patterns + | pattern doesn't bind `b` + +error[E0408]: variable `c` is not bound in all patterns + --> $DIR/issue-39698.rs:20:9 + | +20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); } + | ^^^^^^^^^^^ ^^^^^^^^^^^ - ^^^^^^^^ pattern doesn't bind `c` + | | | | + | | | variable not in all patterns + | | pattern doesn't bind `c` + | pattern doesn't bind `c` + +error: aborting due to 4 previous errors + From 3ffa4b52404d9a6aff7390d44336f85166235ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 5 Mar 2017 20:19:05 -0300 Subject: [PATCH 29/29] Use `BTreeSet` instead of `FxHashSet` --- src/librustc_resolve/lib.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index adb0eee652bd0..d51ec268ec217 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -69,6 +69,7 @@ use errors::DiagnosticBuilder; use std::cell::{Cell, RefCell}; use std::cmp; +use std::collections::BTreeSet; use std::fmt; use std::mem::replace; use std::rc::Rc; @@ -100,8 +101,8 @@ enum AssocSuggestion { #[derive(Eq)] struct BindingError { name: Name, - origin: FxHashSet, - target: FxHashSet, + origin: BTreeSet, + target: BTreeSet, } impl PartialOrd for BindingError { @@ -233,16 +234,14 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver, err } ResolutionError::VariableNotBoundInPattern(binding_error) => { - let mut target_sp = binding_error.target.iter().map(|x| *x).collect::>(); - target_sp.sort(); + let target_sp = binding_error.target.iter().map(|x| *x).collect::>(); let msp = MultiSpan::from_spans(target_sp.clone()); let msg = format!("variable `{}` is not bound in all patterns", binding_error.name); let mut err = resolver.session.struct_span_err_with_code(msp, &msg, "E0408"); for sp in target_sp { err.span_label(sp, &format!("pattern doesn't bind `{}`", binding_error.name)); } - let mut origin_sp = binding_error.origin.iter().map(|x| *x).collect::>(); - origin_sp.sort(); + let origin_sp = binding_error.origin.iter().map(|x| *x).collect::>(); for sp in origin_sp { err.span_label(sp, &"variable not in all patterns"); } @@ -1950,8 +1949,8 @@ impl<'a> Resolver<'a> { .entry(key.name) .or_insert(BindingError { name: key.name, - origin: FxHashSet(), - target: FxHashSet(), + origin: BTreeSet::new(), + target: BTreeSet::new(), }); binding_error.origin.insert(binding_i.span); binding_error.target.insert(q.span); @@ -1963,8 +1962,8 @@ impl<'a> Resolver<'a> { .entry(key_j.name) .or_insert(BindingError { name: key_j.name, - origin: FxHashSet(), - target: FxHashSet(), + origin: BTreeSet::new(), + target: BTreeSet::new(), }); binding_error.origin.insert(binding_j.span); binding_error.target.insert(p.span);