Skip to content

Forbid use of generics with foreign functions. Closes #10353. #12337

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 22 additions & 6 deletions src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,17 @@ pub fn ensure_no_ty_param_bounds(ccx: &CrateCtxt,
}
}

fn ensure_generics_abi(ccx: &CrateCtxt,
span: Span,
abis: AbiSet,
generics: &ast::Generics) {
if generics.ty_params.len() > 0 &&
!(abis.is_rust() || abis.is_intrinsic()) {
ccx.tcx.sess.span_err(span,
"foreign functions may not use type parameters");
}
}

pub fn convert(ccx: &CrateCtxt, it: &ast::Item) {
let tcx = ccx.tcx;
debug!("convert: item {} with id {}", token::get_ident(it.ident), it.id);
Expand All @@ -566,7 +577,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) {
tpt.ty,
enum_definition.variants,
generics);
}
},
ast::ItemImpl(ref generics, ref opt_trait_ref, selfty, ref ms) => {
let i_ty_generics = ty_generics(ccx, generics, 0);
let selfty = ccx.to_ty(&ExplicitRscope, selfty);
Expand Down Expand Up @@ -609,7 +620,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) {
for a builtin kind");
}
}
}
},
ast::ItemTrait(ref generics, _, ref trait_methods) => {
let trait_def = trait_def_of_item(ccx, it);

Expand All @@ -629,7 +640,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) {
// convert_methods produces a tcache entry that is wrong for
// static trait methods. This is somewhat unfortunate.
ensure_trait_methods(ccx, it.id);
}
},
ast::ItemStruct(struct_def, ref generics) => {
ensure_no_ty_param_bounds(ccx, it.span, generics, "structure");

Expand All @@ -643,19 +654,24 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) {
}

convert_struct(ccx, struct_def, tpt, it.id);
}
},
ast::ItemTy(_, ref generics) => {
ensure_no_ty_param_bounds(ccx, it.span, generics, "type");
let tpt = ty_of_item(ccx, it);
write_ty_to_tcx(tcx, it.id, tpt.ty);
}
},
ast::ItemFn(_, _, abi, ref generics, _) => {
ensure_generics_abi(ccx, it.span, abi, generics);
let tpt = ty_of_item(ccx, it);
write_ty_to_tcx(tcx, it.id, tpt.ty);
},
_ => {
// This call populates the type cache with the converted type
// of the item in passing. All we have to do here is to write
// it into the node type table.
let tpt = ty_of_item(ccx, it);
write_ty_to_tcx(tcx, it.id, tpt.ty);
}
},
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/generic-extern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2014 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.

extern "C" fn foo<T>() {} //~ERROR foreign functions may not use type parameters

fn main() {
let _ = foo::<int>;
}