Skip to content

Commit 06ef889

Browse files
committed
libsyntax: Remove extern mod foo { ... } from the language.
1 parent 830b945 commit 06ef889

File tree

9 files changed

+160
-146
lines changed

9 files changed

+160
-146
lines changed

doc/rust.md

+40-41
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ each of which may have some number of [attributes](#attributes) attached to it.
618618

619619
~~~~~~~~ {.ebnf .gram}
620620
item : mod_item | fn_item | type_item | struct_item | enum_item
621-
| static_item | trait_item | impl_item | foreign_mod_item ;
621+
| static_item | trait_item | impl_item | extern_block ;
622622
~~~~~~~~
623623

624624
An _item_ is a component of a crate; some module items can be defined in crate
@@ -752,10 +752,11 @@ link_attr : ident '=' literal ;
752752
~~~~~~~~
753753

754754
An _`extern mod` declaration_ specifies a dependency on an external crate.
755-
The external crate is then bound into the declaring scope as the `ident` provided in the `extern_mod_decl`.
755+
The external crate is then bound into the declaring scope
756+
as the `ident` provided in the `extern_mod_decl`.
756757

757-
The external crate is resolved to a specific `soname` at compile time, and a
758-
runtime linkage requirement to that `soname` is passed to the linker for
758+
The external crate is resolved to a specific `soname` at compile time,
759+
and a runtime linkage requirement to that `soname` is passed to the linker for
759760
loading at runtime. The `soname` is resolved at compile time by scanning the
760761
compiler's library path and matching the `link_attrs` provided in the
761762
`use_decl` against any `#link` attributes that were declared on the external
@@ -992,10 +993,10 @@ Thus the return type on `f` only needs to reflect the `if` branch of the conditi
992993
#### Extern functions
993994

994995
Extern functions are part of Rust's foreign function interface,
995-
providing the opposite functionality to [foreign modules](#foreign-modules).
996-
Whereas foreign modules allow Rust code to call foreign code,
997-
extern functions with bodies defined in Rust code _can be called by foreign code_.
998-
They are defined in the same way as any other Rust function,
996+
providing the opposite functionality to [external blocks](#external-blocks).
997+
Whereas external blocks allow Rust code to call foreign code,
998+
extern functions with bodies defined in Rust code _can be called by foreign
999+
code_. They are defined in the same way as any other Rust function,
9991000
except that they have the `extern` modifier.
10001001

10011002
~~~
@@ -1011,7 +1012,8 @@ let fptr: *u8 = new_vec;
10111012
~~~
10121013

10131014
The primary motivation for extern functions is
1014-
to create callbacks for foreign functions that expect to receive function pointers.
1015+
to create callbacks for foreign functions that expect to receive function
1016+
pointers.
10151017

10161018
### Type definitions
10171019

@@ -1308,64 +1310,61 @@ impl Seq<bool> for u32 {
13081310
}
13091311
~~~~
13101312

1311-
### Foreign modules
1313+
### External blocks
13121314

13131315
~~~ {.ebnf .gram}
1314-
foreign_mod_item : "extern mod" ident '{' foreign_mod '} ;
1315-
foreign_mod : [ foreign_fn ] * ;
1316+
extern_block_item : "extern" '{' extern_block '} ;
1317+
extern_block : [ foreign_fn ] * ;
13161318
~~~
13171319

1318-
Foreign modules form the basis for Rust's foreign function interface. A
1319-
foreign module describes functions in external, non-Rust
1320-
libraries.
1321-
Functions within foreign modules are declared in the same way as other Rust functions,
1322-
with the exception that they may not have a body and are instead terminated by a semicolon.
1320+
External blocks form the basis for Rust's foreign function interface.
1321+
Declarations in an external block describe symbols
1322+
in external, non-Rust libraries.
1323+
1324+
Functions within external blocks
1325+
are declared in the same way as other Rust functions,
1326+
with the exception that they may not have a body
1327+
and are instead terminated by a semicolon.
13231328

13241329
~~~
13251330
# use core::libc::{c_char, FILE};
13261331
# #[nolink]
13271332
1328-
extern mod c {
1333+
extern {
13291334
fn fopen(filename: *c_char, mode: *c_char) -> *FILE;
13301335
}
13311336
~~~
13321337

1333-
Functions within foreign modules may be called by Rust code, just like functions defined in Rust.
1334-
The Rust compiler automatically translates between the Rust ABI and the foreign ABI.
1335-
1336-
The name of the foreign module has special meaning to the Rust compiler in
1337-
that it will treat the module name as the name of a library to link to,
1338-
performing the linking as appropriate for the target platform. The name
1339-
given for the foreign module will be transformed in a platform-specific way
1340-
to determine the name of the library. For example, on Linux the name of the
1341-
foreign module is prefixed with 'lib' and suffixed with '.so', so the
1342-
foreign mod 'rustrt' would be linked to a library named 'librustrt.so'.
1338+
Functions within external blocks may be called by Rust code,
1339+
just like functions defined in Rust.
1340+
The Rust compiler automatically translates
1341+
between the Rust ABI and the foreign ABI.
13431342

1344-
A number of [attributes](#attributes) control the behavior of foreign
1345-
modules.
1343+
A number of [attributes](#attributes) control the behavior of external
1344+
blocks.
13461345

1347-
By default foreign modules assume that the library they are calling use the
1348-
standard C "cdecl" ABI. Other ABIs may be specified using the `abi`
1349-
attribute as in
1346+
By default external blocks assume
1347+
that the library they are calling uses the standard C "cdecl" ABI.
1348+
Other ABIs may be specified using the `abi` attribute as in
13501349

13511350
~~~{.xfail-test}
13521351
// Interface to the Windows API
13531352
#[abi = "stdcall"]
1354-
extern mod kernel32 { }
1353+
extern { }
13551354
~~~
13561355

1357-
The `link_name` attribute allows the default library naming behavior to
1358-
be overridden by explicitly specifying the name of the library.
1356+
The `link_name` attribute allows the name of the library to be specified.
13591357

13601358
~~~{.xfail-test}
13611359
#[link_name = "crypto"]
1362-
extern mod mycrypto { }
1360+
extern { }
13631361
~~~
13641362

1365-
The `nolink` attribute tells the Rust compiler not to do any linking for the foreign module.
1366-
This is particularly useful for creating foreign
1367-
modules for libc, which tends to not follow standard library naming
1368-
conventions and is linked to all Rust programs anyway.
1363+
The `nolink` attribute tells the Rust compiler
1364+
not to do any linking for the external block.
1365+
This is particularly useful for creating external blocks for libc,
1366+
which tends to not follow standard library naming conventions
1367+
and is linked to all Rust programs anyway.
13691368

13701369
## Attributes
13711370

doc/tutorial-ffi.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ convention to use:
237237
~~~~
238238
#[cfg(target_os = "win32")]
239239
#[abi = "stdcall"]
240-
extern mod kernel32 {
240+
#[link_name = "kernel32"]
241+
extern {
241242
fn SetEnvironmentVariableA(n: *u8, v: *u8) -> int;
242243
}
243244
~~~~

src/libcore/os.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ pub fn env() -> ~[(~str,~str)] {
196196
}
197197
#[cfg(unix)]
198198
unsafe fn get_env_pairs() -> ~[~str] {
199-
extern mod rustrt {
199+
extern {
200200
unsafe fn rust_env_pairs() -> **libc::c_char;
201201
}
202-
let environ = rustrt::rust_env_pairs();
202+
let environ = rust_env_pairs();
203203
if (environ as uint == 0) {
204204
fail!(fmt!("os::env() failure getting env string from OS: %s",
205205
os::last_os_error()));
@@ -685,9 +685,8 @@ pub fn list_dir(p: &Path) -> ~[~str] {
685685
unsafe fn get_list(p: &Path) -> ~[~str] {
686686
use libc::{dirent_t};
687687
use libc::{opendir, readdir, closedir};
688-
extern mod rustrt {
689-
unsafe fn rust_list_dir_val(ptr: *dirent_t)
690-
-> *libc::c_char;
688+
extern {
689+
unsafe fn rust_list_dir_val(ptr: *dirent_t) -> *libc::c_char;
691690
}
692691
let input = p.to_str();
693692
let mut strings = ~[];
@@ -698,10 +697,8 @@ pub fn list_dir(p: &Path) -> ~[~str] {
698697
debug!("os::list_dir -- opendir() SUCCESS");
699698
let mut entry_ptr = readdir(dir_ptr);
700699
while (entry_ptr as uint != 0) {
701-
strings.push(
702-
str::raw::from_c_str(
703-
rustrt::rust_list_dir_val(
704-
entry_ptr)));
700+
strings.push(str::raw::from_c_str(rust_list_dir_val(
701+
entry_ptr)));
705702
entry_ptr = readdir(dir_ptr);
706703
}
707704
closedir(dir_ptr);
@@ -729,25 +726,23 @@ pub fn list_dir(p: &Path) -> ~[~str] {
729726
};
730727
use unstable::exchange_alloc::{malloc_raw, free_raw};
731728
#[nolink]
732-
extern mod rustrt {
729+
extern {
733730
unsafe fn rust_list_dir_wfd_size() -> libc::size_t;
734731
unsafe fn rust_list_dir_wfd_fp_buf(wfd: *libc::c_void)
735732
-> *u16;
736733
}
737734
fn star(p: &Path) -> Path { p.push("*") }
738735
do as_utf16_p(star(p).to_str()) |path_ptr| {
739736
let mut strings = ~[];
740-
let wfd_ptr = malloc_raw(
741-
rustrt::rust_list_dir_wfd_size() as uint);
737+
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint);
742738
let find_handle =
743739
FindFirstFileW(
744740
path_ptr,
745741
::cast::transmute(wfd_ptr));
746742
if find_handle as int != INVALID_HANDLE_VALUE {
747743
let mut more_files = 1 as libc::c_int;
748744
while more_files != 0 {
749-
let fp_buf = rustrt::rust_list_dir_wfd_fp_buf(
750-
wfd_ptr);
745+
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr);
751746
if fp_buf as uint == 0 {
752747
fail!(~"os::list_dir() failure:"+
753748
~" got null ptr from wfd");

src/libstd/priority_queue.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::old_iter::BaseIter;
1414
use core::util::{replace, swap};
1515

1616
#[abi = "rust-intrinsic"]
17-
extern "rust-intrinsic" mod rusti {
17+
extern "rust-intrinsic" {
1818
fn move_val_init<T>(dst: &mut T, src: T);
1919
fn init<T>() -> T;
2020
#[cfg(not(stage0))]
@@ -154,13 +154,13 @@ pub impl <T:Ord> PriorityQueue<T> {
154154
let parent = (pos - 1) >> 1;
155155
if new > self.data[parent] {
156156
let x = replace(&mut self.data[parent], rusti::uninit());
157-
rusti::move_val_init(&mut self.data[pos], x);
157+
move_val_init(&mut self.data[pos], x);
158158
pos = parent;
159159
loop
160160
}
161161
break
162162
}
163-
rusti::move_val_init(&mut self.data[pos], new);
163+
move_val_init(&mut self.data[pos], new);
164164
}
165165
}
166166

@@ -173,13 +173,13 @@ pub impl <T:Ord> PriorityQueue<T> {
173173
let parent = (pos - 1) >> 1;
174174
if new > self.data[parent] {
175175
let x = replace(&mut self.data[parent], rusti::init());
176-
rusti::move_val_init(&mut self.data[pos], x);
176+
move_val_init(&mut self.data[pos], x);
177177
pos = parent;
178178
loop
179179
}
180180
break
181181
}
182-
rusti::move_val_init(&mut self.data[pos], new);
182+
move_val_init(&mut self.data[pos], new);
183183
}
184184
}
185185

@@ -197,12 +197,12 @@ pub impl <T:Ord> PriorityQueue<T> {
197197
child = right;
198198
}
199199
let x = replace(&mut self.data[child], rusti::uninit());
200-
rusti::move_val_init(&mut self.data[pos], x);
200+
move_val_init(&mut self.data[pos], x);
201201
pos = child;
202202
child = 2 * pos + 1;
203203
}
204204

205-
rusti::move_val_init(&mut self.data[pos], new);
205+
move_val_init(&mut self.data[pos], new);
206206
self.siftup(start, pos);
207207
}
208208
}
@@ -220,12 +220,12 @@ pub impl <T:Ord> PriorityQueue<T> {
220220
child = right;
221221
}
222222
let x = replace(&mut self.data[child], rusti::init());
223-
rusti::move_val_init(&mut self.data[pos], x);
223+
move_val_init(&mut self.data[pos], x);
224224
pos = child;
225225
child = 2 * pos + 1;
226226
}
227227

228-
rusti::move_val_init(&mut self.data[pos], new);
228+
move_val_init(&mut self.data[pos], new);
229229
self.siftup(start, pos);
230230
}
231231
}

src/libstd/rc.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ mod test_rc {
110110
}
111111
}
112112

113+
#[abi = "rust-intrinsic"]
114+
extern "rust-intrinsic" {
115+
fn init<T>() -> T;
116+
#[cfg(not(stage0))]
117+
fn uninit<T>() -> T;
118+
}
119+
113120
#[deriving(Eq)]
114121
enum Borrow {
115122
Mutable,
@@ -171,7 +178,7 @@ impl<T: Owned> Drop for RcMut<T> {
171178
unsafe {
172179
(*self.ptr).count -= 1;
173180
if (*self.ptr).count == 0 {
174-
util::replace_ptr(self.ptr, intrinsics::uninit());
181+
util::replace_ptr(self.ptr, uninit());
175182
free(self.ptr as *c_void)
176183
}
177184
}
@@ -185,7 +192,7 @@ impl<T: Owned> Drop for RcMut<T> {
185192
unsafe {
186193
(*self.ptr).count -= 1;
187194
if (*self.ptr).count == 0 {
188-
util::replace_ptr(self.ptr, intrinsics::init());
195+
util::replace_ptr(self.ptr, init());
189196
free(self.ptr as *c_void)
190197
}
191198
}

0 commit comments

Comments
 (0)