From dd5c9bf1392bdc697740e62a1924b7942cdfd86a Mon Sep 17 00:00:00 2001 From: Jack Huey Date: Wed, 7 Oct 2020 20:02:06 -0400 Subject: Use map_bound(_ref) instead of Binder::bind when possible --- compiler/rustc_codegen_llvm/src/intrinsic.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'compiler/rustc_codegen_llvm') diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index e78a4ea2e3c..e1f675e3ae9 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -673,17 +673,9 @@ fn codegen_emcc_try( fn gen_fn<'ll, 'tcx>( cx: &CodegenCx<'ll, 'tcx>, name: &str, - inputs: Vec>, - output: Ty<'tcx>, + rust_fn_sig: ty::PolyFnSig<'tcx>, codegen: &mut dyn FnMut(Builder<'_, 'll, 'tcx>), ) -> &'ll Value { - let rust_fn_sig = ty::Binder::bind(cx.tcx.mk_fn_sig( - inputs.into_iter(), - output, - false, - hir::Unsafety::Unsafe, - Abi::Rust, - )); let fn_abi = FnAbi::of_fn_ptr(cx, rust_fn_sig, &[]); let llfn = cx.declare_fn(name, &fn_abi); cx.set_frame_pointer_elimination(llfn); @@ -710,14 +702,16 @@ fn get_rust_try_fn<'ll, 'tcx>( // Define the type up front for the signature of the rust_try function. let tcx = cx.tcx; let i8p = tcx.mk_mut_ptr(tcx.types.i8); - let try_fn_ty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig( + // `unsafe fn(*mut i8) -> ()` + let try_fn_ty = tcx.mk_fn_ptr(ty::Binder::dummy(tcx.mk_fn_sig( iter::once(i8p), tcx.mk_unit(), false, hir::Unsafety::Unsafe, Abi::Rust, ))); - let catch_fn_ty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig( + // `unsafe fn(*mut i8, *mut i8) -> ()` + let catch_fn_ty = tcx.mk_fn_ptr(ty::Binder::dummy(tcx.mk_fn_sig( [i8p, i8p].iter().cloned(), tcx.mk_unit(), false, @@ -725,7 +719,15 @@ fn get_rust_try_fn<'ll, 'tcx>( Abi::Rust, ))); let output = tcx.types.i32; - let rust_try = gen_fn(cx, "__rust_try", vec![try_fn_ty, i8p, catch_fn_ty], output, codegen); + // `unsafe fn(unsafe fn(*mut i8) -> (), unsafe fn(*mut i8, *mut i8) -> ()) -> i32` + let rust_fn_sig = ty::Binder::dummy(cx.tcx.mk_fn_sig( + vec![try_fn_ty, i8p, catch_fn_ty].into_iter(), + output, + false, + hir::Unsafety::Unsafe, + Abi::Rust, + )); + let rust_try = gen_fn(cx, "__rust_try", rust_fn_sig, codegen); cx.rust_try_fn.set(Some(rust_try)); rust_try } -- cgit 1.4.1-3-g733a5 From 11d62aa2849b48b850c412cc7ce20d8a60a74ab8 Mon Sep 17 00:00:00 2001 From: Jack Huey Date: Sun, 11 Oct 2020 17:14:07 -0400 Subject: Review comments --- compiler/rustc_codegen_llvm/src/intrinsic.rs | 5 ++--- compiler/rustc_middle/src/ty/mod.rs | 14 +++++++++++++- compiler/rustc_middle/src/ty/print/pretty.rs | 2 +- compiler/rustc_traits/src/chalk/lowering.rs | 28 ++++++++++++++++++++-------- 4 files changed, 36 insertions(+), 13 deletions(-) (limited to 'compiler/rustc_codegen_llvm') diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index e1f675e3ae9..e9900e8bc10 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -718,11 +718,10 @@ fn get_rust_try_fn<'ll, 'tcx>( hir::Unsafety::Unsafe, Abi::Rust, ))); - let output = tcx.types.i32; - // `unsafe fn(unsafe fn(*mut i8) -> (), unsafe fn(*mut i8, *mut i8) -> ()) -> i32` + // `unsafe fn(unsafe fn(*mut i8) -> (), *mut i8, unsafe fn(*mut i8, *mut i8) -> ()) -> i32` let rust_fn_sig = ty::Binder::dummy(cx.tcx.mk_fn_sig( vec![try_fn_ty, i8p, catch_fn_ty].into_iter(), - output, + tcx.types.i32, false, hir::Unsafety::Unsafe, Abi::Rust, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 275888b0ce2..7c5c954a64b 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1056,9 +1056,21 @@ impl<'tcx> Predicate<'tcx> { } } + /// Converts this to a `Binder>`. If the value was an + /// `Atom`, then it is not allowed to contain escaping bound vars. + pub fn bound_atom(self, _tcx: TyCtxt<'tcx>) -> Binder> { + match self.kind() { + &PredicateKind::ForAll(binder) => binder, + &PredicateKind::Atom(atom) => { + assert!(!atom.has_escaping_bound_vars()); + Binder::dummy(atom) + } + } + } + /// Allows using a `Binder>` even if the given predicate previously /// contained unbound variables by shifting these variables outwards. - pub fn bound_atom(self, tcx: TyCtxt<'tcx>) -> Binder> { + pub fn bound_atom_with_opt_escaping(self, tcx: TyCtxt<'tcx>) -> Binder> { match self.kind() { &PredicateKind::ForAll(binder) => binder, &PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom), diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 20dc49991b4..eaddfefb8e5 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -618,7 +618,7 @@ pub trait PrettyPrinter<'tcx>: // may contain unbound variables. We therefore do this manually. // // FIXME(lcnr): Find out why exactly this is the case :) - let bound_predicate = predicate.bound_atom(self.tcx()); + let bound_predicate = predicate.bound_atom_with_opt_escaping(self.tcx()); if let ty::PredicateAtom::Trait(pred, _) = bound_predicate.skip_binder() { let trait_ref = bound_predicate.map_bound(|_| pred.trait_ref); // Don't print +Sized, but rather +?Sized if absent. diff --git a/compiler/rustc_traits/src/chalk/lowering.rs b/compiler/rustc_traits/src/chalk/lowering.rs index 2ca94c6444a..5ca0fc0c88b 100644 --- a/compiler/rustc_traits/src/chalk/lowering.rs +++ b/compiler/rustc_traits/src/chalk/lowering.rs @@ -81,8 +81,11 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment, ) -> chalk_ir::InEnvironment>> { let clauses = self.environment.into_iter().map(|predicate| { - let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, &predicate.bound_atom(interner.tcx)); + let (predicate, binders, _named_regions) = collect_bound_vars( + interner, + interner.tcx, + &predicate.bound_atom_with_opt_escaping(interner.tcx), + ); let consequence = match predicate { ty::PredicateAtom::TypeWellFormedFromEnv(ty) => { chalk_ir::DomainGoal::FromEnv(chalk_ir::FromEnv::Ty(ty.lower_into(interner))) @@ -133,8 +136,11 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment LowerInto<'tcx, chalk_ir::GoalData>> for ty::Predicate<'tcx> { fn lower_into(self, interner: &RustInterner<'tcx>) -> chalk_ir::GoalData> { - let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, &self.bound_atom(interner.tcx)); + let (predicate, binders, _named_regions) = collect_bound_vars( + interner, + interner.tcx, + &self.bound_atom_with_opt_escaping(interner.tcx), + ); let value = match predicate { ty::PredicateAtom::Trait(predicate, _) => { @@ -653,8 +659,11 @@ impl<'tcx> LowerInto<'tcx, Option, ) -> Option>> { - let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, &self.bound_atom(interner.tcx)); + let (predicate, binders, _named_regions) = collect_bound_vars( + interner, + interner.tcx, + &self.bound_atom_with_opt_escaping(interner.tcx), + ); let value = match predicate { ty::PredicateAtom::Trait(predicate, _) => { Some(chalk_ir::WhereClause::Implemented(predicate.trait_ref.lower_into(interner))) @@ -762,8 +771,11 @@ impl<'tcx> LowerInto<'tcx, Option, ) -> Option>> { - let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, &self.bound_atom(interner.tcx)); + let (predicate, binders, _named_regions) = collect_bound_vars( + interner, + interner.tcx, + &self.bound_atom_with_opt_escaping(interner.tcx), + ); match predicate { ty::PredicateAtom::Trait(predicate, _) => Some(chalk_ir::Binders::new( binders, -- cgit 1.4.1-3-g733a5