diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index 3b41b220ccd4cc1acee071c0d93ce72ba65d9bac..51bb09e4c54ba29bebcf1a8356bb01a9a6fc9587 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -693,6 +693,61 @@ pub fn instantiate_mono_trait_ref( ) } + fn instantiate_poly_trait_ref_inner( + &self, + hir_id: hir::HirId, + span: Span, + binding_span: Option, + constness: ty::BoundConstness, + bounds: &mut Bounds<'tcx>, + speculative: bool, + trait_ref_span: Span, + trait_def_id: DefId, + trait_segment: &hir::PathSegment<'_>, + args: &GenericArgs<'_>, + infer_args: bool, + self_ty: Ty<'tcx>, + ) -> GenericArgCountResult { + let (substs, arg_count) = self.create_substs_for_ast_path( + trait_ref_span, + trait_def_id, + &[], + trait_segment, + args, + infer_args, + Some(self_ty), + ); + + let tcx = self.tcx(); + let bound_vars = tcx.late_bound_vars(hir_id); + debug!(?bound_vars); + + let assoc_bindings = self.create_assoc_bindings_for_generic_args(args); + + let poly_trait_ref = + ty::Binder::bind_with_vars(ty::TraitRef::new(trait_def_id, substs), bound_vars); + + debug!(?poly_trait_ref, ?assoc_bindings); + bounds.trait_bounds.push((poly_trait_ref, span, constness)); + + let mut dup_bindings = FxHashMap::default(); + for binding in &assoc_bindings { + // Specify type to assert that error was already reported in `Err` case. + let _: Result<_, ErrorReported> = self.add_predicates_for_ast_type_binding( + hir_id, + poly_trait_ref, + binding, + bounds, + speculative, + &mut dup_bindings, + binding_span.unwrap_or(binding.span), + ); + // Okay to ignore `Err` because of `ErrorReported` (see above). + } + + arg_count + } + /// Given a trait bound like `Debug`, applies that trait bound the given self-type to construct /// a full trait reference. The resulting trait reference is returned. This may also generate /// auxiliary bounds, which are added to `bounds`. @@ -713,7 +768,7 @@ pub fn instantiate_mono_trait_ref( /// `Bar<'a>`. The returned poly-trait-ref will have this binder instantiated explicitly, /// however. #[tracing::instrument(level = "debug", skip(self, span, constness, bounds, speculative))] - pub fn instantiate_poly_trait_ref( + pub(crate) fn instantiate_poly_trait_ref( &self, trait_ref: &hir::TraitRef<'_>, span: Span, @@ -722,48 +777,34 @@ pub fn instantiate_poly_trait_ref( bounds: &mut Bounds<'tcx>, speculative: bool, ) -> GenericArgCountResult { + let hir_id = trait_ref.hir_ref_id; + let binding_span = None; + let trait_ref_span = trait_ref.path.span; let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise()); + let trait_segment = trait_ref.path.segments.last().unwrap(); + let args = trait_segment.args(); + let infer_args = trait_segment.infer_args; self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1); + self.complain_about_internal_fn_trait(span, trait_def_id, trait_segment); - let tcx = self.tcx(); - let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id); - debug!(?bound_vars); - - let (substs, arg_count) = self.create_substs_for_ast_trait_ref( - trait_ref.path.span, + self.instantiate_poly_trait_ref_inner( + hir_id, + span, + binding_span, + constness, + bounds, + speculative, + trait_ref_span, trait_def_id, + trait_segment, + args, + infer_args, self_ty, - trait_ref.path.segments.last().unwrap(), - ); - let assoc_bindings = self - .create_assoc_bindings_for_generic_args(trait_ref.path.segments.last().unwrap().args()); - - let poly_trait_ref = - ty::Binder::bind_with_vars(ty::TraitRef::new(trait_def_id, substs), bound_vars); - - debug!(?poly_trait_ref, ?assoc_bindings); - bounds.trait_bounds.push((poly_trait_ref, span, constness)); - - let mut dup_bindings = FxHashMap::default(); - for binding in &assoc_bindings { - // Specify type to assert that error was already reported in `Err` case. - let _: Result<_, ErrorReported> = self.add_predicates_for_ast_type_binding( - trait_ref.hir_ref_id, - poly_trait_ref, - binding, - bounds, - speculative, - &mut dup_bindings, - binding.span, - ); - // Okay to ignore `Err` because of `ErrorReported` (see above). - } - - arg_count + ) } - pub fn instantiate_lang_item_trait_ref( + pub(crate) fn instantiate_lang_item_trait_ref( &self, lang_item: hir::LangItem, span: Span, @@ -772,36 +813,28 @@ pub fn instantiate_lang_item_trait_ref( self_ty: Ty<'tcx>, bounds: &mut Bounds<'tcx>, ) { + let binding_span = Some(span); + let constness = ty::BoundConstness::NotConst; + let speculative = false; + let trait_ref_span = span; let trait_def_id = self.tcx().require_lang_item(lang_item, Some(span)); + let trait_segment = &hir::PathSegment::invalid(); + let infer_args = false; - let (substs, _) = self.create_substs_for_ast_path( + self.instantiate_poly_trait_ref_inner( + hir_id, span, + binding_span, + constness, + bounds, + speculative, + trait_ref_span, trait_def_id, - &[], - &hir::PathSegment::invalid(), + trait_segment, args, - false, - Some(self_ty), + infer_args, + self_ty, ); - let assoc_bindings = self.create_assoc_bindings_for_generic_args(args); - let tcx = self.tcx(); - let bound_vars = tcx.late_bound_vars(hir_id); - let poly_trait_ref = - ty::Binder::bind_with_vars(ty::TraitRef::new(trait_def_id, substs), bound_vars); - bounds.trait_bounds.push((poly_trait_ref, span, ty::BoundConstness::NotConst)); - - let mut dup_bindings = FxHashMap::default(); - for binding in assoc_bindings { - let _: Result<_, ErrorReported> = self.add_predicates_for_ast_type_binding( - hir_id, - poly_trait_ref, - &binding, - bounds, - false, - &mut dup_bindings, - span, - ); - } } fn ast_path_to_mono_trait_ref( @@ -935,45 +968,43 @@ pub(crate) fn add_implicitly_sized<'hir>( /// **A note on binders:** there is an implied binder around /// `param_ty` and `ast_bounds`. See `instantiate_poly_trait_ref` /// for more details. - #[tracing::instrument(level = "debug", skip(self, bounds))] - fn add_bounds( + #[tracing::instrument(level = "debug", skip(self, ast_bounds, bounds))] + pub(crate) fn add_bounds<'hir, I: Iterator>>( &self, param_ty: Ty<'tcx>, - ast_bounds: &[hir::GenericBound<'_>], + ast_bounds: I, bounds: &mut Bounds<'tcx>, bound_vars: &'tcx ty::List, ) { for ast_bound in ast_bounds { - match *ast_bound { - hir::GenericBound::Trait(ref b, hir::TraitBoundModifier::None) => { - self.instantiate_poly_trait_ref( - &b.trait_ref, - b.span, - ty::BoundConstness::NotConst, + match ast_bound { + hir::GenericBound::Trait(poly_trait_ref, modifier) => { + let constness = match modifier { + hir::TraitBoundModifier::MaybeConst => ty::BoundConstness::ConstIfConst, + hir::TraitBoundModifier::None => ty::BoundConstness::NotConst, + hir::TraitBoundModifier::Maybe => continue, + }; + + let _ = self.instantiate_poly_trait_ref( + &poly_trait_ref.trait_ref, + poly_trait_ref.span, + constness, param_ty, bounds, false, ); } - hir::GenericBound::Trait(ref b, hir::TraitBoundModifier::MaybeConst) => { - self.instantiate_poly_trait_ref( - &b.trait_ref, - b.span, - ty::BoundConstness::ConstIfConst, - param_ty, - bounds, - false, + &hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => { + self.instantiate_lang_item_trait_ref( + lang_item, span, hir_id, args, param_ty, bounds, ); } - hir::GenericBound::Trait(_, hir::TraitBoundModifier::Maybe) => {} - hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => self - .instantiate_lang_item_trait_ref( - lang_item, span, hir_id, args, param_ty, bounds, - ), - hir::GenericBound::Outlives(ref l) => bounds.region_bounds.push(( - ty::Binder::bind_with_vars(self.ast_region_to_region(l, None), bound_vars), - l.span, - )), + hir::GenericBound::Outlives(lifetime) => { + let region = self.ast_region_to_region(lifetime, None); + bounds + .region_bounds + .push((ty::Binder::bind_with_vars(region, bound_vars), lifetime.span)); + } } } } @@ -1032,7 +1063,7 @@ fn compute_bounds_inner( ) -> Bounds<'tcx> { let mut bounds = Bounds::default(); - self.add_bounds(param_ty, ast_bounds, &mut bounds, ty::List::empty()); + self.add_bounds(param_ty, ast_bounds.iter(), &mut bounds, ty::List::empty()); bounds } @@ -1224,7 +1255,7 @@ fn add_predicates_for_ast_type_binding( // Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty` // parameter to have a skipped binder. let param_ty = tcx.mk_ty(ty::Projection(projection_ty.skip_binder())); - self.add_bounds(param_ty, ast_bounds, bounds, candidate.bound_vars()); + self.add_bounds(param_ty, ast_bounds.iter(), bounds, candidate.bound_vars()); } } Ok(()) diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index f02ed3304d63af5f1493c236ad2305985ec93b1c..3688fa05e038eb2948da4152ded88e55e01aabd5 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -2220,62 +2220,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP } } - for bound in bound_pred.bounds.iter() { - match bound { - hir::GenericBound::Trait(poly_trait_ref, modifier) => { - let constness = match modifier { - hir::TraitBoundModifier::None => ty::BoundConstness::NotConst, - hir::TraitBoundModifier::MaybeConst => { - ty::BoundConstness::ConstIfConst - } - // We ignore `where T: ?Sized`, it is already part of - // type parameter `T`. - hir::TraitBoundModifier::Maybe => continue, - }; - - let mut bounds = Bounds::default(); - let _ = >::instantiate_poly_trait_ref( - &icx, - &poly_trait_ref.trait_ref, - poly_trait_ref.span, - constness, - ty, - &mut bounds, - false, - ); - predicates.extend(bounds.predicates(tcx, ty)); - } - - &hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => { - let mut bounds = Bounds::default(); - >::instantiate_lang_item_trait_ref( - &icx, - lang_item, - span, - hir_id, - args, - ty, - &mut bounds, - ); - predicates.extend(bounds.predicates(tcx, ty)); - } - - hir::GenericBound::Outlives(lifetime) => { - let region = - >::ast_region_to_region(&icx, lifetime, None); - predicates.insert(( - ty::Binder::bind_with_vars( - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate( - ty, region, - )), - bound_vars, - ) - .to_predicate(tcx), - lifetime.span, - )); - } - } - } + let mut bounds = Bounds::default(); + >::add_bounds( + &icx, + ty, + bound_pred.bounds.iter(), + &mut bounds, + bound_vars, + ); + predicates.extend(bounds.predicates(tcx, ty)); } hir::WherePredicate::RegionPredicate(region_pred) => { @@ -2489,44 +2442,14 @@ fn predicates_from_bound<'tcx>( param_ty: Ty<'tcx>, bound: &'tcx hir::GenericBound<'tcx>, ) -> Vec<(ty::Predicate<'tcx>, Span)> { - match *bound { - hir::GenericBound::Trait(ref tr, modifier) => { - let constness = match modifier { - hir::TraitBoundModifier::Maybe => return vec![], - hir::TraitBoundModifier::MaybeConst => ty::BoundConstness::ConstIfConst, - hir::TraitBoundModifier::None => ty::BoundConstness::NotConst, - }; - - let mut bounds = Bounds::default(); - let _ = astconv.instantiate_poly_trait_ref( - &tr.trait_ref, - tr.span, - constness, - param_ty, - &mut bounds, - false, - ); - bounds.predicates(astconv.tcx(), param_ty) - } - hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => { - let mut bounds = Bounds::default(); - astconv.instantiate_lang_item_trait_ref( - lang_item, - span, - hir_id, - args, - param_ty, - &mut bounds, - ); - bounds.predicates(astconv.tcx(), param_ty) - } - hir::GenericBound::Outlives(ref lifetime) => { - let region = astconv.ast_region_to_region(lifetime, None); - let pred = ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(param_ty, region)) - .to_predicate(astconv.tcx()); - vec![(pred, lifetime.span)] - } - } + let mut bounds = Bounds::default(); + astconv.add_bounds( + param_ty, + std::array::IntoIter::new([bound]), + &mut bounds, + ty::List::empty(), + ); + bounds.predicates(astconv.tcx(), param_ty) } fn compute_sig_of_foreign_fn_decl<'tcx>( diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 71348b3eb65ff022d53f4d6b9c82d2f3da9f53ef..a95300217ab75edab1c4d3125bbf9d70dff6f95f 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -129,7 +129,6 @@ impl Clean for hir::GenericBound<'_> { fn clean(&self, cx: &mut DocContext<'_>) -> GenericBound { match *self { hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)), - hir::GenericBound::Unsized(_) => GenericBound::maybe_sized(cx), hir::GenericBound::LangItemTrait(lang_item, span, _, generic_args) => { let def_id = cx.tcx.require_lang_item(lang_item, Some(span)); @@ -557,19 +556,13 @@ fn is_elided_lifetime(param: &hir::GenericParam<'_>) -> bool { WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds, .. } => { - if let [] | [GenericBound::TraitBound(_, hir::TraitBoundModifier::Maybe)] = - &bounds[..] - { + if bounds.is_empty() { for param in &mut generics.params { match param.kind { GenericParamDefKind::Lifetime { .. } => {} GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => { if ¶m.name == name { mem::swap(bounds, ty_bounds); - // We now keep track of `?Sized` obligations in the HIR. - // If we don't clear `ty_bounds` we end up with - // `fn foo(_: X) where X: ?Sized`. - ty_bounds.clear(); break; } } diff --git a/src/test/ui/error-codes/e0119/complex-impl.stderr b/src/test/ui/error-codes/e0119/complex-impl.stderr index 04babb0644718fa37121e1c1e00876d6d0d923a5..6a1a502749a75eb1e58320688e8f369b15c60768 100644 --- a/src/test/ui/error-codes/e0119/complex-impl.stderr +++ b/src/test/ui/error-codes/e0119/complex-impl.stderr @@ -6,7 +6,7 @@ LL | impl External for (Q, R) {} | = note: conflicting implementation in crate `complex_impl_support`: - impl<'a, 'b, 'c, T, U, V, W> External for (T, M<'a, 'b, 'c, Box, V, W>) - where >::Output == V, ::Item == T, 'b: 'a, T: 'a, U: FnOnce<(T,)>, U: 'static, V: Iterator, V: Clone, W: Add, ::Output: Copy; + where >::Output == V, ::Item == T, 'b: 'a, T: 'a, U: 'static, U: FnOnce<(T,)>, V: Iterator, V: Clone, W: Add, ::Output: Copy; error[E0117]: only traits defined in the current crate can be implemented for arbitrary types --> $DIR/complex-impl.rs:9:1 diff --git a/src/test/ui/generics/wrong-number-of-args.stderr b/src/test/ui/generics/wrong-number-of-args.stderr index 98e5cd6bab89351861edeb9f1184ce01ae36325a..22da0dffbf29eb87cf51d02eafa70ea5f6766338 100644 --- a/src/test/ui/generics/wrong-number-of-args.stderr +++ b/src/test/ui/generics/wrong-number-of-args.stderr @@ -440,17 +440,6 @@ note: trait defined here, with 1 lifetime parameter: `'a` LL | trait GenericLifetimeAT<'a> { | ^^^^^^^^^^^^^^^^^ -- -error[E0106]: missing lifetime specifier - --> $DIR/wrong-number-of-args.rs:169:44 - | -LL | type C = Box>; - | ^ expected named lifetime parameter - | -help: consider introducing a named lifetime parameter - | -LL | type C<'a> = Box>; - | ++++ +++ - error[E0107]: this trait takes 0 generic arguments but 1 generic argument was supplied --> $DIR/wrong-number-of-args.rs:169:26 | @@ -465,6 +454,17 @@ note: trait defined here, with 0 generic parameters LL | trait GenericLifetimeAT<'a> { | ^^^^^^^^^^^^^^^^^ +error[E0106]: missing lifetime specifier + --> $DIR/wrong-number-of-args.rs:169:44 + | +LL | type C = Box>; + | ^ expected named lifetime parameter + | +help: consider introducing a named lifetime parameter + | +LL | type C<'a> = Box>; + | ++++ +++ + error[E0107]: this trait takes 1 generic argument but 0 generic arguments were supplied --> $DIR/wrong-number-of-args.rs:181:26 | @@ -525,17 +525,6 @@ help: add missing generic argument LL | type C = Box>; | +++ -error[E0106]: missing lifetime specifier - --> $DIR/wrong-number-of-args.rs:201:48 - | -LL | type A = Box>; - | ^ expected named lifetime parameter - | -help: consider introducing a named lifetime parameter - | -LL | type A<'a> = Box>; - | ++++ +++ - error[E0107]: this trait takes 1 generic argument but 0 generic arguments were supplied --> $DIR/wrong-number-of-args.rs:201:26 | @@ -552,6 +541,17 @@ help: add missing generic argument LL | type A = Box>; | ++ +error[E0106]: missing lifetime specifier + --> $DIR/wrong-number-of-args.rs:201:48 + | +LL | type A = Box>; + | ^ expected named lifetime parameter + | +help: consider introducing a named lifetime parameter + | +LL | type A<'a> = Box>; + | ++++ +++ + error[E0107]: this trait takes 1 generic argument but 0 generic arguments were supplied --> $DIR/wrong-number-of-args.rs:207:26 | @@ -609,17 +609,6 @@ help: consider introducing a named lifetime parameter LL | type D<'a> = Box>; | ++++ +++ -error[E0106]: missing lifetime specifier - --> $DIR/wrong-number-of-args.rs:221:48 - | -LL | type E = Box>; - | ^ expected named lifetime parameter - | -help: consider introducing a named lifetime parameter - | -LL | type E<'a> = Box>; - | ++++ +++ - error[E0107]: this trait takes 1 generic argument but 2 generic arguments were supplied --> $DIR/wrong-number-of-args.rs:221:26 | @@ -634,6 +623,17 @@ note: trait defined here, with 1 generic parameter: `A` LL | trait GenericLifetimeTypeAT<'a, A> { | ^^^^^^^^^^^^^^^^^^^^^ - +error[E0106]: missing lifetime specifier + --> $DIR/wrong-number-of-args.rs:221:48 + | +LL | type E = Box>; + | ^ expected named lifetime parameter + | +help: consider introducing a named lifetime parameter + | +LL | type E<'a> = Box>; + | ++++ +++ + error[E0107]: this trait takes 1 lifetime argument but 2 lifetime arguments were supplied --> $DIR/wrong-number-of-args.rs:227:26 | @@ -767,17 +767,6 @@ help: add missing lifetime argument LL | type B = Box>; | ++++ -error[E0106]: missing lifetime specifiers - --> $DIR/wrong-number-of-args.rs:279:56 - | -LL | type A = Box>; - | ^ expected 2 lifetime parameters - | -help: consider introducing a named lifetime parameter - | -LL | type A<'a> = Box>; - | ++++ +++++++ - error[E0107]: this trait takes 1 generic argument but 0 generic arguments were supplied --> $DIR/wrong-number-of-args.rs:279:26 | @@ -794,6 +783,17 @@ help: add missing generic argument LL | type A = Box>; | ++ +error[E0106]: missing lifetime specifiers + --> $DIR/wrong-number-of-args.rs:279:56 + | +LL | type A = Box>; + | ^ expected 2 lifetime parameters + | +help: consider introducing a named lifetime parameter + | +LL | type A<'a> = Box>; + | ++++ +++++++ + error[E0107]: this trait takes 2 lifetime arguments but 1 lifetime argument was supplied --> $DIR/wrong-number-of-args.rs:285:26 |