Pre-RFC? Making `extern` methods invalid


#1

There are a number of bugs related to methods marked extern. There was a “fix” some time ago that made them not error (but only in one place), but they’ve never actually worked. I’d like to make it so they actually error, since they don’t really make any sense.

extern is currently accepted on all function items, including inherent methods, associated functions and trait methods. Every use I’ve seen of this “feature” has been a mistake on the part of the user, either an accident due to copy-paste or a misunderstanding of what extern means. I think it would be much clearer to just disallow them altogether, rather than try to get them to work.


#2

We currently use extern "rust-call" fn for the methods of the closure traits, but they’re unstable/this can be changed.


#3

As long as this is only for methods and not regular functions, then I’m okay with it.


#4

Why are extern methods a problem again?


#5

Ah yes, I think I mean to specify non-Rust calling conventions. So those would still be ok.

The main issue is that they’re not properly implemented, a good number of ICEs are due to people using extern on methods. They probably could be made to work, but I’d prefer to just disallow it. On traits, their use could potentially limit the types they can be implemented on (at least without causing warnings).

Ultimately, I’m not convinced that use of a non-Rust calling convention for methods is anything other than a mistake in most cases. The only two cases for declaring a non-Rust calling convention on a function are 1) linking to a foreign library that exposes the function and 2) passing a function to a foreign function that expects a certain calling convention. The first one isn’t applicable, as you can’t expose methods like that from object files, and while the second case can apply, a regular function works equally as well and is arguably much clearer.


#6

What about moving extern methods to a feature gate, since they’re clearly not stable.


#7

The rust-call calling convention is used for Fn trait methods.


#8

I am inclined to agree with @aatch. The simplest thing would be to extend the closure feature gate to cover all extern methods, at least for now.

Niko


#9

Cool, so the last thing is: Does this change need a full RFC? (Hence the ‘?’ in the title)