Skip to content

Commit

Permalink
Rollup merge of #74276 - lcnr:discriminant-kind-what, r=nagisa
Browse files Browse the repository at this point in the history
improve DiscriminantKind handling

Adds a lang item `discriminant_type` for the associated type `DiscriminantKind::Discriminant`.

Changes the discriminant of generators from `i32` to `u32`, which should not be observable to fix an
oversight where MIR was using `u32` and codegen and typeck used `i32`.
  • Loading branch information
Manishearth authored Jul 15, 2020
2 parents 0d07db9 + fcf52c1 commit 47f6a9d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/libcore/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ mod impls {
pub trait DiscriminantKind {
/// The type of the discriminant, which must satisfy the trait
/// bounds required by `mem::Discriminant`.
#[cfg_attr(not(bootstrap), lang = "discriminant_type")]
type Discriminant: Clone + Copy + Debug + Eq + PartialEq + Hash + Send + Sync + Unpin;
}

Expand Down
3 changes: 3 additions & 0 deletions src/librustc_hir/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ language_item_table! {
CloneTraitLangItem, sym::clone, clone_trait, Target::Trait;
SyncTraitLangItem, sym::sync, sync_trait, Target::Trait;
DiscriminantKindTraitLangItem, sym::discriminant_kind, discriminant_kind_trait, Target::Trait;
// The associated item of `trait DiscriminantKind`.
DiscriminantTypeLangItem, sym::discriminant_type, discriminant_type, Target::AssocTy;

FreezeTraitLangItem, sym::freeze, freeze_trait, Target::Trait;

DropTraitLangItem, sym::drop, drop_trait, Target::Trait;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ symbols! {
diagnostic,
direct,
discriminant_kind,
discriminant_type,
discriminant_value,
dispatch_from_dyn,
div,
Expand Down
20 changes: 5 additions & 15 deletions src/librustc_trait_selection/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ use crate::traits::error_reporting::InferCtxtExt;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::ErrorReported;
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::{FnOnceOutputLangItem, FnOnceTraitLangItem, GeneratorTraitLangItem};
use rustc_hir::lang_items::{
DiscriminantTypeLangItem, FnOnceOutputLangItem, FnOnceTraitLangItem, GeneratorTraitLangItem,
};
use rustc_infer::infer::resolve::OpportunisticRegionResolver;
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::util::IntTypeExt;
use rustc_middle::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness};
use rustc_span::symbol::sym;
use rustc_span::DUMMY_SP;
Expand Down Expand Up @@ -1324,22 +1325,11 @@ fn confirm_discriminant_kind_candidate<'cx, 'tcx>(
let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
let substs = tcx.mk_substs([self_ty.into()].iter());

let assoc_items = tcx.associated_items(tcx.lang_items().discriminant_kind_trait().unwrap());
// FIXME: emit an error if the trait definition is wrong
let discriminant_def_id = assoc_items.in_definition_order().next().unwrap().def_id;

let discriminant_ty = match self_ty.kind {
// Use the discriminant type for enums.
ty::Adt(adt, _) if adt.is_enum() => adt.repr.discr_type().to_ty(tcx),
// Default to `i32` for generators.
ty::Generator(..) => tcx.types.i32,
// Use `u8` for all other types.
_ => tcx.types.u8,
};
let discriminant_def_id = tcx.require_lang_item(DiscriminantTypeLangItem, None);

let predicate = ty::ProjectionPredicate {
projection_ty: ty::ProjectionTy { substs, item_def_id: discriminant_def_id },
ty: discriminant_ty,
ty: self_ty.discriminant_ty(tcx),
};

confirm_param_env_candidate(selcx, obligation, ty::Binder::bind(predicate))
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/generator/discriminant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ macro_rules! yield250 {
}

fn cycle(
gen: impl Generator<()> + Unpin + DiscriminantKind<Discriminant = i32>,
expected_max_discr: i32
gen: impl Generator<()> + Unpin + DiscriminantKind<Discriminant = u32>,
expected_max_discr: u32
) {
let mut gen = Box::pin(gen);
let mut max_discr = 0;
Expand Down

0 comments on commit 47f6a9d

Please sign in to comment.