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

First step toward implementing impl Trait in argument position #44866

Merged
merged 2 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,10 @@ impl<'a> LoweringContext<'a> {
default: tp.default.as_ref().map(|x| self.lower_ty(x)),
span: tp.span,
pure_wrt_drop: tp.attrs.iter().any(|attr| attr.check_name("may_dangle")),
synthetic: tp.attrs.iter()
.filter(|attr| attr.check_name("rustc_synthetic"))
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
.nth(0),
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ pub struct TyParam {
pub default: Option<P<Ty>>,
pub span: Span,
pub pure_wrt_drop: bool,
pub synthetic: Option<SyntheticTyParamKind>,
}

/// Represents lifetimes and type parameters attached to a declaration
Expand Down Expand Up @@ -419,6 +420,13 @@ impl Generics {
}
}

/// Synthetic Type Parameters are converted to an other form during lowering, this allows
/// to track the original form they had. Usefull for error messages.
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum SyntheticTyParamKind {
ImplTrait
}

/// A `where` clause in a definition
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct WhereClause {
Expand Down
7 changes: 6 additions & 1 deletion src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ impl_stable_hash_for!(struct hir::TyParam {
bounds,
default,
span,
pure_wrt_drop
pure_wrt_drop,
synthetic
});

impl_stable_hash_for!(struct hir::Generics {
Expand All @@ -187,6 +188,10 @@ impl_stable_hash_for!(struct hir::Generics {
span
});

impl_stable_hash_for!(enum hir::SyntheticTyParamKind {
ImplTrait
});

impl_stable_hash_for!(struct hir::WhereClause {
id,
predicates
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ impl_stable_hash_for!(struct ty::TypeParameterDef {
index,
has_default,
object_lifetime_default,
pure_wrt_drop
pure_wrt_drop,
synthetic
});

impl<'gcx, T> HashStable<StableHashingContext<'gcx>>
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ pub struct TypeParameterDef {
/// on generic parameter `T`, asserts data behind the parameter
/// `T` won't be accessed during the parent type's `Drop` impl.
pub pure_wrt_drop: bool,

pub synthetic: Option<hir::SyntheticTyParamKind>,
}

#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
Expand Down
31 changes: 31 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4647,6 +4647,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// a problem.
self.check_path_parameter_count(span, &mut type_segment, false);
self.check_path_parameter_count(span, &mut fn_segment, false);
self.check_impl_trait(span, &mut fn_segment);

let (fn_start, has_self) = match (type_segment, fn_segment) {
(_, Some((_, generics))) => {
Expand Down Expand Up @@ -4871,6 +4872,36 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}

/// Report error if there is an explicit type parameter when using `impl Trait`.
fn check_impl_trait(&self,
span: Span,
segment: &mut Option<(&hir::PathSegment, &ty::Generics)>) {
use hir::SyntheticTyParamKind::*;

segment.map(|(path_segment, generics)| {
let explicit = !path_segment.infer_types;
let impl_trait = generics.types.iter()
.any(|ty_param| {
match ty_param.synthetic {
Some(ImplTrait) => true,
_ => false,
}
});

if explicit && impl_trait {
let mut err = struct_span_err! {
self.tcx.sess,
span,
E0631,
"cannot provide explicit type parameters when `impl Trait` is \
used in argument position."
};

err.emit();
}
});
}

fn structurally_resolve_type_or_else<F>(&self, sp: Span, ty: Ty<'tcx>, f: F)
-> Ty<'tcx>
where F: Fn() -> Ty<'tcx>
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
has_default: false,
object_lifetime_default: rl::Set1::Empty,
pure_wrt_drop: false,
synthetic: None,
});

allow_defaults = true;
Expand Down Expand Up @@ -993,6 +994,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
object_lifetime_default:
object_lifetime_defaults.as_ref().map_or(rl::Set1::Empty, |o| o[i]),
pure_wrt_drop: p.pure_wrt_drop,
synthetic: p.synthetic,
}
});
let mut types: Vec<_> = opt_self.into_iter().chain(types).collect();
Expand All @@ -1009,6 +1011,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
has_default: false,
object_lifetime_default: rl::Set1::Empty,
pure_wrt_drop: false,
synthetic: None,
}));
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_typeck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4677,4 +4677,6 @@ register_diagnostics! {
E0592, // duplicate definitions with name `{}`
// E0613, // Removed (merged with E0609)
E0627, // yield statement outside of generator literal
E0631, // cannot provide explicit type parameters when `impl Trait` is used in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error code got taken in the meanwhile, you should pick a different one to avoid an error:

[00:03:50] tidy error: duplicate error code: 631
[00:03:50] tidy error: /checkout/src/librustc/diagnostics.rs:2057:     E0631, // type mismatch in closure arguments
[00:03:50] tidy error: /checkout/src/librustc_typeck/diagnostics.rs:4680:     E0631, // cannot provide explicit type parameters when `impl Trait` is used in
[00:03:51] some tidy checks failed

// argument position.
}
6 changes: 6 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,12 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG
is just used for rustc unit tests \
and will never be stable",
cfg_fn!(rustc_attrs))),
("rustc_synthetic", Whitelisted, Gated(Stability::Unstable,
"rustc_attrs",
"this attribute \
is just used for rustc unit tests \
and will never be stable",
cfg_fn!(rustc_attrs))),
("rustc_symbol_name", Whitelisted, Gated(Stability::Unstable,
"rustc_attrs",
"internal rustc attributes will never be stable",
Expand Down
38 changes: 38 additions & 0 deletions src/test/compile-fail/synthetic-param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(generic_param_attrs, rustc_attrs)]

fn func<#[rustc_synthetic] T>(_: T) {}

struct Foo;

impl Foo {
pub fn func<#[rustc_synthetic] T>(_: T) {}
}

struct Bar<S> {
t: S
}

impl<S> Bar<S> {
pub fn func<#[rustc_synthetic] T>(_: T) {}
}

fn main() {
func::<u8>(42); //~ ERROR cannot provide explicit type parameters
func(42); // Ok

Foo::func::<u8>(42); //~ ERROR cannot provide explicit type parameters
Foo::func(42); // Ok

Bar::<i8>::func::<u8>(42); //~ ERROR cannot provide explicit type parameters
Bar::<i8>::func(42); // Ok
}