Skip to content

Commit 9b4372e

Browse files
committed
Incorporate review feedback
Add requested comments, restructure some small bits of code. Fix extern declarations allowing impl Trait.
1 parent 7e9948f commit 9b4372e

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

src/librustc/hir/lowering.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,19 @@ impl<'a> LoweringContext<'a> {
11301130
}).collect()
11311131
}
11321132

1133+
11331134
fn lower_fn_decl(&mut self,
11341135
decl: &FnDecl,
11351136
fn_def_id: Option<DefId>,
11361137
impl_trait_return_allow: bool)
11371138
-> P<hir::FnDecl> {
1139+
// NOTE: The two last paramters here have to do with impl Trait. If fn_def_id is Some,
1140+
// then impl Trait arguments are lowered into generic paramters on the given
1141+
// fn_def_id, otherwise impl Trait is disallowed. (for now)
1142+
//
1143+
// Furthermore, if impl_trait_return_allow is true, then impl Trait may be used in
1144+
// return positions as well. This guards against trait declarations and their impls
1145+
// where impl Trait is disallowed. (again for now)
11381146
P(hir::FnDecl {
11391147
inputs: decl.inputs.iter()
11401148
.map(|arg| if let Some(def_id) = fn_def_id {
@@ -1143,9 +1151,9 @@ impl<'a> LoweringContext<'a> {
11431151
self.lower_ty(&arg.ty, ImplTraitContext::Disallowed)
11441152
}).collect(),
11451153
output: match decl.output {
1146-
FunctionRetTy::Ty(ref ty) => match (impl_trait_return_allow, fn_def_id) {
1147-
(false, _) => hir::Return(self.lower_ty(ty, ImplTraitContext::Disallowed)),
1148-
(_, Some(_)) => hir::Return(self.lower_ty(ty, ImplTraitContext::Existential)),
1154+
FunctionRetTy::Ty(ref ty) => match fn_def_id {
1155+
Some(_) if impl_trait_return_allow =>
1156+
hir::Return(self.lower_ty(ty, ImplTraitContext::Existential)),
11491157
_ => hir::Return(self.lower_ty(ty, ImplTraitContext::Disallowed)),
11501158
},
11511159
FunctionRetTy::Default(span) => hir::DefaultReturn(span),
@@ -1730,7 +1738,8 @@ impl<'a> LoweringContext<'a> {
17301738
this.expr_block(body, ThinVec::new())
17311739
});
17321740
let impl_trait_return_allow = !this.is_in_trait_impl;
1733-
hir::ImplItemKind::Method(this.lower_method_sig(sig, fn_def_id,
1741+
hir::ImplItemKind::Method(this.lower_method_sig(sig,
1742+
fn_def_id,
17341743
impl_trait_return_allow),
17351744
body_id)
17361745
}
@@ -1833,8 +1842,8 @@ impl<'a> LoweringContext<'a> {
18331842
attrs: this.lower_attrs(&i.attrs),
18341843
node: match i.node {
18351844
ForeignItemKind::Fn(ref fdec, ref generics) => {
1836-
let fn_def_id = this.resolver.definitions().opt_local_def_id(i.id);
1837-
hir::ForeignItemFn(this.lower_fn_decl(fdec, fn_def_id, true),
1845+
// Disallow impl Trait in foreign items
1846+
hir::ForeignItemFn(this.lower_fn_decl(fdec, None, false),
18381847
this.lower_fn_args_to_names(fdec),
18391848
this.lower_generics(generics))
18401849
}

src/librustc_typeck/collect.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,9 +1763,13 @@ fn extract_universal_impl_trait_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
17631763
}
17641764

17651765
let mut visitor = ImplTraitUniversalVisitor { items: Vec::new() };
1766-
opt_inputs.map(|inputs| for t in inputs.iter() {
1767-
visitor.visit_ty(t);
1768-
});
1766+
1767+
if let Some(inputs) = opt_inputs {
1768+
for t in inputs.iter() {
1769+
visitor.visit_ty(t);
1770+
}
1771+
}
1772+
17691773
visitor.items.into_iter().map(|ty| if let hir::TyImplTraitUniversal(_, ref bounds) = ty.node {
17701774
ImplTraitUniversalInfo {
17711775
id: ty.id,

src/test/compile-fail/impl-trait/where-allowed.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,9 @@ impl DummyType {
144144
extern "C" {
145145
fn in_foreign_parameters(_: impl Debug);
146146
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
147-
// FIXME currently allowed
148147

149148
fn in_foreign_return() -> impl Debug;
150149
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
151-
// FIXME currently allowed
152150
}
153151

154152
// Allowed

0 commit comments

Comments
 (0)