From dcdf3d62c1bd30e896d54374f49ed80a1a1348e5 Mon Sep 17 00:00:00 2001 From: Masood Malekghassemi Date: Tue, 29 Mar 2016 20:06:42 -0700 Subject: [PATCH 01/17] Plumb obligations through librustc/infer --- src/librustc/infer/combine.rs | 4 +- src/librustc/infer/equate.rs | 5 ++ src/librustc/infer/glb.rs | 5 ++ src/librustc/infer/lub.rs | 5 ++ src/librustc/infer/mod.rs | 94 +++++++++++++-------- src/librustc/infer/sub.rs | 5 ++ src/librustc/traits/fulfill.rs | 8 +- src/librustc/traits/project.rs | 23 +++-- src/librustc/traits/select.rs | 83 ++++++++++-------- src/librustc_driver/test.rs | 25 ++++-- src/librustc_mir/transform/type_check.rs | 6 +- src/librustc_typeck/check/_match.rs | 9 +- src/librustc_typeck/check/coercion.rs | 34 +++++++- src/librustc_typeck/check/compare_method.rs | 7 +- src/librustc_typeck/check/demand.rs | 22 +++-- src/librustc_typeck/check/method/probe.rs | 5 +- src/librustc_typeck/check/mod.rs | 33 ++++++-- src/librustc_typeck/check/regionck.rs | 8 +- 18 files changed, 274 insertions(+), 107 deletions(-) diff --git a/src/librustc/infer/combine.rs b/src/librustc/infer/combine.rs index 2b30698882d62..6df8f076f32e8 100644 --- a/src/librustc/infer/combine.rs +++ b/src/librustc/infer/combine.rs @@ -37,7 +37,7 @@ use super::equate::Equate; use super::glb::Glb; use super::lub::Lub; use super::sub::Sub; -use super::{InferCtxt}; +use super::InferCtxt; use super::{MiscVariable, TypeTrace}; use super::type_variable::{RelationDir, BiTo, EqTo, SubtypeOf, SupertypeOf}; @@ -46,6 +46,7 @@ use ty::{self, Ty, TyCtxt}; use ty::error::TypeError; use ty::fold::{TypeFolder, TypeFoldable}; use ty::relate::{Relate, RelateResult, TypeRelation}; +use traits::PredicateObligations; use syntax::ast; use syntax::codemap::Span; @@ -56,6 +57,7 @@ pub struct CombineFields<'a, 'tcx: 'a> { pub a_is_expected: bool, pub trace: TypeTrace<'tcx>, pub cause: Option, + pub obligations: PredicateObligations<'tcx>, } pub fn super_combine_tys<'a,'tcx:'a,R>(infcx: &InferCtxt<'a, 'tcx>, diff --git a/src/librustc/infer/equate.rs b/src/librustc/infer/equate.rs index 3c9c9c5788414..5540046c9e363 100644 --- a/src/librustc/infer/equate.rs +++ b/src/librustc/infer/equate.rs @@ -16,6 +16,7 @@ use super::type_variable::{EqTo}; use ty::{self, Ty, TyCtxt}; use ty::TyVar; use ty::relate::{Relate, RelateResult, TypeRelation}; +use traits::PredicateObligations; /// Ensures `a` is made equal to `b`. Returns `a` on success. pub struct Equate<'a, 'tcx: 'a> { @@ -26,6 +27,10 @@ impl<'a, 'tcx> Equate<'a, 'tcx> { pub fn new(fields: CombineFields<'a, 'tcx>) -> Equate<'a, 'tcx> { Equate { fields: fields } } + + pub fn obligations(self) -> PredicateObligations<'tcx> { + self.fields.obligations + } } impl<'a, 'tcx> TypeRelation<'a,'tcx> for Equate<'a, 'tcx> { diff --git a/src/librustc/infer/glb.rs b/src/librustc/infer/glb.rs index 235428a6898a9..37717c2b6bc99 100644 --- a/src/librustc/infer/glb.rs +++ b/src/librustc/infer/glb.rs @@ -16,6 +16,7 @@ use super::Subtype; use ty::{self, Ty, TyCtxt}; use ty::relate::{Relate, RelateResult, TypeRelation}; +use traits::PredicateObligations; /// "Greatest lower bound" (common subtype) pub struct Glb<'a, 'tcx: 'a> { @@ -26,6 +27,10 @@ impl<'a, 'tcx> Glb<'a, 'tcx> { pub fn new(fields: CombineFields<'a, 'tcx>) -> Glb<'a, 'tcx> { Glb { fields: fields } } + + pub fn obligations(self) -> PredicateObligations<'tcx> { + self.fields.obligations + } } impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Glb<'a, 'tcx> { diff --git a/src/librustc/infer/lub.rs b/src/librustc/infer/lub.rs index 00b85929b4b15..32b2fe911e86d 100644 --- a/src/librustc/infer/lub.rs +++ b/src/librustc/infer/lub.rs @@ -16,6 +16,7 @@ use super::Subtype; use ty::{self, Ty, TyCtxt}; use ty::relate::{Relate, RelateResult, TypeRelation}; +use traits::PredicateObligations; /// "Least upper bound" (common supertype) pub struct Lub<'a, 'tcx: 'a> { @@ -26,6 +27,10 @@ impl<'a, 'tcx> Lub<'a, 'tcx> { pub fn new(fields: CombineFields<'a, 'tcx>) -> Lub<'a, 'tcx> { Lub { fields: fields } } + + pub fn obligations(self) -> PredicateObligations<'tcx> { + self.fields.obligations + } } impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Lub<'a, 'tcx> { diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index 725c6d9593cdd..6e97cdef3d7ca 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -27,13 +27,13 @@ use middle::region::CodeExtent; use ty::subst; use ty::subst::Substs; use ty::subst::Subst; -use traits::{self, ProjectionMode}; use ty::adjustment; use ty::{TyVid, IntVid, FloatVid}; use ty::{self, Ty, TyCtxt}; use ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric}; use ty::fold::{TypeFolder, TypeFoldable}; use ty::relate::{Relate, RelateResult, TypeRelation}; +use traits::{self, PredicateObligations, ProjectionMode}; use rustc_data_structures::unify::{self, UnificationTable}; use std::cell::{RefCell, Ref}; use std::fmt; @@ -63,6 +63,12 @@ pub mod sub; pub mod type_variable; pub mod unify_key; +pub struct InferOk<'tcx, T> { + pub value: T, + pub obligations: PredicateObligations<'tcx>, +} +pub type InferResult<'tcx, T> = Result, TypeError<'tcx>>; + pub type Bound = Option; pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result" pub type FixupResult = Result; // "fixup result" @@ -391,16 +397,15 @@ pub fn mk_subty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>, origin: TypeOrigin, a: Ty<'tcx>, b: Ty<'tcx>) - -> UnitResult<'tcx> + -> InferResult<'tcx, ()> { debug!("mk_subty({:?} <: {:?})", a, b); cx.sub_types(a_is_expected, origin, a, b) } -pub fn can_mk_subty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>, - a: Ty<'tcx>, - b: Ty<'tcx>) - -> UnitResult<'tcx> { +pub fn can_mk_subty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) + -> UnitResult<'tcx> +{ debug!("can_mk_subty({:?} <: {:?})", a, b); cx.probe(|_| { let trace = TypeTrace { @@ -412,7 +417,7 @@ pub fn can_mk_subty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>, } pub fn can_mk_eqty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) - -> UnitResult<'tcx> + -> UnitResult<'tcx> { cx.can_equate(&a, &b) } @@ -432,7 +437,7 @@ pub fn mk_eqty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>, origin: TypeOrigin, a: Ty<'tcx>, b: Ty<'tcx>) - -> UnitResult<'tcx> + -> InferResult<'tcx, ()> { debug!("mk_eqty({:?} <: {:?})", a, b); cx.eq_types(a_is_expected, origin, a, b) @@ -443,7 +448,7 @@ pub fn mk_eq_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>, origin: TypeOrigin, a: ty::TraitRef<'tcx>, b: ty::TraitRef<'tcx>) - -> UnitResult<'tcx> + -> InferResult<'tcx, ()> { debug!("mk_eq_trait_refs({:?} = {:?})", a, b); cx.eq_trait_refs(a_is_expected, origin, a, b) @@ -454,7 +459,7 @@ pub fn mk_sub_poly_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>, origin: TypeOrigin, a: ty::PolyTraitRef<'tcx>, b: ty::PolyTraitRef<'tcx>) - -> UnitResult<'tcx> + -> InferResult<'tcx, ()> { debug!("mk_sub_poly_trait_refs({:?} <: {:?})", a, b); cx.sub_poly_trait_refs(a_is_expected, origin, a, b) @@ -465,7 +470,7 @@ pub fn mk_eq_impl_headers<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>, origin: TypeOrigin, a: &ty::ImplHeader<'tcx>, b: &ty::ImplHeader<'tcx>) - -> UnitResult<'tcx> + -> InferResult<'tcx, ()> { debug!("mk_eq_impl_header({:?} = {:?})", a, b); match (a.trait_ref, b.trait_ref) { @@ -661,39 +666,51 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { } fn combine_fields(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>) - -> CombineFields<'a, 'tcx> { - CombineFields {infcx: self, - a_is_expected: a_is_expected, - trace: trace, - cause: None} + -> CombineFields<'a, 'tcx> + { + CombineFields { + infcx: self, + a_is_expected: a_is_expected, + trace: trace, + cause: None, + obligations: PredicateObligations::new(), + } } pub fn equate(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T) - -> RelateResult<'tcx, T> + -> InferResult<'tcx, T> where T: Relate<'a, 'tcx> { - self.combine_fields(a_is_expected, trace).equate().relate(a, b) + let mut equate = self.combine_fields(a_is_expected, trace).equate(); + let result = equate.relate(a, b); + result.map(|t| InferOk { value: t, obligations: equate.obligations() }) } pub fn sub(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T) - -> RelateResult<'tcx, T> + -> InferResult<'tcx, T> where T: Relate<'a, 'tcx> { - self.combine_fields(a_is_expected, trace).sub().relate(a, b) + let mut sub = self.combine_fields(a_is_expected, trace).sub(); + let result = sub.relate(a, b); + result.map(|t| InferOk { value: t, obligations: sub.obligations() }) } pub fn lub(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T) - -> RelateResult<'tcx, T> + -> InferResult<'tcx, T> where T: Relate<'a, 'tcx> { - self.combine_fields(a_is_expected, trace).lub().relate(a, b) + let mut lub = self.combine_fields(a_is_expected, trace).lub(); + let result = lub.relate(a, b); + result.map(|t| InferOk { value: t, obligations: lub.obligations() }) } pub fn glb(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T) - -> RelateResult<'tcx, T> + -> InferResult<'tcx, T> where T: Relate<'a, 'tcx> { - self.combine_fields(a_is_expected, trace).glb().relate(a, b) + let mut glb = self.combine_fields(a_is_expected, trace).glb(); + let result = glb.relate(a, b); + result.map(|t| InferOk { value: t, obligations: glb.obligations() }) } fn start_snapshot(&self) -> CombinedSnapshot { @@ -829,12 +846,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { origin: TypeOrigin, a: Ty<'tcx>, b: Ty<'tcx>) - -> UnitResult<'tcx> + -> InferResult<'tcx, ()> { debug!("sub_types({:?} <: {:?})", a, b); self.commit_if_ok(|_| { let trace = TypeTrace::types(origin, a_is_expected, a, b); - self.sub(a_is_expected, trace, &a, &b).map(|_| ()) + self.sub(a_is_expected, trace, &a, &b) + .map(|InferOk { obligations, .. }| InferOk { value: (), obligations: obligations }) }) } @@ -843,11 +861,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { origin: TypeOrigin, a: Ty<'tcx>, b: Ty<'tcx>) - -> UnitResult<'tcx> + -> InferResult<'tcx, ()> { self.commit_if_ok(|_| { let trace = TypeTrace::types(origin, a_is_expected, a, b); - self.equate(a_is_expected, trace, &a, &b).map(|_| ()) + self.equate(a_is_expected, trace, &a, &b) + .map(|InferOk { obligations, .. }| InferOk { value: (), obligations: obligations }) }) } @@ -856,7 +875,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { origin: TypeOrigin, a: ty::TraitRef<'tcx>, b: ty::TraitRef<'tcx>) - -> UnitResult<'tcx> + -> InferResult<'tcx, ()> { debug!("eq_trait_refs({:?} <: {:?})", a, @@ -866,7 +885,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { origin: origin, values: TraitRefs(expected_found(a_is_expected, a.clone(), b.clone())) }; - self.equate(a_is_expected, trace, &a, &b).map(|_| ()) + self.equate(a_is_expected, trace, &a, &b) + .map(|InferOk { obligations, .. }| InferOk { value: (), obligations: obligations }) }) } @@ -875,7 +895,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { origin: TypeOrigin, a: ty::PolyTraitRef<'tcx>, b: ty::PolyTraitRef<'tcx>) - -> UnitResult<'tcx> + -> InferResult<'tcx, ()> { debug!("sub_poly_trait_refs({:?} <: {:?})", a, @@ -885,7 +905,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { origin: origin, values: PolyTraitRefs(expected_found(a_is_expected, a.clone(), b.clone())) }; - self.sub(a_is_expected, trace, &a, &b).map(|_| ()) + self.sub(a_is_expected, trace, &a, &b) + .map(|InferOk { obligations, .. }| InferOk { value: (), obligations: obligations }) }) } @@ -928,20 +949,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { pub fn equality_predicate(&self, span: Span, predicate: &ty::PolyEquatePredicate<'tcx>) - -> UnitResult<'tcx> { + -> InferResult<'tcx, ()> + { self.commit_if_ok(|snapshot| { let (ty::EquatePredicate(a, b), skol_map) = self.skolemize_late_bound_regions(predicate, snapshot); let origin = TypeOrigin::EquatePredicate(span); - let () = mk_eqty(self, false, origin, a, b)?; + let InferOk { obligations, .. } = mk_eqty(self, false, origin, a, b)?; self.leak_check(&skol_map, snapshot) + .map(|_| InferOk { value: (), obligations: obligations }) }) } pub fn region_outlives_predicate(&self, span: Span, predicate: &ty::PolyRegionOutlivesPredicate) - -> UnitResult<'tcx> { + -> UnitResult<'tcx> + { self.commit_if_ok(|snapshot| { let (ty::OutlivesPredicate(r_a, r_b), skol_map) = self.skolemize_late_bound_regions(predicate, snapshot); diff --git a/src/librustc/infer/sub.rs b/src/librustc/infer/sub.rs index 0505c9d627b9b..ece8c0c696af8 100644 --- a/src/librustc/infer/sub.rs +++ b/src/librustc/infer/sub.rs @@ -16,6 +16,7 @@ use super::type_variable::{SubtypeOf, SupertypeOf}; use ty::{self, Ty, TyCtxt}; use ty::TyVar; use ty::relate::{Cause, Relate, RelateResult, TypeRelation}; +use traits::PredicateObligations; use std::mem; /// Ensures `a` is made a subtype of `b`. Returns `a` on success. @@ -27,6 +28,10 @@ impl<'a, 'tcx> Sub<'a, 'tcx> { pub fn new(f: CombineFields<'a, 'tcx>) -> Sub<'a, 'tcx> { Sub { fields: f } } + + pub fn obligations(self) -> PredicateObligations<'tcx> { + self.fields.obligations + } } impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Sub<'a, 'tcx> { diff --git a/src/librustc/traits/fulfill.rs b/src/librustc/traits/fulfill.rs index 321144126c9f8..68173bc9ea513 100644 --- a/src/librustc/traits/fulfill.rs +++ b/src/librustc/traits/fulfill.rs @@ -9,7 +9,7 @@ // except according to those terms. use dep_graph::DepGraph; -use infer::InferCtxt; +use infer::{InferCtxt, InferOk}; use ty::{self, Ty, TyCtxt, TypeFoldable, ToPolyTraitRef}; use rustc_data_structures::obligation_forest::{Backtrace, ObligationForest, Error}; use std::iter; @@ -526,7 +526,11 @@ fn process_predicate1<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>, ty::Predicate::Equate(ref binder) => { match selcx.infcx().equality_predicate(obligation.cause.span, binder) { - Ok(()) => Ok(Some(Vec::new())), + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + Ok(Some(Vec::new())) + }, Err(_) => Err(CodeSelectionError(Unimplemented)), } } diff --git a/src/librustc/traits/project.rs b/src/librustc/traits/project.rs index 3adaf5fa6bb2f..85fe457c75e8c 100644 --- a/src/librustc/traits/project.rs +++ b/src/librustc/traits/project.rs @@ -24,7 +24,7 @@ use super::VtableImplData; use super::util; use middle::def_id::DefId; -use infer::{self, TypeOrigin}; +use infer::{self, InferOk, TypeOrigin}; use ty::subst::Subst; use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt}; use ty::fold::{TypeFoldable, TypeFolder}; @@ -232,7 +232,11 @@ fn project_and_unify_type<'cx,'tcx>( let infcx = selcx.infcx(); let origin = TypeOrigin::RelateOutputImplTypes(obligation.cause.span); match infer::mk_eqty(infcx, true, origin, normalized_ty, obligation.predicate.ty) { - Ok(()) => Ok(Some(obligations)), + Ok(InferOk { obligations: inferred_obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(inferred_obligations.is_empty()); + Ok(Some(obligations)) + }, Err(err) => Err(MismatchedProjectionTypes { err: err }), } } @@ -278,7 +282,10 @@ fn consider_unification_despite_ambiguity<'cx,'tcx>(selcx: &mut SelectionContext let origin = TypeOrigin::RelateOutputImplTypes(obligation.cause.span); let obligation_ty = obligation.predicate.ty; match infer::mk_eqty(infcx, true, origin, obligation_ty, ret_type) { - Ok(()) => { } + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + } Err(_) => { /* ignore errors */ } } } @@ -829,7 +836,10 @@ fn assemble_candidates_from_predicates<'cx,'tcx,I>( infcx.sub_poly_trait_refs(false, origin, data_poly_trait_ref, - obligation_poly_trait_ref).is_ok() + obligation_poly_trait_ref) + // FIXME(#????) propagate obligations + .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) + .is_ok() }); debug!("assemble_candidates_from_predicates: candidate={:?} \ @@ -1082,7 +1092,10 @@ fn confirm_param_env_candidate<'cx,'tcx>( origin, obligation.predicate.trait_ref.clone(), projection.projection_ty.trait_ref.clone()) { - Ok(()) => { } + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + } Err(e) => { selcx.tcx().sess.span_bug( obligation.cause.span, diff --git a/src/librustc/traits/select.rs b/src/librustc/traits/select.rs index e2a48688d4bbc..013c75bf8d2bb 100644 --- a/src/librustc/traits/select.rs +++ b/src/librustc/traits/select.rs @@ -38,7 +38,7 @@ use super::util; use middle::def_id::DefId; use infer; -use infer::{InferCtxt, TypeFreshener, TypeOrigin}; +use infer::{InferCtxt, InferOk, TypeFreshener, TypeOrigin}; use ty::subst::{Subst, Substs, TypeSpace}; use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable}; use traits; @@ -484,7 +484,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ty::Predicate::Equate(ref p) => { // does this code ever run? match self.infcx.equality_predicate(obligation.cause.span, p) { - Ok(()) => EvaluatedToOk, + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + EvaluatedToOk + }, Err(_) => EvaluatedToErr } } @@ -1185,7 +1189,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { origin, trait_bound.clone(), ty::Binder(skol_trait_ref.clone())) { - Ok(()) => { } + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + } Err(_) => { return false; } } @@ -2494,13 +2501,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let origin = TypeOrigin::RelateOutputImplTypes(obligation_cause.span); let obligation_trait_ref = obligation_trait_ref.clone(); - match self.infcx.sub_poly_trait_refs(false, - origin, - expected_trait_ref.clone(), - obligation_trait_ref.clone()) { - Ok(()) => Ok(()), - Err(e) => Err(OutputTypeParameterMismatch(expected_trait_ref, obligation_trait_ref, e)) - } + self.infcx.sub_poly_trait_refs(false, + origin, + expected_trait_ref.clone(), + obligation_trait_ref.clone()) + // FIXME(#????) propagate obligations + .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) + .map_err(|e| OutputTypeParameterMismatch(expected_trait_ref, obligation_trait_ref, e)) } fn confirm_builtin_unsize_candidate(&mut self, @@ -2531,9 +2538,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let new_trait = tcx.mk_trait(data_a.principal.clone(), bounds); let origin = TypeOrigin::Misc(obligation.cause.span); - if self.infcx.sub_types(false, origin, new_trait, target).is_err() { - return Err(Unimplemented); - } + let InferOk { obligations, .. } = + self.infcx.sub_types(false, origin, new_trait, target) + .map_err(|_| Unimplemented)?; + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); // Register one obligation for 'a: 'b. let cause = ObligationCause::new(obligation.cause.span, @@ -2596,9 +2605,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // [T; n] -> [T]. (&ty::TyArray(a, _), &ty::TySlice(b)) => { let origin = TypeOrigin::Misc(obligation.cause.span); - if self.infcx.sub_types(false, origin, a, b).is_err() { - return Err(Unimplemented); - } + let InferOk { obligations, .. } = + self.infcx.sub_types(false, origin, a, b) + .map_err(|_| Unimplemented)?; + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); } // Struct -> Struct. @@ -2654,9 +2665,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } let new_struct = tcx.mk_struct(def, tcx.mk_substs(new_substs)); let origin = TypeOrigin::Misc(obligation.cause.span); - if self.infcx.sub_types(false, origin, new_struct, target).is_err() { - return Err(Unimplemented); - } + let InferOk { obligations, .. } = + self.infcx.sub_types(false, origin, new_struct, target) + .map_err(|_| Unimplemented)?; + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); // Construct the nested Field: Unsize> predicate. nested.push(util::predicate_for_trait_def(tcx, @@ -2742,13 +2755,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { skol_obligation_trait_ref); let origin = TypeOrigin::RelateOutputImplTypes(obligation.cause.span); - if let Err(e) = self.infcx.eq_trait_refs(false, - origin, - impl_trait_ref.value.clone(), - skol_obligation_trait_ref) { - debug!("match_impl: failed eq_trait_refs due to `{}`", e); - return Err(()); - } + let InferOk { obligations, .. } = + self.infcx.eq_trait_refs(false, + origin, + impl_trait_ref.value.clone(), + skol_obligation_trait_ref) + .map_err(|e| { + debug!("match_impl: failed eq_trait_refs due to `{}`", e); + () + })?; + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); if let Err(e) = self.infcx.leak_check(&skol_map, snapshot) { debug!("match_impl: failed leak check due to `{}`", e); @@ -2811,13 +2828,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { poly_trait_ref); let origin = TypeOrigin::RelateOutputImplTypes(obligation.cause.span); - match self.infcx.sub_poly_trait_refs(false, - origin, - poly_trait_ref, - obligation.predicate.to_poly_trait_ref()) { - Ok(()) => Ok(()), - Err(_) => Err(()), - } + self.infcx.sub_poly_trait_refs(false, + origin, + poly_trait_ref, + obligation.predicate.to_poly_trait_ref()) + // FIXME(#????) propagate obligations + .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) + .map_err(|_| ()) } /////////////////////////////////////////////////////////////////////////// diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index 6ce623a3b2892..00569d50cbb9c 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -24,8 +24,8 @@ use rustc::ty::subst; use rustc::ty::subst::Subst; use rustc::traits::ProjectionMode; use rustc::ty::{self, Ty, TyCtxt, TypeFoldable}; -use rustc::ty::relate::{TypeRelation, RelateResult}; -use rustc::infer::{self, TypeOrigin}; +use rustc::ty::relate::TypeRelation; +use rustc::infer::{self, InferOk, InferResult, TypeOrigin}; use rustc_metadata::cstore::CStore; use rustc::front::map as hir_map; use rustc::session::{self, config}; @@ -355,17 +355,17 @@ impl<'a, 'tcx> Env<'a, 'tcx> { infer::TypeTrace::dummy(self.tcx()) } - pub fn sub(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { + pub fn sub(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> { let trace = self.dummy_type_trace(); self.infcx.sub(true, trace, t1, t2) } - pub fn lub(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { + pub fn lub(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> { let trace = self.dummy_type_trace(); self.infcx.lub(true, trace, t1, t2) } - pub fn glb(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { + pub fn glb(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> { let trace = self.dummy_type_trace(); self.infcx.glb(true, trace, t1, t2) } @@ -374,7 +374,10 @@ impl<'a, 'tcx> Env<'a, 'tcx> { /// region checks). pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) { match self.sub(&t1, &t2) { - Ok(_) => {} + Ok(InferOk { obligations, .. }) => { + // FIXME once obligations are being propagated, assert the right thing. + assert!(obligations.is_empty()); + } Err(ref e) => { panic!("unexpected error computing sub({:?},{:?}): {}", t1, t2, e); } @@ -395,7 +398,10 @@ impl<'a, 'tcx> Env<'a, 'tcx> { /// Checks that `LUB(t1,t2) == t_lub` pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) { match self.lub(&t1, &t2) { - Ok(t) => { + Ok(InferOk { obligations, value: t }) => { + // FIXME once obligations are being propagated, assert the right thing. + assert!(obligations.is_empty()); + self.assert_eq(t, t_lub); } Err(ref e) => { @@ -411,7 +417,10 @@ impl<'a, 'tcx> Env<'a, 'tcx> { Err(e) => { panic!("unexpected error computing LUB: {:?}", e) } - Ok(t) => { + Ok(InferOk { obligations, value: t }) => { + // FIXME once obligations are being propagated, assert the right thing. + assert!(obligations.is_empty()); + self.assert_eq(t, t_glb); // sanity check for good measure: diff --git a/src/librustc_mir/transform/type_check.rs b/src/librustc_mir/transform/type_check.rs index 6cfde27ac97b6..3f85a3e1d4618 100644 --- a/src/librustc_mir/transform/type_check.rs +++ b/src/librustc_mir/transform/type_check.rs @@ -12,7 +12,7 @@ #![allow(unreachable_code)] use rustc::dep_graph::DepNode; -use rustc::infer::{self, InferCtxt}; +use rustc::infer::{self, InferCtxt, InferOk}; use rustc::traits::{self, ProjectionMode}; use rustc::ty::fold::TypeFoldable; use rustc::ty::{self, Ty, TyCtxt}; @@ -338,6 +338,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { { infer::mk_subty(self.infcx, false, infer::TypeOrigin::Misc(span), sup, sub) + // FIXME(#????) propagate obligations + .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } fn mk_eqty(&self, span: Span, a: Ty<'tcx>, b: Ty<'tcx>) @@ -345,6 +347,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { { infer::mk_eqty(self.infcx, false, infer::TypeOrigin::Misc(span), a, b) + // FIXME(#????) propagate obligations + .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } fn tcx(&self) -> &'a TyCtxt<'tcx> { diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index f8fa9c8e9b6cb..dcbfa2bb79b6c 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -9,7 +9,7 @@ // except according to those terms. use middle::def::{self, Def}; -use rustc::infer::{self, TypeOrigin}; +use rustc::infer::{self, InferOk, TypeOrigin}; use middle::pat_util::{PatIdMap, pat_id_map, pat_is_binding}; use middle::pat_util::pat_is_resolved_const; use rustc::ty::subst::Substs; @@ -532,7 +532,12 @@ pub fn check_match<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, }; let result = if is_if_let_fallback { - fcx.infcx().eq_types(true, origin, arm_ty, result_ty).map(|_| arm_ty) + fcx.infcx().eq_types(true, origin, arm_ty, result_ty) + .map(|InferOk { obligations, .. }| { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + arm_ty + }) } else if i == 0 { // Special-case the first arm, as it has no "previous expressions". coercion::try(fcx, &arm.body, coerce_first) diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index e36da1a568a7d..a4d8e2ae04938 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -62,7 +62,7 @@ use check::{autoderef, FnCtxt, UnresolvedTypeAction}; -use rustc::infer::{Coercion, TypeOrigin, TypeTrace}; +use rustc::infer::{Coercion, InferOk, TypeOrigin, TypeTrace}; use rustc::traits::{self, ObligationCause}; use rustc::traits::{predicate_for_trait_def, report_selection_error}; use rustc::ty::adjustment::{AutoAdjustment, AutoDerefRef, AdjustDerefRef}; @@ -118,8 +118,18 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { let trace = TypeTrace::types(self.origin, false, a, b); if self.use_lub { infcx.lub(false, trace, &a, &b) + .map(|InferOk { value, obligations }| { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + value + }) } else { infcx.sub(false, trace, &a, &b) + .map(|InferOk { value, obligations }| { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + value + }) } }) } @@ -656,12 +666,22 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>, (&ty::TyFnDef(a_def_id, a_substs, a_fty), &ty::TyFnDef(b_def_id, b_substs, b_fty)) => { // The signature must always match. - let fty = fcx.infcx().lub(true, trace.clone(), a_fty, b_fty)?; + let fty = fcx.infcx().lub(true, trace.clone(), a_fty, b_fty) + .map(|InferOk { value, obligations }| { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + value + })?; if a_def_id == b_def_id { // Same function, maybe the parameters match. let substs = fcx.infcx().commit_if_ok(|_| { fcx.infcx().lub(true, trace.clone(), a_substs, b_substs) + .map(|InferOk { value, obligations }| { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + value + }) }).map(|s| fcx.tcx().mk_substs(s)); if let Ok(substs) = substs { @@ -725,6 +745,11 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>, if !noop { return fcx.infcx().commit_if_ok(|_| { fcx.infcx().lub(true, trace.clone(), &prev_ty, &new_ty) + .map(|InferOk { value, obligations }| { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + value + }) }); } } @@ -737,6 +762,11 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>, } else { fcx.infcx().commit_if_ok(|_| { fcx.infcx().lub(true, trace, &prev_ty, &new_ty) + .map(|InferOk { value, obligations }| { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + value + }) }) } } diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs index 6d429fa7b73a1..5f49b0335d929 100644 --- a/src/librustc_typeck/check/compare_method.rs +++ b/src/librustc_typeck/check/compare_method.rs @@ -9,7 +9,7 @@ // except according to those terms. use middle::free_region::FreeRegionMap; -use rustc::infer::{self, TypeOrigin}; +use rustc::infer::{self, InferOk, TypeOrigin}; use rustc::ty::{self, TyCtxt}; use rustc::traits::{self, ProjectionMode}; use rustc::ty::subst::{self, Subst, Substs, VecPerParamSpace}; @@ -475,7 +475,10 @@ pub fn compare_const_impl<'tcx>(tcx: &TyCtxt<'tcx>, }); match err { - Ok(()) => { } + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()) + } Err(terr) => { debug!("checking associated const for compatibility: impl ty {:?}, trait ty {:?}", impl_ty, diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index d8bdf6c61aad4..75f6b11a22931 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -11,7 +11,7 @@ use check::{coercion, FnCtxt}; use rustc::ty::Ty; -use rustc::infer::TypeOrigin; +use rustc::infer::{InferOk, TypeOrigin}; use syntax::codemap::Span; use rustc_front::hir; @@ -21,16 +21,28 @@ use rustc_front::hir; pub fn suptype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) { let origin = TypeOrigin::Misc(sp); - if let Err(e) = fcx.infcx().sub_types(false, origin, actual, expected) { - fcx.infcx().report_mismatched_types(origin, expected, actual, e); + match fcx.infcx().sub_types(false, origin, actual, expected) { + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + }, + Err(e) => { + fcx.infcx().report_mismatched_types(origin, expected, actual, e); + } } } pub fn eqtype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) { let origin = TypeOrigin::Misc(sp); - if let Err(e) = fcx.infcx().eq_types(false, origin, actual, expected) { - fcx.infcx().report_mismatched_types(origin, expected, actual, e); + match fcx.infcx().eq_types(false, origin, actual, expected) { + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + }, + Err(e) => { + fcx.infcx().report_mismatched_types(origin, expected, actual, e); + } } } diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 4b42846297b16..44dbcd051e201 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -20,8 +20,7 @@ use rustc::ty::subst; use rustc::ty::subst::Subst; use rustc::traits; use rustc::ty::{self, NoPreference, Ty, TyCtxt, ToPolyTraitRef, TraitRef, TypeFoldable}; -use rustc::infer; -use rustc::infer::{InferCtxt, TypeOrigin}; +use rustc::infer::{self, InferCtxt, InferOk, TypeOrigin}; use syntax::ast; use syntax::codemap::{Span, DUMMY_SP}; use rustc_front::hir; @@ -1135,6 +1134,8 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> { fn make_sub_ty(&self, sub: Ty<'tcx>, sup: Ty<'tcx>) -> infer::UnitResult<'tcx> { self.infcx().sub_types(false, TypeOrigin::Misc(DUMMY_SP), sub, sup) + // FIXME(#????) propagate obligations + .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } fn has_applicable_self(&self, item: &ty::ImplOrTraitItem) -> bool { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 107497a2aa367..625e59c6e3c63 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -88,8 +88,7 @@ use middle::astconv_util::prohibit_type_params; use middle::cstore::LOCAL_CRATE; use middle::def::{self, Def}; use middle::def_id::DefId; -use rustc::infer; -use rustc::infer::{TypeOrigin, TypeTrace, type_variable}; +use rustc::infer::{self, InferOk, TypeOrigin, TypeTrace, type_variable}; use middle::pat_util::{self, pat_id_map}; use rustc::ty::subst::{self, Subst, Substs, VecPerParamSpace, ParamSpace}; use rustc::traits::{self, report_fulfillment_errors, ProjectionMode}; @@ -1629,6 +1628,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { sup: Ty<'tcx>) -> Result<(), TypeError<'tcx>> { infer::mk_subty(self.infcx(), a_is_expected, origin, sub, sup) + // FIXME(#????) propagate obligations + .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } pub fn mk_eqty(&self, @@ -1638,6 +1639,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { sup: Ty<'tcx>) -> Result<(), TypeError<'tcx>> { infer::mk_eqty(self.infcx(), a_is_expected, origin, sub, sup) + // FIXME(#????) propagate obligations + .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } pub fn mk_subr(&self, @@ -1916,7 +1919,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { match infer::mk_eqty(self.infcx(), false, TypeOrigin::Misc(default.origin_span), ty, default.ty) { - Ok(()) => {} + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()) + }, Err(_) => { conflicts.push((*ty, default)); } @@ -2009,7 +2015,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { match infer::mk_eqty(self.infcx(), false, TypeOrigin::Misc(default.origin_span), ty, default.ty) { - Ok(()) => {} + // FIXME(#????) propagate obligations + Ok(InferOk { obligations, .. }) => assert!(obligations.is_empty()), Err(_) => { result = Some(default); } @@ -2776,8 +2783,10 @@ fn expected_types_for_fn_args<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, let ures = fcx.infcx().sub_types(false, origin, formal_ret_ty, ret_ty); // FIXME(#15760) can't use try! here, FromError doesn't default // to identity so the resulting type is not constrained. - if let Err(e) = ures { - return Err(e); + match ures { + // FIXME(#????) propagate obligations + Ok(InferOk { obligations, .. }) => assert!(obligations.is_empty()), + Err(e) => return Err(e), } // Record all the argument types, with the substitutions @@ -2905,13 +2914,23 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, fcx.infcx().commit_if_ok(|_| { let trace = TypeTrace::types(origin, true, then_ty, else_ty); fcx.infcx().lub(true, trace, &then_ty, &else_ty) + .map(|InferOk { value, obligations }| { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + value + }) }) }; (origin, then_ty, else_ty, result) } else { let origin = TypeOrigin::IfExpressionWithNoElse(sp); (origin, unit, then_ty, - fcx.infcx().eq_types(true, origin, unit, then_ty).map(|_| unit)) + fcx.infcx().eq_types(true, origin, unit, then_ty) + .map(|InferOk { obligations, .. }| { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + unit + })) }; let if_ty = match result { diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index 533d24686e71f..515a898699e0c 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -92,7 +92,7 @@ use middle::region::{self, CodeExtent}; use rustc::ty::subst::Substs; use rustc::traits; use rustc::ty::{self, Ty, TyCtxt, MethodCall, TypeFoldable}; -use rustc::infer::{self, GenericKind, InferCtxt, SubregionOrigin, TypeOrigin, VerifyBound}; +use rustc::infer::{self, GenericKind, InferCtxt, InferOk, SubregionOrigin, TypeOrigin, VerifyBound}; use middle::pat_util; use rustc::ty::adjustment; use rustc::ty::wf::ImpliedBound; @@ -1846,7 +1846,11 @@ fn declared_projection_bounds_from_trait<'a,'tcx>(rcx: &Rcx<'a, 'tcx>, // check whether this predicate applies to our current projection match infer::mk_eqty(infcx, false, TypeOrigin::Misc(span), ty, outlives.0) { - Ok(()) => { Ok(outlives.1) } + Ok(InferOk { obligations, .. }) => { + // FIXME(#????) propagate obligations + assert!(obligations.is_empty()); + Ok(outlives.1) + } Err(_) => { Err(()) } } }); From b1543a1aac0c9443e509e00ed6cddcc9b9400ac3 Mon Sep 17 00:00:00 2001 From: mitaa Date: Thu, 31 Mar 2016 18:15:54 +0200 Subject: [PATCH 02/17] Make the rendering process less pass-aware Instead of hardcoding knowledge about the strip-private pass into the rendering process we represent (some) stripped items as `ItemEnum::StrippedItem`. Rustdoc will, for example, generate redirect pages for public items contained in private modules which have been re-exported to somewhere externally reachable - this will now not only work for the `strip-private` pass, but for other passes as well, such as the `strip-hidden` pass. --- src/librustdoc/clean/mod.rs | 29 +++-- src/librustdoc/fold.rs | 56 ++++++-- src/librustdoc/html/item_type.rs | 8 +- src/librustdoc/html/render.rs | 184 ++++++++++++--------------- src/librustdoc/passes.rs | 40 +++--- src/librustdoc/test.rs | 4 +- src/test/auxiliary/reexp_stripped.rs | 21 +++ src/test/rustdoc/redirect.rs | 48 +++++++ 8 files changed, 244 insertions(+), 146 deletions(-) create mode 100644 src/test/auxiliary/reexp_stripped.rs create mode 100644 src/test/rustdoc/redirect.rs diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index b26e56008accb..9c60b90a1fcf5 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -53,6 +53,7 @@ use std::env::current_dir; use core::DocContext; use doctree; use visit_ast; +use html::item_type::ItemType; /// A stable identifier to the particular version of JSON output. /// Increment this when the `Crate` and related structures change. @@ -273,36 +274,40 @@ impl Item { } pub fn is_crate(&self) -> bool { match self.inner { - ModuleItem(Module { items: _, is_crate: true }) => true, - _ => false + StrippedItem(box ModuleItem(Module { is_crate: true, ..})) | + ModuleItem(Module { is_crate: true, ..}) => true, + _ => false, } } pub fn is_mod(&self) -> bool { - match self.inner { ModuleItem(..) => true, _ => false } + ItemType::from_item(self) == ItemType::Module } pub fn is_trait(&self) -> bool { - match self.inner { TraitItem(..) => true, _ => false } + ItemType::from_item(self) == ItemType::Trait } pub fn is_struct(&self) -> bool { - match self.inner { StructItem(..) => true, _ => false } + ItemType::from_item(self) == ItemType::Struct } pub fn is_enum(&self) -> bool { - match self.inner { EnumItem(..) => true, _ => false } + ItemType::from_item(self) == ItemType::Module } pub fn is_fn(&self) -> bool { - match self.inner { FunctionItem(..) => true, _ => false } + ItemType::from_item(self) == ItemType::Function } pub fn is_associated_type(&self) -> bool { - match self.inner { AssociatedTypeItem(..) => true, _ => false } + ItemType::from_item(self) == ItemType::AssociatedType } pub fn is_associated_const(&self) -> bool { - match self.inner { AssociatedConstItem(..) => true, _ => false } + ItemType::from_item(self) == ItemType::AssociatedConst } pub fn is_method(&self) -> bool { - match self.inner { MethodItem(..) => true, _ => false } + ItemType::from_item(self) == ItemType::Method } pub fn is_ty_method(&self) -> bool { - match self.inner { TyMethodItem(..) => true, _ => false } + ItemType::from_item(self) == ItemType::TyMethod + } + pub fn is_stripped(&self) -> bool { + match self.inner { StrippedItem(..) => true, _ => false } } pub fn stability_class(&self) -> String { @@ -352,6 +357,8 @@ pub enum ItemEnum { AssociatedConstItem(Type, Option), AssociatedTypeItem(Vec, Option), DefaultImplItem(DefaultImpl), + /// An item that has been stripped by a rustdoc pass + StrippedItem(Box), } #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] diff --git a/src/librustdoc/fold.rs b/src/librustdoc/fold.rs index ceec80402c01e..5595c749256df 100644 --- a/src/librustdoc/fold.rs +++ b/src/librustdoc/fold.rs @@ -10,28 +10,50 @@ use clean::*; +pub enum FoldItem { + Retain(Item), + Strip(Item), + Erase, +} + +impl FoldItem { + pub fn fold(self) -> Option { + match self { + FoldItem::Erase => None, + FoldItem::Retain(i) => Some(i), + FoldItem::Strip(item@ Item { inner: StrippedItem(..), .. } ) => Some(item), + FoldItem::Strip(mut i) => { + i.inner = StrippedItem(box i.inner); + Some(i) + } + } + } +} + pub trait DocFolder : Sized { fn fold_item(&mut self, item: Item) -> Option { self.fold_item_recur(item) } /// don't override! - fn fold_item_recur(&mut self, item: Item) -> Option { - let Item { attrs, name, source, visibility, def_id, inner, stability, deprecation } = item; - let inner = match inner { + fn fold_inner_recur(&mut self, inner: ItemEnum) -> ItemEnum { + match inner { + StrippedItem(..) => unreachable!(), + ModuleItem(i) => { + ModuleItem(self.fold_mod(i)) + }, StructItem(mut i) => { let num_fields = i.fields.len(); i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect(); - i.fields_stripped |= num_fields != i.fields.len(); + i.fields_stripped |= num_fields != i.fields.len() || + i.fields.iter().any(|f| f.is_stripped()); StructItem(i) }, - ModuleItem(i) => { - ModuleItem(self.fold_mod(i)) - }, EnumItem(mut i) => { let num_variants = i.variants.len(); i.variants = i.variants.into_iter().filter_map(|x| self.fold_item(x)).collect(); - i.variants_stripped |= num_variants != i.variants.len(); + i.variants_stripped |= num_variants != i.variants.len() || + i.variants.iter().any(|f| f.is_stripped()); EnumItem(i) }, TraitItem(mut i) => { @@ -48,13 +70,24 @@ pub trait DocFolder : Sized { StructVariant(mut j) => { let num_fields = j.fields.len(); j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect(); - j.fields_stripped |= num_fields != j.fields.len(); + j.fields_stripped |= num_fields != j.fields.len() || + j.fields.iter().any(|f| f.is_stripped()); VariantItem(Variant {kind: StructVariant(j), ..i2}) }, _ => VariantItem(i2) } }, x => x + } + } + + /// don't override! + fn fold_item_recur(&mut self, item: Item) -> Option { + let Item { attrs, name, source, visibility, def_id, inner, stability, deprecation } = item; + + let inner = match inner { + StrippedItem(box i) => StrippedItem(box self.fold_inner_recur(i)), + _ => self.fold_inner_recur(inner), }; Some(Item { attrs: attrs, name: name, source: source, inner: inner, @@ -70,9 +103,8 @@ pub trait DocFolder : Sized { } fn fold_crate(&mut self, mut c: Crate) -> Crate { - c.module = c.module.and_then(|module| { - self.fold_item(module) - }); + c.module = c.module.and_then(|module| self.fold_item(module)); + c.external_traits = c.external_traits.into_iter().map(|(k, mut v)| { v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect(); (k, v) diff --git a/src/librustdoc/html/item_type.rs b/src/librustdoc/html/item_type.rs index afc93f41172e8..74f7b099044f1 100644 --- a/src/librustdoc/html/item_type.rs +++ b/src/librustdoc/html/item_type.rs @@ -44,7 +44,12 @@ pub enum ItemType { impl ItemType { pub fn from_item(item: &clean::Item) -> ItemType { - match item.inner { + let inner = match item.inner { + clean::StrippedItem(box ref item) => item, + ref inner@_ => inner, + }; + + match *inner { clean::ModuleItem(..) => ItemType::Module, clean::ExternCrateItem(..) => ItemType::ExternCrate, clean::ImportItem(..) => ItemType::Import, @@ -67,6 +72,7 @@ impl ItemType { clean::AssociatedConstItem(..) => ItemType::AssociatedConst, clean::AssociatedTypeItem(..) => ItemType::AssociatedType, clean::DefaultImplItem(..) => ItemType::Impl, + clean::StrippedItem(..) => unreachable!(), } } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 0f436efd70b2e..782b3cc52e82d 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -245,8 +245,7 @@ pub struct Cache { parent_stack: Vec, parent_is_trait_impl: bool, search_index: Vec, - privmod: bool, - remove_priv: bool, + stripped_mod: bool, access_levels: AccessLevels, deref_trait_did: Option, @@ -492,8 +491,7 @@ pub fn run(mut krate: clean::Crate, parent_is_trait_impl: false, extern_locations: HashMap::new(), primitive_locations: HashMap::new(), - remove_priv: cx.passes.contains("strip-private"), - privmod: false, + stripped_mod: false, access_levels: access_levels, orphan_methods: Vec::new(), traits: mem::replace(&mut krate.external_traits, HashMap::new()), @@ -874,7 +872,6 @@ impl<'a> DocFolder for SourceCollector<'a> { } }; } - self.fold_item_recur(item) } } @@ -938,14 +935,15 @@ impl<'a> SourceCollector<'a> { impl DocFolder for Cache { fn fold_item(&mut self, item: clean::Item) -> Option { - // If this is a private module, we don't want it in the search index. - let orig_privmod = match item.inner { - clean::ModuleItem(..) => { - let prev = self.privmod; - self.privmod = prev || (self.remove_priv && item.visibility != Some(hir::Public)); + // If this is a stripped module, + // we don't want it or its children in the search index. + let orig_stripped_mod = match item.inner { + clean::StrippedItem(box clean::ModuleItem(..)) => { + let prev = self.stripped_mod; + self.stripped_mod = true; prev } - _ => self.privmod, + _ => self.stripped_mod, }; // Register any generics to their corresponding string. This is used @@ -983,6 +981,7 @@ impl DocFolder for Cache { // Index this method for searching later on if let Some(ref s) = item.name { let (parent, is_method) = match item.inner { + clean::StrippedItem(..) => ((None, None), false), clean::AssociatedConstItem(..) | clean::TypedefItem(_, true) if self.parent_is_trait_impl => { // skip associated items in trait impls @@ -1027,7 +1026,7 @@ impl DocFolder for Cache { }; match parent { - (parent, Some(path)) if is_method || (!self.privmod && !hidden_field) => { + (parent, Some(path)) if is_method || (!self.stripped_mod && !hidden_field) => { // Needed to determine `self` type. let parent_basename = self.parent_stack.first().and_then(|parent| { match self.paths.get(parent) { @@ -1035,6 +1034,7 @@ impl DocFolder for Cache { _ => None } }); + debug_assert!(!item.is_stripped()); // A crate has a module at its root, containing all items, // which should not be indexed. The crate-item itself is @@ -1051,7 +1051,7 @@ impl DocFolder for Cache { }); } } - (Some(parent), None) if is_method || (!self.privmod && !hidden_field)=> { + (Some(parent), None) if is_method || (!self.stripped_mod && !hidden_field)=> { if parent.is_local() { // We have a parent, but we don't know where they're // defined yet. Wait for later to index this item. @@ -1075,7 +1075,7 @@ impl DocFolder for Cache { clean::StructItem(..) | clean::EnumItem(..) | clean::TypedefItem(..) | clean::TraitItem(..) | clean::FunctionItem(..) | clean::ModuleItem(..) | - clean::ForeignFunctionItem(..) if !self.privmod => { + clean::ForeignFunctionItem(..) if !self.stripped_mod => { // Reexported items mean that the same id can show up twice // in the rustdoc ast that we're looking at. We know, // however, that a reexported item doesn't show up in the @@ -1093,7 +1093,7 @@ impl DocFolder for Cache { } // link variants to their parent enum because pages aren't emitted // for each variant - clean::VariantItem(..) if !self.privmod => { + clean::VariantItem(..) if !self.stripped_mod => { let mut stack = self.stack.clone(); stack.pop(); self.paths.insert(item.def_id, (stack, ItemType::Enum)); @@ -1176,7 +1176,7 @@ impl DocFolder for Cache { if pushed { self.stack.pop().unwrap(); } if parent_pushed { self.parent_stack.pop().unwrap(); } - self.privmod = orig_privmod; + self.stripped_mod = orig_stripped_mod; self.parent_is_trait_impl = orig_parent_is_trait_impl; return ret; } @@ -1233,15 +1233,12 @@ impl Context { // render the crate documentation let mut work = vec!((self, item)); - loop { - match work.pop() { - Some((mut cx, item)) => cx.item(item, |cx, item| { - work.push((cx.clone(), item)); - })?, - None => break, - } - } + while let Some((mut cx, item)) = work.pop() { + cx.item(item, |cx, item| { + work.push((cx.clone(), item)) + })? + } Ok(()) } @@ -1296,79 +1293,72 @@ impl Context { layout::render(&mut writer, &cx.layout, &page, &Sidebar{ cx: cx, item: it }, &Item{ cx: cx, item: it })?; + } else { let mut url = repeat("../").take(cx.current.len()) .collect::(); - match cache().paths.get(&it.def_id) { - Some(&(ref names, _)) => { - for name in &names[..names.len() - 1] { - url.push_str(name); - url.push_str("/"); - } - url.push_str(&item_path(it)); - layout::redirect(&mut writer, &url)?; + if let Some(&(ref names, _)) = cache().paths.get(&it.def_id) { + for name in &names[..names.len() - 1] { + url.push_str(name); + url.push_str("/"); } - None => {} + url.push_str(&item_path(it)); + layout::redirect(&mut writer, &url)?; } } writer.flush() } - // Private modules may survive the strip-private pass if they - // contain impls for public types. These modules can also + // Stripped modules survive the rustdoc passes (i.e. `strip-private`) + // if they contain impls for public types. These modules can also // contain items such as publicly reexported structures. // // External crates will provide links to these structures, so - // these modules are recursed into, but not rendered normally (a - // flag on the context). + // these modules are recursed into, but not rendered normally + // (a flag on the context). if !self.render_redirect_pages { - self.render_redirect_pages = self.ignore_private_item(&item); + self.render_redirect_pages = self.maybe_ignore_item(&item); } - match item.inner { + if item.is_mod() { // modules are special because they add a namespace. We also need to // recurse into the items of the module as well. - clean::ModuleItem(..) => { - let name = item.name.as_ref().unwrap().to_string(); - let mut item = Some(item); - self.recurse(name, |this| { - let item = item.take().unwrap(); - let joint_dst = this.dst.join("index.html"); - let dst = try_err!(File::create(&joint_dst), &joint_dst); - try_err!(render(dst, this, &item, false), &joint_dst); - - let m = match item.inner { - clean::ModuleItem(m) => m, - _ => unreachable!() - }; - - // render sidebar-items.js used throughout this module - { - let items = this.build_sidebar_items(&m); - let js_dst = this.dst.join("sidebar-items.js"); - let mut js_out = BufWriter::new(try_err!(File::create(&js_dst), &js_dst)); - try_err!(write!(&mut js_out, "initSidebarItems({});", - as_json(&items)), &js_dst); - } + let name = item.name.as_ref().unwrap().to_string(); + let mut item = Some(item); + self.recurse(name, |this| { + let item = item.take().unwrap(); + let joint_dst = this.dst.join("index.html"); + let dst = try_err!(File::create(&joint_dst), &joint_dst); + try_err!(render(dst, this, &item, false), &joint_dst); - for item in m.items { - f(this,item); - } - Ok(()) - }) - } + let m = match item.inner { + clean::StrippedItem(box clean::ModuleItem(m)) | + clean::ModuleItem(m) => m, + _ => unreachable!() + }; - // Things which don't have names (like impls) don't get special - // pages dedicated to them. - _ if item.name.is_some() => { - let joint_dst = self.dst.join(&item_path(&item)); + // render sidebar-items.js used throughout this module + { + let items = this.build_sidebar_items(&m); + let js_dst = this.dst.join("sidebar-items.js"); + let mut js_out = BufWriter::new(try_err!(File::create(&js_dst), &js_dst)); + try_err!(write!(&mut js_out, "initSidebarItems({});", + as_json(&items)), &js_dst); + } - let dst = try_err!(File::create(&joint_dst), &joint_dst); - try_err!(render(dst, self, &item, true), &joint_dst); + for item in m.items { + f(this,item); + } Ok(()) - } + }) + } else if item.name.is_some() { + let joint_dst = self.dst.join(&item_path(&item)); - _ => Ok(()) + let dst = try_err!(File::create(&joint_dst), &joint_dst); + try_err!(render(dst, self, &item, true), &joint_dst); + Ok(()) + } else { + Ok(()) } } @@ -1376,7 +1366,7 @@ impl Context { // BTreeMap instead of HashMap to get a sorted output let mut map = BTreeMap::new(); for item in &m.items { - if self.ignore_private_item(item) { continue } + if self.maybe_ignore_item(item) { continue } let short = shortty(item).to_static_str(); let myname = match item.name { @@ -1394,27 +1384,18 @@ impl Context { return map; } - fn ignore_private_item(&self, it: &clean::Item) -> bool { + fn maybe_ignore_item(&self, it: &clean::Item) -> bool { match it.inner { + clean::StrippedItem(..) => true, clean::ModuleItem(ref m) => { - (m.items.is_empty() && - it.doc_value().is_none() && - it.visibility != Some(hir::Public)) || - (self.passes.contains("strip-private") && it.visibility != Some(hir::Public)) - } - clean::PrimitiveItem(..) => it.visibility != Some(hir::Public), + it.doc_value().is_none() && m.items.is_empty() && it.visibility != Some(hir::Public) + }, _ => false, } } } impl<'a> Item<'a> { - fn ismodule(&self) -> bool { - match self.item.inner { - clean::ModuleItem(..) => true, _ => false - } - } - /// Generate a url appropriate for an `href` attribute back to the source of /// this item. /// @@ -1495,6 +1476,7 @@ impl<'a> Item<'a> { impl<'a> fmt::Display for Item<'a> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + debug_assert!(!self.item.is_stripped()); // Write the breadcrumb trail header for the top write!(fmt, "\n

")?; match self.item.inner { @@ -1516,7 +1498,7 @@ impl<'a> fmt::Display for Item<'a> { }; if !is_primitive { let cur = &self.cx.current; - let amt = if self.ismodule() { cur.len() - 1 } else { cur.len() }; + let amt = if self.item.is_mod() { cur.len() - 1 } else { cur.len() }; for (i, component) in cur.iter().enumerate().take(amt) { write!(fmt, "{}::", repeat("../").take(cur.len() - i - 1) @@ -1575,15 +1557,12 @@ impl<'a> fmt::Display for Item<'a> { } fn item_path(item: &clean::Item) -> String { - match item.inner { - clean::ModuleItem(..) => { - format!("{}/index.html", item.name.as_ref().unwrap()) - } - _ => { - format!("{}.{}.html", - shortty(item).to_static_str(), - *item.name.as_ref().unwrap()) - } + if item.is_mod() { + format!("{}/index.html", item.name.as_ref().unwrap()) + } else { + format!("{}.{}.html", + shortty(item).to_static_str(), + *item.name.as_ref().unwrap()) } } @@ -1626,7 +1605,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context, document(w, cx, item)?; let mut indices = (0..items.len()).filter(|i| { - !cx.ignore_private_item(&items[*i]) + !cx.maybe_ignore_item(&items[*i]) }).collect::>(); // the order of item types in the listing @@ -1670,6 +1649,9 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context, let mut curty = None; for &idx in &indices { let myitem = &items[idx]; + if myitem.is_stripped() { + continue; + } let myty = Some(shortty(myitem)); if curty == Some(ItemType::ExternCrate) && myty == Some(ItemType::Import) { @@ -2146,6 +2128,7 @@ fn render_assoc_item(w: &mut fmt::Formatter, where_clause = WhereClause(g)) } match item.inner { + clean::StrippedItem(..) => Ok(()), clean::TyMethodItem(ref m) => { method(w, item, m.unsafety, hir::Constness::NotConst, m.abi, &m.generics, &m.self_, &m.decl, link) @@ -2540,6 +2523,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi assoc_type(w, item, bounds, default.as_ref(), link)?; write!(w, "

\n")?; } + clean::StrippedItem(..) => return Ok(()), _ => panic!("can't make docs for trait item with name {:?}", item.name) } diff --git a/src/librustdoc/passes.rs b/src/librustdoc/passes.rs index 88cb20991d660..06d84fc8822ed 100644 --- a/src/librustdoc/passes.rs +++ b/src/librustdoc/passes.rs @@ -21,6 +21,7 @@ use clean::Item; use plugins; use fold; use fold::DocFolder; +use fold::FoldItem::Strip; /// Strip items marked `#[doc(hidden)]` pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult { @@ -45,12 +46,10 @@ pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult { ..i }); } - _ => { - return None; - } + clean::ModuleItem(..) => return Strip(i).fold(), + _ => return None, } } - self.fold_item_recur(i) } } @@ -125,6 +124,7 @@ struct Stripper<'a> { impl<'a> fold::DocFolder for Stripper<'a> { fn fold_item(&mut self, i: Item) -> Option { match i.inner { + clean::StrippedItem(..) => return Some(i), // These items can all get re-exported clean::TypedefItem(..) | clean::StaticItem(..) | clean::StructItem(..) | clean::EnumItem(..) | @@ -153,8 +153,11 @@ impl<'a> fold::DocFolder for Stripper<'a> { } } - // handled below - clean::ModuleItem(..) => {} + clean::ModuleItem(..) => { + if i.def_id.is_local() && i.visibility != Some(hir::Public) { + return Strip(self.fold_item_recur(i).unwrap()).fold() + } + } // trait impls for private items should be stripped clean::ImplItem(clean::Impl{ @@ -165,7 +168,7 @@ impl<'a> fold::DocFolder for Stripper<'a> { } } // handled in the `strip-priv-imports` pass - clean::ExternCrateItem(..) | clean::ImportItem(_) => {} + clean::ExternCrateItem(..) | clean::ImportItem(..) => {} clean::DefaultImplItem(..) | clean::ImplItem(..) => {} @@ -187,7 +190,6 @@ impl<'a> fold::DocFolder for Stripper<'a> { // implementations of traits are always public. clean::ImplItem(ref imp) if imp.trait_.is_some() => true, - // Struct variant fields have inherited visibility clean::VariantItem(clean::Variant { kind: clean::StructVariant(..) @@ -202,19 +204,17 @@ impl<'a> fold::DocFolder for Stripper<'a> { self.fold_item_recur(i) }; - i.and_then(|i| { - match i.inner { - // emptied modules/impls have no need to exist - clean::ModuleItem(ref m) - if m.items.is_empty() && - i.doc_value().is_none() => None, - clean::ImplItem(ref i) if i.items.is_empty() => None, - _ => { - self.retained.insert(i.def_id); - Some(i) - } + i.and_then(|i| { match i.inner { + // emptied modules/impls have no need to exist + clean::ModuleItem(ref m) + if m.items.is_empty() && + i.doc_value().is_none() => None, + clean::ImplItem(ref i) if i.items.is_empty() => None, + _ => { + self.retained.insert(i.def_id); + Some(i) } - }) + }}) } } diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 331e3431cee84..5bd3b9c4f59e7 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -431,7 +431,7 @@ impl Collector { // compiler failures are test failures should_panic: testing::ShouldPanic::No, }, - testfn: testing::DynTestFn(Box::new(move|| { + testfn: testing::DynTestFn(box move|| { runtest(&test, &cratename, cfgs, @@ -442,7 +442,7 @@ impl Collector { as_test_harness, compile_fail, &opts); - })) + }) }); } diff --git a/src/test/auxiliary/reexp_stripped.rs b/src/test/auxiliary/reexp_stripped.rs new file mode 100644 index 0000000000000..2b061e3997d84 --- /dev/null +++ b/src/test/auxiliary/reexp_stripped.rs @@ -0,0 +1,21 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub use private::Quz; +pub use hidden::Bar; + +mod private { + pub struct Quz; +} + +#[doc(hidden)] +pub mod hidden { + pub struct Bar; +} diff --git a/src/test/rustdoc/redirect.rs b/src/test/rustdoc/redirect.rs new file mode 100644 index 0000000000000..98e66e8c024bd --- /dev/null +++ b/src/test/rustdoc/redirect.rs @@ -0,0 +1,48 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:reexp_stripped.rs +// build-aux-docs +// ignore-cross-compile + +extern crate reexp_stripped; + +pub trait Foo {} + +// @has redirect/index.html +// @has - '//code' 'pub use reexp_stripped::Bar' +// @has - '//code/a' 'Bar' +// @has reexp_stripped/hidden/struct.Bar.html +// @has - '//p/a' '../../reexp_stripped/struct.Bar.html' +// @has 'reexp_stripped/struct.Bar.html' +#[doc(no_inline)] +pub use reexp_stripped::Bar; +impl Foo for Bar {} + +// @has redirect/index.html +// @has - '//code' 'pub use reexp_stripped::Quz' +// @has - '//code/a' 'Quz' +// @has reexp_stripped/private/struct.Quz.html +// @has - '//p/a' '../../reexp_stripped/struct.Quz.html' +// @has 'reexp_stripped/struct.Quz.html' +#[doc(no_inline)] +pub use reexp_stripped::Quz; +impl Foo for Quz {} + +mod private_no_inline { + pub struct Qux; + impl ::Foo for Qux {} +} + +// @has redirect/index.html +// @has - '//code' 'pub use private_no_inline::Qux' +// @!has - '//code/a' 'Qux' +#[doc(no_inline)] +pub use private_no_inline::Qux; From 0ef85c1e6a573d736592f00402456616a25eee0f Mon Sep 17 00:00:00 2001 From: mitaa Date: Sat, 2 Apr 2016 08:17:59 +0200 Subject: [PATCH 03/17] Refactor `HiddenStructField` into `StrippedItem` --- src/librustdoc/clean/mod.rs | 26 +++++++++---------- src/librustdoc/html/render.rs | 40 ++++++++++------------------- src/librustdoc/passes.rs | 22 ++++------------ src/test/rustdoc/structfields.rs | 44 ++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 56 deletions(-) create mode 100644 src/test/rustdoc/structfields.rs diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 9c60b90a1fcf5..7437d6087718a 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -14,7 +14,6 @@ pub use self::Type::*; pub use self::PrimitiveType::*; pub use self::TypeKind::*; -pub use self::StructField::*; pub use self::VariantKind::*; pub use self::Mutability::*; pub use self::Import::*; @@ -309,6 +308,15 @@ impl Item { pub fn is_stripped(&self) -> bool { match self.inner { StrippedItem(..) => true, _ => false } } + pub fn has_stripped_fields(&self) -> Option { + match self.inner { + StructItem(ref _struct) => Some(_struct.fields_stripped), + VariantItem(Variant { kind: StructVariant(ref vstruct)} ) => { + Some(vstruct.fields_stripped) + }, + _ => None, + } + } pub fn stability_class(&self) -> String { self.stability.as_ref().map(|ref s| { @@ -346,7 +354,7 @@ pub enum ItemEnum { TyMethodItem(TyMethod), /// A method with a body. MethodItem(Method), - StructFieldItem(StructField), + StructFieldItem(Type), VariantItem(Variant), /// `fn`s from an extern block ForeignFunctionItem(Function), @@ -1740,12 +1748,6 @@ impl<'tcx> Clean for ty::Ty<'tcx> { } } -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] -pub enum StructField { - HiddenStructField, // inserted later by strip passes - TypedStructField(Type), -} - impl Clean for hir::StructField { fn clean(&self, cx: &DocContext) -> Item { Item { @@ -1756,7 +1758,7 @@ impl Clean for hir::StructField { stability: get_stability(cx, cx.map.local_def_id(self.id)), deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)), def_id: cx.map.local_def_id(self.id), - inner: StructFieldItem(TypedStructField(self.ty.clean(cx))), + inner: StructFieldItem(self.ty.clean(cx)), } } } @@ -1773,7 +1775,7 @@ impl<'tcx> Clean for ty::FieldDefData<'tcx, 'static> { stability: get_stability(cx, self.did), deprecation: get_deprecation(cx, self.did), def_id: self.did, - inner: StructFieldItem(TypedStructField(self.unsubst_ty().clean(cx))), + inner: StructFieldItem(self.unsubst_ty().clean(cx)), } } } @@ -1904,9 +1906,7 @@ impl<'tcx> Clean for ty::VariantDefData<'tcx, 'static> { def_id: field.did, stability: get_stability(cx, field.did), deprecation: get_deprecation(cx, field.did), - inner: StructFieldItem( - TypedStructField(field.unsubst_ty().clean(cx)) - ) + inner: StructFieldItem(field.unsubst_ty().clean(cx)) } }).collect() }) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 782b3cc52e82d..678a9d75f96cc 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1020,13 +1020,9 @@ impl DocFolder for Cache { } _ => ((None, Some(&*self.stack)), false) }; - let hidden_field = match item.inner { - clean::StructFieldItem(clean::HiddenStructField) => true, - _ => false - }; match parent { - (parent, Some(path)) if is_method || (!self.stripped_mod && !hidden_field) => { + (parent, Some(path)) if is_method || (!self.stripped_mod) => { // Needed to determine `self` type. let parent_basename = self.parent_stack.first().and_then(|parent| { match self.paths.get(parent) { @@ -1051,7 +1047,7 @@ impl DocFolder for Cache { }); } } - (Some(parent), None) if is_method || (!self.stripped_mod && !hidden_field)=> { + (Some(parent), None) if is_method || (!self.stripped_mod)=> { if parent.is_local() { // We have a parent, but we don't know where they're // defined yet. Wait for later to index this item. @@ -2165,8 +2161,7 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, document(w, cx, it)?; let mut fields = s.fields.iter().filter(|f| { match f.inner { - clean::StructFieldItem(clean::HiddenStructField) => false, - clean::StructFieldItem(clean::TypedStructField(..)) => true, + clean::StructFieldItem(..) => true, _ => false, } }).peekable(); @@ -2256,7 +2251,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, if let clean::VariantItem( Variant { kind: StructVariant(ref s) } ) = variant.inner { let fields = s.fields.iter().filter(|f| { match f.inner { - clean::StructFieldItem(clean::TypedStructField(..)) => true, + clean::StructFieldItem(..) => true, _ => false, } }); @@ -2315,24 +2310,17 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item, match ty { doctree::Plain => { write!(w, " {{\n{}", tab)?; - let mut fields_stripped = false; for field in fields { - match field.inner { - clean::StructFieldItem(clean::HiddenStructField) => { - fields_stripped = true; - } - clean::StructFieldItem(clean::TypedStructField(ref ty)) => { - write!(w, " {}{}: {},\n{}", - VisSpace(field.visibility), - field.name.as_ref().unwrap(), - *ty, - tab)?; - } - _ => unreachable!(), - }; + if let clean::StructFieldItem(ref ty) = field.inner { + write!(w, " {}{}: {},\n{}", + VisSpace(field.visibility), + field.name.as_ref().unwrap(), + *ty, + tab)?; + } } - if fields_stripped { + if it.has_stripped_fields().unwrap() { write!(w, " // some fields omitted\n{}", tab)?; } write!(w, "}}")?; @@ -2344,10 +2332,10 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item, write!(w, ", ")?; } match field.inner { - clean::StructFieldItem(clean::HiddenStructField) => { + clean::StrippedItem(box clean::StructFieldItem(..)) => { write!(w, "_")? } - clean::StructFieldItem(clean::TypedStructField(ref ty)) => { + clean::StructFieldItem(ref ty) => { write!(w, "{}{}", VisSpace(field.visibility), *ty)? } _ => unreachable!() diff --git a/src/librustdoc/passes.rs b/src/librustdoc/passes.rs index 06d84fc8822ed..f93ecb46228c2 100644 --- a/src/librustdoc/passes.rs +++ b/src/librustdoc/passes.rs @@ -40,13 +40,9 @@ pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult { // use a dedicated hidden item for given item type if any match i.inner { - clean::StructFieldItem(..) => { - return Some(clean::Item { - inner: clean::StructFieldItem(clean::HiddenStructField), - ..i - }); + clean::StructFieldItem(..) | clean::ModuleItem(..) => { + return Strip(i).fold() } - clean::ModuleItem(..) => return Strip(i).fold(), _ => return None, } } @@ -130,7 +126,8 @@ impl<'a> fold::DocFolder for Stripper<'a> { clean::StructItem(..) | clean::EnumItem(..) | clean::TraitItem(..) | clean::FunctionItem(..) | clean::VariantItem(..) | clean::MethodItem(..) | - clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) => { + clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) | + clean::ConstantItem(..) => { if i.def_id.is_local() { if !self.access_levels.is_exported(i.def_id) { return None; @@ -138,18 +135,9 @@ impl<'a> fold::DocFolder for Stripper<'a> { } } - clean::ConstantItem(..) => { - if i.def_id.is_local() && !self.access_levels.is_exported(i.def_id) { - return None; - } - } - clean::StructFieldItem(..) => { if i.visibility != Some(hir::Public) { - return Some(clean::Item { - inner: clean::StructFieldItem(clean::HiddenStructField), - ..i - }) + return Strip(i).fold(); } } diff --git a/src/test/rustdoc/structfields.rs b/src/test/rustdoc/structfields.rs new file mode 100644 index 0000000000000..c4327f70728cb --- /dev/null +++ b/src/test/rustdoc/structfields.rs @@ -0,0 +1,44 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// @has structfields/struct.Foo.html +pub struct Foo { + // @has - //pre "pub a: ()" + pub a: (), + // @has - //pre "// some fields omitted" + // @!has - //pre "b: ()" + b: (), + // @!has - //pre "c: usize" + #[doc(hidden)] + c: usize, + // @has - //pre "pub d: usize" + pub d: usize, +} + +// @has structfields/struct.Bar.html +pub struct Bar { + // @has - //pre "pub a: ()" + pub a: (), + // @!has - //pre "// some fields omitted" +} + +// @has structfields/enum.Qux.html +pub enum Qux { + Quz { + // @has - //pre "a: ()" + a: (), + // @!has - //pre "b: ()" + #[doc(hidden)] + b: (), + // @has - //pre "c: usize" + c: usize, + // @has - //pre "// some fields omitted" + }, +} From 95eb8a68aa38ebeaadcca337d6005efabcf4a05e Mon Sep 17 00:00:00 2001 From: mitaa Date: Sat, 2 Apr 2016 09:03:55 +0200 Subject: [PATCH 04/17] Slim down `rustdoc::html::render::Context` Like the comment on `Context` explains, `Context` is supposed to be lightweight, so we're putting everything that's immutable after creation of the Context behind an `Arc`. --- src/librustdoc/html/render.rs | 87 +++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 678a9d75f96cc..78dd14766e742 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -91,12 +91,20 @@ pub struct Context { /// String representation of how to get back to the root path of the 'doc/' /// folder in terms of a relative URL. pub root_path: String, - /// The path to the crate root source minus the file name. - /// Used for simplifying paths to the highlighted source code files. - pub src_root: PathBuf, /// The current destination folder of where HTML artifacts should be placed. /// This changes as the context descends into the module hierarchy. pub dst: PathBuf, + /// A flag, which when `true`, will render pages which redirect to the + /// real location of an item. This is used to allow external links to + /// publicly reused items to redirect to the right location. + pub render_redirect_pages: bool, + pub shared: Arc, +} + +pub struct SharedContext { + /// The path to the crate root source minus the file name. + /// Used for simplifying paths to the highlighted source code files. + pub src_root: PathBuf, /// This describes the layout of each page, and is not modified after /// creation of the context (contains info like the favicon and added html). pub layout: layout::Layout, @@ -106,10 +114,6 @@ pub struct Context { pub include_sources: bool, /// The local file sources we've emitted and their respective url-paths. pub local_sources: HashMap, - /// A flag, which when turned off, will render pages which redirect to the - /// real location of an item. This is used to allow external links to - /// publicly reused items to redirect to the right location. - pub render_redirect_pages: bool, /// All the passes that were run on this crate. pub passes: HashSet, /// The base-URL of the issue tracker for when an item has been tagged with @@ -259,7 +263,7 @@ pub struct Cache { /// Helper struct to render all source code to HTML pages struct SourceCollector<'a> { - cx: &'a mut Context, + scx: &'a mut SharedContext, /// Root destination to place all HTML output into dst: PathBuf, @@ -412,12 +416,12 @@ pub fn run(mut krate: clean::Crate, Some(p) => p.to_path_buf(), None => PathBuf::new(), }; - let mut cx = Context { - dst: dst, + let mut scx = SharedContext { src_root: src_root, passes: passes, - current: Vec::new(), - root_path: String::new(), + include_sources: true, + local_sources: HashMap::new(), + issue_tracker_base_url: None, layout: layout::Layout { logo: "".to_string(), favicon: "".to_string(), @@ -425,14 +429,8 @@ pub fn run(mut krate: clean::Crate, krate: krate.name.clone(), playground_url: "".to_string(), }, - include_sources: true, - local_sources: HashMap::new(), - render_redirect_pages: false, - issue_tracker_base_url: None, }; - try_err!(mkdir(&cx.dst), &cx.dst); - // Crawl the crate attributes looking for attributes which control how we're // going to emit HTML if let Some(attrs) = krate.module.as_ref().map(|m| m.attrs.list("doc")) { @@ -440,15 +438,15 @@ pub fn run(mut krate: clean::Crate, match *attr { clean::NameValue(ref x, ref s) if "html_favicon_url" == *x => { - cx.layout.favicon = s.to_string(); + scx.layout.favicon = s.to_string(); } clean::NameValue(ref x, ref s) if "html_logo_url" == *x => { - cx.layout.logo = s.to_string(); + scx.layout.logo = s.to_string(); } clean::NameValue(ref x, ref s) if "html_playground_url" == *x => { - cx.layout.playground_url = s.to_string(); + scx.layout.playground_url = s.to_string(); markdown::PLAYGROUND_KRATE.with(|slot| { if slot.borrow().is_none() { let name = krate.name.clone(); @@ -458,16 +456,25 @@ pub fn run(mut krate: clean::Crate, } clean::NameValue(ref x, ref s) if "issue_tracker_base_url" == *x => { - cx.issue_tracker_base_url = Some(s.to_string()); + scx.issue_tracker_base_url = Some(s.to_string()); } clean::Word(ref x) if "html_no_source" == *x => { - cx.include_sources = false; + scx.include_sources = false; } _ => {} } } } + try_err!(mkdir(&dst), &dst); + krate = render_sources(&dst, &mut scx, krate)?; + let cx = Context { + current: Vec::new(), + root_path: String::new(), + dst: dst, + render_redirect_pages: false, + shared: Arc::new(scx), + }; // Crawl the crate to build various caches used for the output let analysis = ::ANALYSISKEY.with(|a| a.clone()); @@ -538,7 +545,6 @@ pub fn run(mut krate: clean::Crate, CURRENT_LOCATION_KEY.with(|s| s.borrow_mut().clear()); write_shared(&cx, &krate, &*cache, index)?; - let krate = render_sources(&mut cx, krate)?; // And finally render the whole crate's documentation cx.krate(krate) @@ -760,16 +766,16 @@ fn write_shared(cx: &Context, Ok(()) } -fn render_sources(cx: &mut Context, +fn render_sources(dst: &Path, scx: &mut SharedContext, krate: clean::Crate) -> Result { info!("emitting source files"); - let dst = cx.dst.join("src"); + let dst = dst.join("src"); try_err!(mkdir(&dst), &dst); let dst = dst.join(&krate.name); try_err!(mkdir(&dst), &dst); let mut folder = SourceCollector { dst: dst, - cx: cx, + scx: scx, }; Ok(folder.fold_crate(krate)) } @@ -847,7 +853,7 @@ impl<'a> DocFolder for SourceCollector<'a> { fn fold_item(&mut self, item: clean::Item) -> Option { // If we're including source files, and we haven't seen this file yet, // then we need to render it out to the filesystem - if self.cx.include_sources + if self.scx.include_sources // skip all invalid spans && item.source.filename != "" // macros from other libraries get special filenames which we can @@ -860,7 +866,7 @@ impl<'a> DocFolder for SourceCollector<'a> { // something like that), so just don't include sources for the // entire crate. The other option is maintaining this mapping on a // per-file basis, but that's probably not worth it... - self.cx + self.scx .include_sources = match self.emit_source(&item.source.filename) { Ok(()) => true, Err(e) => { @@ -880,7 +886,7 @@ impl<'a> SourceCollector<'a> { /// Renders the given filename into its corresponding HTML source file. fn emit_source(&mut self, filename: &str) -> io::Result<()> { let p = PathBuf::from(filename); - if self.cx.local_sources.contains_key(&p) { + if self.scx.local_sources.contains_key(&p) { // We've already emitted this source return Ok(()); } @@ -901,7 +907,7 @@ impl<'a> SourceCollector<'a> { let mut cur = self.dst.clone(); let mut root_path = String::from("../../"); let mut href = String::new(); - clean_srcpath(&self.cx.src_root, &p, false, |component| { + clean_srcpath(&self.scx.src_root, &p, false, |component| { cur.push(component); mkdir(&cur).unwrap(); root_path.push_str("../"); @@ -925,10 +931,10 @@ impl<'a> SourceCollector<'a> { description: &desc, keywords: BASIC_KEYWORDS, }; - layout::render(&mut w, &self.cx.layout, + layout::render(&mut w, &self.scx.layout, &page, &(""), &Source(contents))?; w.flush()?; - self.cx.local_sources.insert(p, href); + self.scx.local_sources.insert(p, href); Ok(()) } } @@ -1265,10 +1271,10 @@ impl Context { let tyname = shortty(it).to_static_str(); let desc = if it.is_crate() { format!("API documentation for the Rust `{}` crate.", - cx.layout.krate) + cx.shared.layout.krate) } else { format!("API documentation for the Rust `{}` {} in crate `{}`.", - it.name.as_ref().unwrap(), tyname, cx.layout.krate) + it.name.as_ref().unwrap(), tyname, cx.shared.layout.krate) }; let keywords = make_item_keywords(it); let page = layout::Page { @@ -1286,7 +1292,7 @@ impl Context { // write syscall all the time. let mut writer = BufWriter::new(w); if !cx.render_redirect_pages { - layout::render(&mut writer, &cx.layout, &page, + layout::render(&mut writer, &cx.shared.layout, &page, &Sidebar{ cx: cx, item: it }, &Item{ cx: cx, item: it })?; @@ -1434,10 +1440,11 @@ impl<'a> Item<'a> { // know the span, so we plow forward and generate a proper url. The url // has anchors for the line numbers that we're linking to. } else if self.item.def_id.is_local() { - self.cx.local_sources.get(&PathBuf::from(&self.item.source.filename)).map(|path| { + let path = PathBuf::from(&self.item.source.filename); + self.cx.shared.local_sources.get(&path).map(|path| { format!("{root}src/{krate}/{path}#{href}", root = self.cx.root_path, - krate = self.cx.layout.krate, + krate = self.cx.shared.layout.krate, path = path, href = href) }) @@ -1520,7 +1527,7 @@ impl<'a> fmt::Display for Item<'a> { // [src] link in the downstream documentation will actually come back to // this page, and this link will be auto-clicked. The `id` attribute is // used to find the link to auto-click. - if self.cx.include_sources && !is_primitive { + if self.cx.shared.include_sources && !is_primitive { if let Some(l) = self.href() { write!(fmt, "[src]", @@ -1752,7 +1759,7 @@ fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Optio format!("Deprecated{}{}", since, Markdown(&reason)) } else if stab.level == stability::Unstable { let unstable_extra = if show_reason { - match (!stab.feature.is_empty(), &cx.issue_tracker_base_url, stab.issue) { + match (!stab.feature.is_empty(), &cx.shared.issue_tracker_base_url, stab.issue) { (true, &Some(ref tracker_url), Some(issue_no)) if issue_no > 0 => format!(" ({} #{})", Escape(&stab.feature), tracker_url, issue_no, issue_no), From 39055800c1ae762b7eac2e3627d4a5be91a65289 Mon Sep 17 00:00:00 2001 From: Dave Huseby Date: Sat, 2 Apr 2016 08:50:09 -0700 Subject: [PATCH 05/17] adding freebsd i686 snapshot 4d3eebf --- src/snapshots.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/snapshots.txt b/src/snapshots.txt index d1a2ab464cba7..d11faadaef850 100644 --- a/src/snapshots.txt +++ b/src/snapshots.txt @@ -16,6 +16,7 @@ S 2016-02-17 4d3eebf winnt-i386 0c336d794a65f8e285c121866c7d59aa2dd0d1e1 winnt-x86_64 27e75b1bf99770b3564bcebd7f3230be01135a92 openbsd-x86_64 ac957c6b84de2bd67f01df085d9ea515f96e22f3 + freebsd-i386 4e2af0b34eb335e173aebff543be693724a956c2 freebsd-x86_64 f38991fbb81c1cd8d0bbda396f98f13a55b42804 S 2015-12-18 3391630 From 9f3de647326fbe50e0e283b9018ab7c41abccde3 Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Sat, 2 Apr 2016 18:40:59 +0200 Subject: [PATCH 06/17] Prefix jemalloc on DragonFly to prevent segfaults. Similar to commits ed015456a114ae907a36af80c06f81ea93182a24 (iOS) and e3b414d8612314e74e2b0ebde1ed5c6997d28e8d (Android) --- mk/rt.mk | 2 ++ src/liballoc_jemalloc/build.rs | 2 ++ src/liballoc_jemalloc/lib.rs | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/mk/rt.mk b/mk/rt.mk index 5f46b3a20c9fa..4cb43303fb728 100644 --- a/mk/rt.mk +++ b/mk/rt.mk @@ -157,6 +157,8 @@ else ifeq ($(findstring android, $(OSTYPE_$(1))), android) # If the test suite passes, however, without symbol prefixes then we should be # good to go! JEMALLOC_ARGS_$(1) := --disable-tls --with-jemalloc-prefix=je_ +else ifeq ($(findstring dragonfly, $(OSTYPE_$(1))), dragonfly) + JEMALLOC_ARGS_$(1) := --with-jemalloc-prefix=je_ endif ifdef CFG_ENABLE_DEBUG_JEMALLOC diff --git a/src/liballoc_jemalloc/build.rs b/src/liballoc_jemalloc/build.rs index 9e2090c3246c6..5d521913b48f3 100644 --- a/src/liballoc_jemalloc/build.rs +++ b/src/liballoc_jemalloc/build.rs @@ -86,6 +86,8 @@ fn main() { // should be good to go! cmd.arg("--with-jemalloc-prefix=je_"); cmd.arg("--disable-tls"); + } else if target.contains("dragonfly") { + cmd.arg("--with-jemalloc-prefix=je_"); } if cfg!(feature = "debug-jemalloc") { diff --git a/src/liballoc_jemalloc/lib.rs b/src/liballoc_jemalloc/lib.rs index c96d303e6bb64..3a30bebec5478 100644 --- a/src/liballoc_jemalloc/lib.rs +++ b/src/liballoc_jemalloc/lib.rs @@ -42,22 +42,27 @@ use libc::{c_int, c_void, size_t}; extern {} // Note that the symbols here are prefixed by default on OSX (we don't -// explicitly request it), and on Android we explicitly request it as -// unprefixing cause segfaults (mismatches in allocators). +// explicitly request it), and on Android and DragonFly we explicitly request +// it as unprefixing cause segfaults (mismatches in allocators). extern { - #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios", + target_os = "dragonfly"), link_name = "je_mallocx")] fn mallocx(size: size_t, flags: c_int) -> *mut c_void; - #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios", + target_os = "dragonfly"), link_name = "je_rallocx")] fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void; - #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios", + target_os = "dragonfly"), link_name = "je_xallocx")] fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t; - #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios", + target_os = "dragonfly"), link_name = "je_sdallocx")] fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int); - #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios", + target_os = "dragonfly"), link_name = "je_nallocx")] fn nallocx(size: size_t, flags: c_int) -> size_t; } From f486b7c3b31a483fee944970c725cf741a4969db Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 2 Apr 2016 20:41:37 -0700 Subject: [PATCH 07/17] Inline Duration constructors and accessors These are all super small functions --- src/libstd/time/duration.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index 945eb6a42e5a7..8a50f07e6d854 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -52,6 +52,7 @@ impl Duration { /// If the nanoseconds is greater than 1 billion (the number of nanoseconds /// in a second), then it will carry over into the seconds provided. #[stable(feature = "duration", since = "1.3.0")] + #[inline] pub fn new(secs: u64, nanos: u32) -> Duration { let secs = secs + (nanos / NANOS_PER_SEC) as u64; let nanos = nanos % NANOS_PER_SEC; @@ -60,12 +61,14 @@ impl Duration { /// Creates a new `Duration` from the specified number of seconds. #[stable(feature = "duration", since = "1.3.0")] + #[inline] pub fn from_secs(secs: u64) -> Duration { Duration { secs: secs, nanos: 0 } } /// Creates a new `Duration` from the specified number of milliseconds. #[stable(feature = "duration", since = "1.3.0")] + #[inline] pub fn from_millis(millis: u64) -> Duration { let secs = millis / MILLIS_PER_SEC; let nanos = ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI; @@ -77,6 +80,7 @@ impl Duration { /// The extra precision represented by this duration is ignored (e.g. extra /// nanoseconds are not represented in the returned value). #[stable(feature = "duration", since = "1.3.0")] + #[inline] pub fn as_secs(&self) -> u64 { self.secs } /// Returns the nanosecond precision represented by this duration. @@ -85,6 +89,7 @@ impl Duration { /// represented by nanoseconds. The returned number always represents a /// fractional portion of a second (e.g. it is less than one billion). #[stable(feature = "duration", since = "1.3.0")] + #[inline] pub fn subsec_nanos(&self) -> u32 { self.nanos } } From 0163ccc36bdd16f0d5c92dfb5625eb1121d16479 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 3 Apr 2016 22:10:21 +0200 Subject: [PATCH 08/17] Fix "consider removing this semicolon" help Check last statement in a block, not the first --- src/librustc/middle/liveness.rs | 4 ++-- .../compile-fail/consider-removing-last-semi.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/test/compile-fail/consider-removing-last-semi.rs diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index cc37ee7dbda05..4451e7ac472e5 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -1502,7 +1502,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { } else { let ends_with_stmt = match body.expr { None if !body.stmts.is_empty() => - match body.stmts.first().unwrap().node { + match body.stmts.last().unwrap().node { hir::StmtSemi(ref e, _) => { self.ir.tcx.expr_ty(&e) == t_ret }, @@ -1515,7 +1515,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { E0269, "not all control paths return a value"); if ends_with_stmt { - let last_stmt = body.stmts.first().unwrap(); + let last_stmt = body.stmts.last().unwrap(); let original_span = original_sp(self.ir.tcx.sess.codemap(), last_stmt.span, sp); let span_semicolon = Span { diff --git a/src/test/compile-fail/consider-removing-last-semi.rs b/src/test/compile-fail/consider-removing-last-semi.rs new file mode 100644 index 0000000000000..a25f0b17335ed --- /dev/null +++ b/src/test/compile-fail/consider-removing-last-semi.rs @@ -0,0 +1,17 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn f() -> String { //~ ERROR E0269 + //~^ HELP detailed explanation + 0u8; + "bla".to_string(); //~ HELP consider removing this semicolon +} + +fn main() {} From da9c43a723a8bfe920483a550666f90de64830d5 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 3 Apr 2016 22:58:44 +0200 Subject: [PATCH 09/17] Autoderef when suggesting to call `(self.field)` Fixes #32128 --- src/librustc_typeck/check/method/suggest.rs | 54 ++++++++++++++------- src/test/compile-fail/issue-32128.rs | 25 ++++++++++ 2 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 src/test/compile-fail/issue-32128.rs diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 4d806bda48426..5b46880d5cfe4 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -14,7 +14,7 @@ use CrateCtxt; use astconv::AstConv; -use check::{self, FnCtxt}; +use check::{self, FnCtxt, UnresolvedTypeAction, autoderef}; use front::map as hir_map; use rustc::ty::{self, Ty, ToPolyTraitRef, ToPredicate, TypeFoldable}; use middle::cstore::{self, CrateStore}; @@ -22,6 +22,7 @@ use middle::def::Def; use middle::def_id::DefId; use middle::lang_items::FnOnceTraitLangItem; use rustc::ty::subst::Substs; +use rustc::ty::LvaluePreference; use rustc::traits::{Obligation, SelectionContext}; use util::nodemap::{FnvHashSet}; @@ -50,23 +51,40 @@ fn is_fn_ty<'a, 'tcx>(ty: &Ty<'tcx>, fcx: &FnCtxt<'a, 'tcx>, span: Span) -> bool if let Ok(fn_once_trait_did) = cx.lang_items.require(FnOnceTraitLangItem) { let infcx = fcx.infcx(); - infcx.probe(|_| { - let fn_once_substs = - Substs::new_trait(vec![infcx.next_ty_var()], - Vec::new(), - ty); - let trait_ref = - ty::TraitRef::new(fn_once_trait_did, - cx.mk_substs(fn_once_substs)); - let poly_trait_ref = trait_ref.to_poly_trait_ref(); - let obligation = Obligation::misc(span, - fcx.body_id, - poly_trait_ref - .to_predicate()); - let mut selcx = SelectionContext::new(infcx); - - return selcx.evaluate_obligation(&obligation) - }) + let (_, _, opt_is_fn) = autoderef(fcx, + span, + ty, + || None, + UnresolvedTypeAction::Ignore, + LvaluePreference::NoPreference, + |ty, _| { + infcx.probe(|_| { + let fn_once_substs = + Substs::new_trait(vec![infcx.next_ty_var()], + Vec::new(), + ty); + let trait_ref = + ty::TraitRef::new(fn_once_trait_did, + cx.mk_substs(fn_once_substs)); + let poly_trait_ref = trait_ref.to_poly_trait_ref(); + let obligation = Obligation::misc(span, + fcx.body_id, + poly_trait_ref + .to_predicate()); + let mut selcx = SelectionContext::new(infcx); + + if selcx.evaluate_obligation(&obligation) { + Some(true) + } else { + None + } + }) + }); + + match opt_is_fn { + Some(result) => result, + None => false, + } } else { false } diff --git a/src/test/compile-fail/issue-32128.rs b/src/test/compile-fail/issue-32128.rs new file mode 100644 index 0000000000000..fe7e66a2116eb --- /dev/null +++ b/src/test/compile-fail/issue-32128.rs @@ -0,0 +1,25 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Example { + example: Box i32> +} + +fn main() { + let demo = Example { + example: Box::new(|x| { + x + 1 + }) + }; + + demo.example(1); //~ ERROR no method named `example` + //~^ NOTE use `(demo.example)(...)` + // (demo.example)(1); +} From 8c2a8ae9cc81d86363e5c3180ce75e5925efe4a2 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 4 Apr 2016 10:32:37 +1200 Subject: [PATCH 10/17] Give better spans for SpanEnd errors --- src/libsyntax/errors/json.rs | 75 ++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 15 deletions(-) diff --git a/src/libsyntax/errors/json.rs b/src/libsyntax/errors/json.rs index 212a54447a861..f369582bc5c30 100644 --- a/src/libsyntax/errors/json.rs +++ b/src/libsyntax/errors/json.rs @@ -20,7 +20,7 @@ // FIXME spec the JSON output properly. -use codemap::{Span, MultiSpan, CodeMap}; +use codemap::{self, Span, MultiSpan, CodeMap}; use diagnostics::registry::Registry; use errors::{Level, DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion}; use errors::emitter::Emitter; @@ -197,8 +197,8 @@ impl DiagnosticSpan { fn from_render_span(rsp: &RenderSpan, je: &JsonEmitter) -> Vec { match *rsp { - // FIXME(#30701) handle Suggestion properly RenderSpan::FullSpan(ref msp) | + // FIXME(#30701) handle Suggestion properly RenderSpan::Suggestion(CodeSuggestion { ref msp, .. }) => { DiagnosticSpan::from_multispan(msp, je) } @@ -207,13 +207,13 @@ impl DiagnosticSpan { let end = je.cm.lookup_char_pos(span.hi); DiagnosticSpan { file_name: end.file.name.clone(), - byte_start: span.lo.0, + byte_start: span.hi.0, byte_end: span.hi.0, - line_start: 0, + line_start: end.line, line_end: end.line, - column_start: 0, + column_start: end.col.0 + 1, column_end: end.col.0 + 1, - text: DiagnosticSpanLine::from_span(span, je), + text: DiagnosticSpanLine::from_span_end(span, je), } }).collect() } @@ -237,25 +237,70 @@ impl DiagnosticSpan { } } -impl DiagnosticSpanLine { - fn from_span(span: &Span, je: &JsonEmitter) -> Vec { - let lines = match je.cm.span_to_lines(*span) { +macro_rules! get_lines_for_span { + ($span: ident, $je: ident) => { + match $je.cm.span_to_lines(*$span) { Ok(lines) => lines, Err(_) => { debug!("unprintable span"); return Vec::new(); } - }; + } + } +} + +impl DiagnosticSpanLine { + fn line_from_filemap(fm: &codemap::FileMap, + index: usize, + h_start: usize, + h_end: usize) + -> DiagnosticSpanLine { + DiagnosticSpanLine { + text: fm.get_line(index).unwrap().to_owned(), + highlight_start: h_start, + highlight_end: h_end, + } + } + + /// Create a list of DiagnosticSpanLines from span - each line with any part + /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the + /// `span` within the line. + fn from_span(span: &Span, je: &JsonEmitter) -> Vec { + let lines = get_lines_for_span!(span, je); let mut result = Vec::new(); let fm = &*lines.file; for line in &lines.lines { - result.push(DiagnosticSpanLine { - text: fm.get_line(line.line_index).unwrap().to_owned(), - highlight_start: line.start_col.0 + 1, - highlight_end: line.end_col.0 + 1, - }); + result.push(DiagnosticSpanLine::line_from_filemap(fm, + line.line_index, + line.start_col.0 + 1, + line.end_col.0 + 1)); + } + + result + } + + /// Create a list of DiagnosticSpanLines from span - the result covers all + /// of `span`, but the highlight is zero-length and at the end of `span`. + fn from_span_end(span: &Span, je: &JsonEmitter) -> Vec { + let lines = get_lines_for_span!(span, je); + + let mut result = Vec::new(); + let fm = &*lines.file; + + for (i, line) in lines.lines.iter().enumerate() { + // Invariant - CodeMap::span_to_lines will not return extra context + // lines - the last line returned is the last line of `span`. + let highlight = if i == lines.lines.len() - 1 { + (line.end_col.0 + 1, line.end_col.0 + 1) + } else { + (0, 0) + }; + result.push(DiagnosticSpanLine::line_from_filemap(fm, + line.line_index, + highlight.0, + highlight.1)); } result From a4e2933c6a26841aa22031c0e0edb7582e5ba657 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 4 Apr 2016 11:07:41 +1200 Subject: [PATCH 11/17] rustdoc: factor out function for getting inner html of highlighted source --- src/librustdoc/html/highlight.rs | 61 ++++++++++++++++++++++---------- src/librustdoc/html/markdown.rs | 6 ++-- src/librustdoc/html/render.rs | 8 ++--- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index b3cad90ccb551..7ca4703a2e185 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -17,22 +17,36 @@ use html::escape::Escape; use std::io; use std::io::prelude::*; -use syntax::parse::lexer; +use syntax::parse::lexer::{self, Reader}; use syntax::parse::token; use syntax::parse; -/// Highlights some source code, returning the HTML output. -pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String { +/// Highlights `src`, returning the HTML output. +pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>) -> String { debug!("highlighting: ================\n{}\n==============", src); let sess = parse::ParseSess::new(); let fm = sess.codemap().new_filemap("".to_string(), src.to_string()); let mut out = Vec::new(); - doit(&sess, - lexer::StringReader::new(&sess.span_diagnostic, fm), - class, - id, - &mut out).unwrap(); + write_header(class, id, &mut out).unwrap(); + write_source(&sess, + lexer::StringReader::new(&sess.span_diagnostic, fm), + &mut out).unwrap(); + write_footer(&mut out).unwrap(); + String::from_utf8_lossy(&out[..]).into_owned() +} + +/// Highlights `src`, returning the HTML output. Returns only the inner html to +/// be inserted into an element. C.f., `render_with_highlighting` which includes +/// an enclosing `
` block.
+pub fn render_inner_with_highlighting(src: &str) -> String {
+    let sess = parse::ParseSess::new();
+    let fm = sess.codemap().new_filemap("".to_string(), src.to_string());
+
+    let mut out = Vec::new();
+    write_source(&sess,
+                 lexer::StringReader::new(&sess.span_diagnostic, fm),
+                 &mut out).unwrap();
     String::from_utf8_lossy(&out[..]).into_owned()
 }
 
@@ -43,17 +57,10 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
 /// it's used. All source code emission is done as slices from the source map,
 /// not from the tokens themselves, in order to stay true to the original
 /// source.
-fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
-        class: Option<&str>, id: Option<&str>,
-        out: &mut Write) -> io::Result<()> {
-    use syntax::parse::lexer::Reader;
-
-    write!(out, "
 write!(out, "id='{}' ", id)?,
-        None => {}
-    }
-    write!(out, "class='rust {}'>\n", class.unwrap_or(""))?;
+fn write_source(sess: &parse::ParseSess,
+                mut lexer: lexer::StringReader,
+                out: &mut Write)
+                -> io::Result<()> {
     let mut is_attribute = false;
     let mut is_macro = false;
     let mut is_macro_nonterminal = false;
@@ -184,5 +191,21 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
         }
     }
 
+    Ok(())
+}
+
+fn write_header(class: Option<&str>,
+                id: Option<&str>,
+                out: &mut Write)
+                -> io::Result<()> {
+    write!(out, "
 write!(out, "id='{}' ", id)?,
+        None => {}
+    }
+    write!(out, "class='rust {}'>\n", class.unwrap_or(""))
+}
+
+fn write_footer(out: &mut Write) -> io::Result<()> {
     write!(out, "
\n") } diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index e7304b6951083..3baf22b38ef68 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -262,9 +262,9 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result { &Default::default()); s.push_str(&format!("{}", Escape(&test))); }); - s.push_str(&highlight::highlight(&text, - Some("rust-example-rendered"), - None)); + s.push_str(&highlight::render_with_highlighting(&text, + Some("rust-example-rendered"), + None)); let output = CString::new(s).unwrap(); hoedown_buffer_puts(ob, output.as_ptr()); }) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 0f436efd70b2e..84ccd4ac661d2 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2661,16 +2661,16 @@ impl<'a> fmt::Display for Source<'a> { write!(fmt, "{0:1$}\n", i, cols)?; } write!(fmt, "
")?; - write!(fmt, "{}", highlight::highlight(s, None, None))?; + write!(fmt, "{}", highlight::render_with_highlighting(s, None, None))?; Ok(()) } } fn item_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, t: &clean::Macro) -> fmt::Result { - w.write_str(&highlight::highlight(&t.source, - Some("macro"), - None))?; + w.write_str(&highlight::render_with_highlighting(&t.source, + Some("macro"), + None))?; render_stability_since_raw(w, it.stable_since(), None)?; document(w, cx, it) } From 99b6247166f8a0119b446e8877f5fca9b04187b0 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 4 Apr 2016 13:53:04 +0200 Subject: [PATCH 12/17] Beef up test --- src/test/compile-fail/consider-removing-last-semi.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/compile-fail/consider-removing-last-semi.rs b/src/test/compile-fail/consider-removing-last-semi.rs index a25f0b17335ed..02148a138c9d9 100644 --- a/src/test/compile-fail/consider-removing-last-semi.rs +++ b/src/test/compile-fail/consider-removing-last-semi.rs @@ -14,4 +14,10 @@ fn f() -> String { //~ ERROR E0269 "bla".to_string(); //~ HELP consider removing this semicolon } +fn g() -> String { //~ ERROR E0269 + //~^ HELP detailed explanation + "this won't work".to_string(); + "removeme".to_string(); //~ HELP consider removing this semicolon +} + fn main() {} From 580c5f92d1050543f5e69f842ef8c4899ab2540e Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 4 Apr 2016 14:10:03 +0200 Subject: [PATCH 13/17] use `unwrap_or` --- src/librustc_typeck/check/method/suggest.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 5b46880d5cfe4..fcd5c4ea94fde 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -81,10 +81,7 @@ fn is_fn_ty<'a, 'tcx>(ty: &Ty<'tcx>, fcx: &FnCtxt<'a, 'tcx>, span: Span) -> bool }) }); - match opt_is_fn { - Some(result) => result, - None => false, - } + opt_is_fn.unwrap_or(false) } else { false } From 1ea98a8b7060579e69c6269cdfb1cc527093d561 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 4 Apr 2016 15:08:38 +0200 Subject: [PATCH 14/17] Just use Some(()) instead --- src/librustc_typeck/check/method/suggest.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index fcd5c4ea94fde..32b5a63817ea5 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -74,14 +74,14 @@ fn is_fn_ty<'a, 'tcx>(ty: &Ty<'tcx>, fcx: &FnCtxt<'a, 'tcx>, span: Span) -> bool let mut selcx = SelectionContext::new(infcx); if selcx.evaluate_obligation(&obligation) { - Some(true) + Some(()) } else { None } }) }); - opt_is_fn.unwrap_or(false) + opt_is_fn.is_some() } else { false } From 86071aca3dd729c77d238d449d2c61b2de0b8425 Mon Sep 17 00:00:00 2001 From: Masood Malekghassemi Date: Mon, 4 Apr 2016 12:41:05 -0700 Subject: [PATCH 15/17] Address nits --- src/librustc/infer/mod.rs | 23 +++++++++++---------- src/librustc/traits/fulfill.rs | 2 +- src/librustc/traits/project.rs | 8 +++---- src/librustc/traits/select.rs | 16 +++++++------- src/librustc_driver/test.rs | 6 +++--- src/librustc_mir/transform/type_check.rs | 4 ++-- src/librustc_typeck/check/_match.rs | 2 +- src/librustc_typeck/check/coercion.rs | 12 +++++------ src/librustc_typeck/check/compare_method.rs | 2 +- src/librustc_typeck/check/demand.rs | 4 ++-- src/librustc_typeck/check/method/probe.rs | 2 +- src/librustc_typeck/check/mod.rs | 14 ++++++------- src/librustc_typeck/check/regionck.rs | 2 +- 13 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index 6e97cdef3d7ca..3bf618202e9b4 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -579,6 +579,12 @@ pub fn drain_fulfillment_cx<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>, Ok(infcx.tcx.erase_regions(&result)) } +impl<'tcx, T> InferOk<'tcx, T> { + fn unit(self) -> InferOk<'tcx, ()> { + InferOk { value: (), obligations: self.obligations } + } +} + impl<'a, 'tcx> InferCtxt<'a, 'tcx> { pub fn projection_mode(&self) -> ProjectionMode { self.projection_mode @@ -851,8 +857,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { debug!("sub_types({:?} <: {:?})", a, b); self.commit_if_ok(|_| { let trace = TypeTrace::types(origin, a_is_expected, a, b); - self.sub(a_is_expected, trace, &a, &b) - .map(|InferOk { obligations, .. }| InferOk { value: (), obligations: obligations }) + self.sub(a_is_expected, trace, &a, &b).map(|ok| ok.unit()) }) } @@ -865,8 +870,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { { self.commit_if_ok(|_| { let trace = TypeTrace::types(origin, a_is_expected, a, b); - self.equate(a_is_expected, trace, &a, &b) - .map(|InferOk { obligations, .. }| InferOk { value: (), obligations: obligations }) + self.equate(a_is_expected, trace, &a, &b).map(|ok| ok.unit()) }) } @@ -885,8 +889,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { origin: origin, values: TraitRefs(expected_found(a_is_expected, a.clone(), b.clone())) }; - self.equate(a_is_expected, trace, &a, &b) - .map(|InferOk { obligations, .. }| InferOk { value: (), obligations: obligations }) + self.equate(a_is_expected, trace, &a, &b).map(|ok| ok.unit()) }) } @@ -905,8 +908,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { origin: origin, values: PolyTraitRefs(expected_found(a_is_expected, a.clone(), b.clone())) }; - self.sub(a_is_expected, trace, &a, &b) - .map(|InferOk { obligations, .. }| InferOk { value: (), obligations: obligations }) + self.sub(a_is_expected, trace, &a, &b).map(|ok| ok.unit()) }) } @@ -955,9 +957,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { let (ty::EquatePredicate(a, b), skol_map) = self.skolemize_late_bound_regions(predicate, snapshot); let origin = TypeOrigin::EquatePredicate(span); - let InferOk { obligations, .. } = mk_eqty(self, false, origin, a, b)?; - self.leak_check(&skol_map, snapshot) - .map(|_| InferOk { value: (), obligations: obligations }) + let eqty_ok = mk_eqty(self, false, origin, a, b)?; + self.leak_check(&skol_map, snapshot).map(|_| eqty_ok.unit()) }) } diff --git a/src/librustc/traits/fulfill.rs b/src/librustc/traits/fulfill.rs index 68173bc9ea513..46323cdf77ee4 100644 --- a/src/librustc/traits/fulfill.rs +++ b/src/librustc/traits/fulfill.rs @@ -527,7 +527,7 @@ fn process_predicate1<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>, ty::Predicate::Equate(ref binder) => { match selcx.infcx().equality_predicate(obligation.cause.span, binder) { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); Ok(Some(Vec::new())) }, diff --git a/src/librustc/traits/project.rs b/src/librustc/traits/project.rs index 85fe457c75e8c..71eb0a227b4ed 100644 --- a/src/librustc/traits/project.rs +++ b/src/librustc/traits/project.rs @@ -233,7 +233,7 @@ fn project_and_unify_type<'cx,'tcx>( let origin = TypeOrigin::RelateOutputImplTypes(obligation.cause.span); match infer::mk_eqty(infcx, true, origin, normalized_ty, obligation.predicate.ty) { Ok(InferOk { obligations: inferred_obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(inferred_obligations.is_empty()); Ok(Some(obligations)) }, @@ -283,7 +283,7 @@ fn consider_unification_despite_ambiguity<'cx,'tcx>(selcx: &mut SelectionContext let obligation_ty = obligation.predicate.ty; match infer::mk_eqty(infcx, true, origin, obligation_ty, ret_type) { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); } Err(_) => { /* ignore errors */ } @@ -837,7 +837,7 @@ fn assemble_candidates_from_predicates<'cx,'tcx,I>( origin, data_poly_trait_ref, obligation_poly_trait_ref) - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) .is_ok() }); @@ -1093,7 +1093,7 @@ fn confirm_param_env_candidate<'cx,'tcx>( obligation.predicate.trait_ref.clone(), projection.projection_ty.trait_ref.clone()) { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); } Err(e) => { diff --git a/src/librustc/traits/select.rs b/src/librustc/traits/select.rs index 013c75bf8d2bb..d18f8e9dc53d0 100644 --- a/src/librustc/traits/select.rs +++ b/src/librustc/traits/select.rs @@ -485,7 +485,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // does this code ever run? match self.infcx.equality_predicate(obligation.cause.span, p) { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); EvaluatedToOk }, @@ -1190,7 +1190,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { trait_bound.clone(), ty::Binder(skol_trait_ref.clone())) { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); } Err(_) => { return false; } @@ -2505,7 +2505,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { origin, expected_trait_ref.clone(), obligation_trait_ref.clone()) - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) .map_err(|e| OutputTypeParameterMismatch(expected_trait_ref, obligation_trait_ref, e)) } @@ -2541,7 +2541,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let InferOk { obligations, .. } = self.infcx.sub_types(false, origin, new_trait, target) .map_err(|_| Unimplemented)?; - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); // Register one obligation for 'a: 'b. @@ -2608,7 +2608,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let InferOk { obligations, .. } = self.infcx.sub_types(false, origin, a, b) .map_err(|_| Unimplemented)?; - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); } @@ -2668,7 +2668,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let InferOk { obligations, .. } = self.infcx.sub_types(false, origin, new_struct, target) .map_err(|_| Unimplemented)?; - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); // Construct the nested Field: Unsize> predicate. @@ -2764,7 +2764,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { debug!("match_impl: failed eq_trait_refs due to `{}`", e); () })?; - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); if let Err(e) = self.infcx.leak_check(&skol_map, snapshot) { @@ -2832,7 +2832,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { origin, poly_trait_ref, obligation.predicate.to_poly_trait_ref()) - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) .map_err(|_| ()) } diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index 00569d50cbb9c..ce0d42203b987 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -375,7 +375,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> { pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) { match self.sub(&t1, &t2) { Ok(InferOk { obligations, .. }) => { - // FIXME once obligations are being propagated, assert the right thing. + // FIXME(#32730) once obligations are being propagated, assert the right thing. assert!(obligations.is_empty()); } Err(ref e) => { @@ -399,7 +399,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> { pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) { match self.lub(&t1, &t2) { Ok(InferOk { obligations, value: t }) => { - // FIXME once obligations are being propagated, assert the right thing. + // FIXME(#32730) once obligations are being propagated, assert the right thing. assert!(obligations.is_empty()); self.assert_eq(t, t_lub); @@ -418,7 +418,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> { panic!("unexpected error computing LUB: {:?}", e) } Ok(InferOk { obligations, value: t }) => { - // FIXME once obligations are being propagated, assert the right thing. + // FIXME(#32730) once obligations are being propagated, assert the right thing. assert!(obligations.is_empty()); self.assert_eq(t, t_glb); diff --git a/src/librustc_mir/transform/type_check.rs b/src/librustc_mir/transform/type_check.rs index 3f85a3e1d4618..ce8ede7f4b959 100644 --- a/src/librustc_mir/transform/type_check.rs +++ b/src/librustc_mir/transform/type_check.rs @@ -338,7 +338,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { { infer::mk_subty(self.infcx, false, infer::TypeOrigin::Misc(span), sup, sub) - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } @@ -347,7 +347,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { { infer::mk_eqty(self.infcx, false, infer::TypeOrigin::Misc(span), a, b) - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index dcbfa2bb79b6c..e359329ebcf7f 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -534,7 +534,7 @@ pub fn check_match<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, let result = if is_if_let_fallback { fcx.infcx().eq_types(true, origin, arm_ty, result_ty) .map(|InferOk { obligations, .. }| { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); arm_ty }) diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index a4d8e2ae04938..a9849e93578c8 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -119,14 +119,14 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { if self.use_lub { infcx.lub(false, trace, &a, &b) .map(|InferOk { value, obligations }| { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); value }) } else { infcx.sub(false, trace, &a, &b) .map(|InferOk { value, obligations }| { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); value }) @@ -668,7 +668,7 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>, // The signature must always match. let fty = fcx.infcx().lub(true, trace.clone(), a_fty, b_fty) .map(|InferOk { value, obligations }| { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); value })?; @@ -678,7 +678,7 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>, let substs = fcx.infcx().commit_if_ok(|_| { fcx.infcx().lub(true, trace.clone(), a_substs, b_substs) .map(|InferOk { value, obligations }| { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); value }) @@ -746,7 +746,7 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>, return fcx.infcx().commit_if_ok(|_| { fcx.infcx().lub(true, trace.clone(), &prev_ty, &new_ty) .map(|InferOk { value, obligations }| { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); value }) @@ -763,7 +763,7 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>, fcx.infcx().commit_if_ok(|_| { fcx.infcx().lub(true, trace, &prev_ty, &new_ty) .map(|InferOk { value, obligations }| { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); value }) diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs index 5f49b0335d929..3c12ab8d59840 100644 --- a/src/librustc_typeck/check/compare_method.rs +++ b/src/librustc_typeck/check/compare_method.rs @@ -476,7 +476,7 @@ pub fn compare_const_impl<'tcx>(tcx: &TyCtxt<'tcx>, match err { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()) } Err(terr) => { diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 75f6b11a22931..bc2ef9aafee59 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -23,7 +23,7 @@ pub fn suptype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span, let origin = TypeOrigin::Misc(sp); match fcx.infcx().sub_types(false, origin, actual, expected) { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); }, Err(e) => { @@ -37,7 +37,7 @@ pub fn eqtype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span, let origin = TypeOrigin::Misc(sp); match fcx.infcx().eq_types(false, origin, actual, expected) { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); }, Err(e) => { diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 44dbcd051e201..7e487c1f717f7 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -1134,7 +1134,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> { fn make_sub_ty(&self, sub: Ty<'tcx>, sup: Ty<'tcx>) -> infer::UnitResult<'tcx> { self.infcx().sub_types(false, TypeOrigin::Misc(DUMMY_SP), sub, sup) - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 625e59c6e3c63..da93180c600dd 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1628,7 +1628,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { sup: Ty<'tcx>) -> Result<(), TypeError<'tcx>> { infer::mk_subty(self.infcx(), a_is_expected, origin, sub, sup) - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } @@ -1639,7 +1639,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { sup: Ty<'tcx>) -> Result<(), TypeError<'tcx>> { infer::mk_eqty(self.infcx(), a_is_expected, origin, sub, sup) - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations .map(|InferOk { obligations, .. }| assert!(obligations.is_empty())) } @@ -1920,7 +1920,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { TypeOrigin::Misc(default.origin_span), ty, default.ty) { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()) }, Err(_) => { @@ -2015,7 +2015,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { match infer::mk_eqty(self.infcx(), false, TypeOrigin::Misc(default.origin_span), ty, default.ty) { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations Ok(InferOk { obligations, .. }) => assert!(obligations.is_empty()), Err(_) => { result = Some(default); @@ -2784,7 +2784,7 @@ fn expected_types_for_fn_args<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, // FIXME(#15760) can't use try! here, FromError doesn't default // to identity so the resulting type is not constrained. match ures { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations Ok(InferOk { obligations, .. }) => assert!(obligations.is_empty()), Err(e) => return Err(e), } @@ -2915,7 +2915,7 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, let trace = TypeTrace::types(origin, true, then_ty, else_ty); fcx.infcx().lub(true, trace, &then_ty, &else_ty) .map(|InferOk { value, obligations }| { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); value }) @@ -2927,7 +2927,7 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, (origin, unit, then_ty, fcx.infcx().eq_types(true, origin, unit, then_ty) .map(|InferOk { obligations, .. }| { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); unit })) diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index 515a898699e0c..dae14c3329689 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -1847,7 +1847,7 @@ fn declared_projection_bounds_from_trait<'a,'tcx>(rcx: &Rcx<'a, 'tcx>, // check whether this predicate applies to our current projection match infer::mk_eqty(infcx, false, TypeOrigin::Misc(span), ty, outlives.0) { Ok(InferOk { obligations, .. }) => { - // FIXME(#????) propagate obligations + // FIXME(#32730) propagate obligations assert!(obligations.is_empty()); Ok(outlives.1) } From d69a5982de0aa65bac47954fb75ef25df8350e6f Mon Sep 17 00:00:00 2001 From: Dave Huseby Date: Sat, 2 Apr 2016 05:08:19 -0700 Subject: [PATCH 16/17] adds dragonflybsd and freebsd to snapshots.txt --- src/snapshots.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/snapshots.txt b/src/snapshots.txt index d1a2ab464cba7..ce7ecb68ac822 100644 --- a/src/snapshots.txt +++ b/src/snapshots.txt @@ -9,6 +9,7 @@ S 2016-03-18 235d774 freebsd-x86_64 390b9a9f60f3d0d6a52c04d939a0355f572d03b3 S 2016-02-17 4d3eebf + dragonfly-x86_64 765bb5820ad406e966ec0ac51c8070b656459b02 linux-i386 5f194aa7628c0703f0fd48adc4ec7f3cc64b98c7 linux-x86_64 d29b7607d13d64078b6324aec82926fb493f59ba macos-i386 4c8e42dd649e247f3576bf9dfa273327b4907f9c From 6c73134fc740a09e22db6a3c2438339c155ef577 Mon Sep 17 00:00:00 2001 From: vlastachu Date: Sun, 20 Mar 2016 03:04:12 +0300 Subject: [PATCH 17/17] Fixes bug which accepting using `super` in use statemet. Issue: #32225 --- src/librustc/lint/builtin.rs | 9 +++++++- src/librustc_lint/lib.rs | 4 ++++ src/librustc_resolve/build_reduced_graph.rs | 20 +++++++++++++++-- src/libsyntax/parse/parser.rs | 10 ++++----- src/libsyntax/parse/token.rs | 2 +- .../compile-fail/use-super-global-path.rs | 22 +++++++++++++++++++ 6 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/test/compile-fail/use-super-global-path.rs diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 879b894562092..1573d0c4292ac 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -179,6 +179,12 @@ declare_lint! { "lints that have been renamed or removed" } +declare_lint! { + pub SUPER_OR_SELF_IN_GLOBAL_PATH, + Warn, + "detects super or self keywords at the beginning of global path" +} + /// Does nothing as a lint pass, but registers some `Lint`s /// which are used by other parts of the compiler. #[derive(Copy, Clone)] @@ -213,7 +219,8 @@ impl LintPass for HardwiredLints { RAW_POINTER_DERIVE, TRANSMUTE_FROM_FN_ITEM_TYPES, OVERLAPPING_INHERENT_IMPLS, - RENAMED_AND_REMOVED_LINTS + RENAMED_AND_REMOVED_LINTS, + SUPER_OR_SELF_IN_GLOBAL_PATH ) } } diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 6e3a961cacae7..0780e4cd04834 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -167,6 +167,10 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { id: LintId::of(INVALID_TYPE_PARAM_DEFAULT), reference: "PR 30742 ", }, + FutureIncompatibleInfo { + id: LintId::of(SUPER_OR_SELF_IN_GLOBAL_PATH), + reference: "PR #32403 ", + }, FutureIncompatibleInfo { id: LintId::of(MATCH_OF_UNIT_VARIANT_VIA_PAREN_DOTDOT), reference: "RFC 218 Resolver<'b, 'tcx> { // Extract and intern the module part of the path. For // globs and lists, the path is found directly in the AST; // for simple paths we have to munge the path a little. - let module_path = match view_path.node { + let is_global; + let module_path: Vec = match view_path.node { ViewPathSimple(_, ref full_path) => { + is_global = full_path.global; full_path.segments .split_last() .unwrap() @@ -130,6 +133,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> { ViewPathGlob(ref module_ident_path) | ViewPathList(ref module_ident_path, _) => { + is_global = module_ident_path.global; module_ident_path.segments .iter() .map(|seg| seg.identifier.name) @@ -137,6 +141,18 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> { } }; + // Checking for special identifiers in path + // prevent `self` or `super` at beginning of global path + if is_global && (module_path.first() == Some(&SELF_KEYWORD_NAME) || + module_path.first() == Some(&SUPER_KEYWORD_NAME)) { + self.session.add_lint( + lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH, + item.id, + item.span, + format!("expected identifier, found keyword `{}`", + module_path.first().unwrap().as_str())); + } + // Build up the import directives. let is_prelude = item.attrs.iter().any(|attr| { attr.name() == special_idents::prelude_import.name.as_str() diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9027a5b107452..75916b87c129d 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -6124,7 +6124,7 @@ impl<'a> Parser<'a> { // Allow a leading :: because the paths are absolute either way. // This occurs with "use $crate::..." in macros. - self.eat(&token::ModSep); + let is_global = self.eat(&token::ModSep); if self.check(&token::OpenDelim(token::Brace)) { // use {foo,bar} @@ -6135,7 +6135,7 @@ impl<'a> Parser<'a> { |p| p.parse_path_list_item())?; let path = ast::Path { span: mk_sp(lo, self.span.hi), - global: false, + global: is_global, segments: Vec::new() }; return Ok(P(spanned(lo, self.span.hi, ViewPathList(path, idents)))); @@ -6164,7 +6164,7 @@ impl<'a> Parser<'a> { )?; let path = ast::Path { span: mk_sp(lo, self.span.hi), - global: false, + global: is_global, segments: path.into_iter().map(|identifier| { ast::PathSegment { identifier: identifier, @@ -6180,7 +6180,7 @@ impl<'a> Parser<'a> { self.bump(); let path = ast::Path { span: mk_sp(lo, self.span.hi), - global: false, + global: is_global, segments: path.into_iter().map(|identifier| { ast::PathSegment { identifier: identifier, @@ -6203,7 +6203,7 @@ impl<'a> Parser<'a> { let mut rename_to = path[path.len() - 1]; let path = ast::Path { span: mk_sp(lo, self.last_span.hi), - global: false, + global: is_global, segments: path.into_iter().map(|identifier| { ast::PathSegment { identifier: identifier, diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index a02a10aa0035a..16417ac004461 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -514,7 +514,7 @@ macro_rules! declare_special_idents_and_keywords {( // If the special idents get renumbered, remember to modify these two as appropriate pub const SELF_KEYWORD_NAME: ast::Name = ast::Name(SELF_KEYWORD_NAME_NUM); const STATIC_KEYWORD_NAME: ast::Name = ast::Name(STATIC_KEYWORD_NAME_NUM); -const SUPER_KEYWORD_NAME: ast::Name = ast::Name(SUPER_KEYWORD_NAME_NUM); +pub const SUPER_KEYWORD_NAME: ast::Name = ast::Name(SUPER_KEYWORD_NAME_NUM); const SELF_TYPE_KEYWORD_NAME: ast::Name = ast::Name(SELF_TYPE_KEYWORD_NAME_NUM); pub const SELF_KEYWORD_NAME_NUM: u32 = 1; diff --git a/src/test/compile-fail/use-super-global-path.rs b/src/test/compile-fail/use-super-global-path.rs new file mode 100644 index 0000000000000..d721d428f29f5 --- /dev/null +++ b/src/test/compile-fail/use-super-global-path.rs @@ -0,0 +1,22 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(rustc_attrs)] + +mod foo { + pub fn g() { + use ::super::main; //~ WARN expected identifier, found keyword `super` + //~^ WARN this was previously accepted by the compiler but is being phased out + main(); + } +} + +#[rustc_error] +fn main() { foo::g(); } //~ ERROR compilation successful