Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure valid TraitRefs are created for GATs #82066

Merged
merged 8 commits into from
Feb 18, 2021
1 change: 1 addition & 0 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ impl<'a> State<'a> {

pub fn print_assoc_constraint(&mut self, constraint: &ast::AssocTyConstraint) {
self.print_ident(constraint.ident);
constraint.gen_args.as_ref().map(|args| self.print_generic_args(args, false));
self.s.space();
match &constraint.kind {
ast::AssocTyConstraintKind::Equality { ty } => {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ language_item_table! {

Deref, sym::deref, deref_trait, Target::Trait;
DerefMut, sym::deref_mut, deref_mut_trait, Target::Trait;
DerefTarget, sym::deref_target, deref_target, Target::AssocTy;
Receiver, sym::receiver, receiver_trait, Target::Trait;

Fn, kw::Fn, fn_trait, Target::Trait;
Expand Down
16 changes: 0 additions & 16 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,22 +1112,6 @@ pub struct ProjectionTy<'tcx> {
}

impl<'tcx> ProjectionTy<'tcx> {
/// Construct a `ProjectionTy` by searching the trait from `trait_ref` for the
/// associated item named `item_name`.
pub fn from_ref_and_name(
tcx: TyCtxt<'_>,
trait_ref: ty::TraitRef<'tcx>,
item_name: Ident,
) -> ProjectionTy<'tcx> {
let item_def_id = tcx
.associated_items(trait_ref.def_id)
.find_by_name_and_kind(tcx, item_name, ty::AssocKind::Type, trait_ref.def_id)
.unwrap()
.def_id;

ProjectionTy { substs: trait_ref.substs, item_def_id }
}

/// Extracts the underlying trait reference from this projection.
/// For example, if this is a projection of `<T as Iterator>::Item`,
/// then this function would return a `T: Iterator` trait reference.
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_trait_selection/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use rustc_infer::infer::InferCtxt;
use rustc_middle::ty::{self, TraitRef, Ty, TyCtxt, WithConstness};
use rustc_middle::ty::{ToPredicate, TypeFoldable};
use rustc_session::DiagnosticMessageId;
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -146,11 +145,10 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
let normalized_ty = fulfillcx.normalize_projection_type(
&self.infcx,
self.param_env,
ty::ProjectionTy::from_ref_and_name(
tcx,
trait_ref,
Ident::with_dummy_span(sym::Target),
),
ty::ProjectionTy {
item_def_id: tcx.lang_items().deref_target()?,
substs: trait_ref.substs,
},
cause,
);
if let Err(e) = fulfillcx.select_where_possible(&self.infcx) {
Expand Down
1 change: 1 addition & 0 deletions library/core/src/ops/deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub trait Deref {
/// The resulting type after dereferencing.
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "deref_target"]
#[cfg_attr(not(bootstrap), lang = "deref_target")]
jackh726 marked this conversation as resolved.
Show resolved Hide resolved
type Target: ?Sized;

/// Dereferences the value.
Expand Down
2 changes: 2 additions & 0 deletions src/test/pretty/gat-bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ impl X for () {
type Y<T> where Self: Sized = u32;
}

fn f<T: X<Y<()> = i32>>() {}

fn main() { }