Skip to content

Commit 1f0fcf1

Browse files
authored
Rollup merge of #109243 - chenyukang:yukang/fix-ice-109144, r=petrochenkov
The name of NativeLib will be presented Fixes #109144 I was working on a quick fix, but found change the name from `Option<Symbol>` to `Symbol` make life a little bit easier.
2 parents 879d6f2 + d5558e6 commit 1f0fcf1

File tree

8 files changed

+72
-56
lines changed

8 files changed

+72
-56
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ fn link_rlib<'a>(
358358
let (data, _) = create_wrapper_file(sess, b".bundled_lib".to_vec(), &src);
359359
let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str());
360360
packed_bundled_libs.push(wrapper_file);
361-
} else if let Some(name) = lib.name {
361+
} else {
362362
let path =
363-
find_native_static_library(name.as_str(), lib.verbatim, &lib_search_paths, sess);
363+
find_native_static_library(lib.name.as_str(), lib.verbatim, &lib_search_paths, sess);
364364
ab.add_archive(&path, Box::new(|_| false)).unwrap_or_else(|error| {
365365
sess.emit_fatal(errors::AddNativeLibrary { library_path: path, error })});
366366
}
@@ -436,7 +436,7 @@ fn collate_raw_dylibs<'a, 'b>(
436436
for lib in used_libraries {
437437
if lib.kind == NativeLibKind::RawDylib {
438438
let ext = if lib.verbatim { "" } else { ".dll" };
439-
let name = format!("{}{}", lib.name.expect("unnamed raw-dylib library"), ext);
439+
let name = format!("{}{}", lib.name, ext);
440440
let imports = dylib_table.entry(name.clone()).or_default();
441441
for import in &lib.dll_imports {
442442
if let Some(old_import) = imports.insert(import.name, import) {
@@ -1296,7 +1296,7 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
12961296
.iter()
12971297
.filter(|l| relevant_lib(sess, l))
12981298
.filter_map(|lib| {
1299-
let name = lib.name?;
1299+
let name = lib.name;
13001300
match lib.kind {
13011301
NativeLibKind::Static { bundle: Some(false), .. }
13021302
| NativeLibKind::Dylib { .. }
@@ -1317,6 +1317,7 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
13171317
// These are included, no need to print them
13181318
NativeLibKind::Static { bundle: None | Some(true), .. }
13191319
| NativeLibKind::LinkArg
1320+
| NativeLibKind::WasmImportModule
13201321
| NativeLibKind::RawDylib => None,
13211322
}
13221323
})
@@ -2275,21 +2276,18 @@ fn add_native_libs_from_crate(
22752276

22762277
let mut last = (None, NativeLibKind::Unspecified, false);
22772278
for lib in native_libs {
2278-
let Some(name) = lib.name else {
2279-
continue;
2280-
};
22812279
if !relevant_lib(sess, lib) {
22822280
continue;
22832281
}
22842282

22852283
// Skip if this library is the same as the last.
2286-
last = if (lib.name, lib.kind, lib.verbatim) == last {
2284+
last = if (Some(lib.name), lib.kind, lib.verbatim) == last {
22872285
continue;
22882286
} else {
2289-
(lib.name, lib.kind, lib.verbatim)
2287+
(Some(lib.name), lib.kind, lib.verbatim)
22902288
};
22912289

2292-
let name = name.as_str();
2290+
let name = lib.name.as_str();
22932291
let verbatim = lib.verbatim;
22942292
match lib.kind {
22952293
NativeLibKind::Static { bundle, whole_archive } => {
@@ -2346,6 +2344,7 @@ fn add_native_libs_from_crate(
23462344
NativeLibKind::RawDylib => {
23472345
// Handled separately in `linker_with_args`.
23482346
}
2347+
NativeLibKind::WasmImportModule => {}
23492348
NativeLibKind::LinkArg => {
23502349
if link_static {
23512350
cmd.arg(name);

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ fn wasm_import_module_map(tcx: TyCtxt<'_>, cnum: CrateNum) -> FxHashMap<DefId, S
595595

596596
let mut ret = FxHashMap::default();
597597
for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
598-
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module);
598+
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module());
599599
let Some(module) = module else { continue };
600600
ret.extend(lib.foreign_items.iter().map(|id| {
601601
assert_eq!(id.krate, cnum);

compiler/rustc_codegen_ssa/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bitflags::bitflags! {
118118
#[derive(Clone, Debug, Encodable, Decodable, HashStable)]
119119
pub struct NativeLib {
120120
pub kind: NativeLibKind,
121-
pub name: Option<Symbol>,
121+
pub name: Symbol,
122122
pub filename: Option<Symbol>,
123123
pub cfg: Option<ast::MetaItem>,
124124
pub verbatim: bool,

compiler/rustc_metadata/src/native_libs.rs

+35-41
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn find_native_static_library(
4646
}
4747

4848
fn find_bundled_library(
49-
name: Option<Symbol>,
49+
name: Symbol,
5050
verbatim: Option<bool>,
5151
kind: NativeLibKind,
5252
has_cfg: bool,
@@ -58,7 +58,7 @@ fn find_bundled_library(
5858
{
5959
let verbatim = verbatim.unwrap_or(false);
6060
let search_paths = &sess.target_filesearch(PathKind::Native).search_path_dirs();
61-
return find_native_static_library(name.unwrap().as_str(), verbatim, search_paths, sess)
61+
return find_native_static_library(name.as_str(), verbatim, search_paths, sess)
6262
.file_name()
6363
.and_then(|s| s.to_str())
6464
.map(Symbol::intern);
@@ -336,10 +336,16 @@ impl<'tcx> Collector<'tcx> {
336336
if name.is_some() || kind.is_some() || modifiers.is_some() || cfg.is_some() {
337337
sess.emit_err(errors::IncompatibleWasmLink { span });
338338
}
339-
} else if name.is_none() {
340-
sess.emit_err(errors::LinkRequiresName { span: m.span });
341339
}
342340

341+
if wasm_import_module.is_some() {
342+
(name, kind) = (wasm_import_module, Some(NativeLibKind::WasmImportModule));
343+
}
344+
let Some((name, name_span)) = name else {
345+
sess.emit_err(errors::LinkRequiresName { span: m.span });
346+
continue;
347+
};
348+
343349
// Do this outside of the loop so that `import_name_type` can be specified before `kind`.
344350
if let Some((_, span)) = import_name_type {
345351
if kind != Some(NativeLibKind::RawDylib) {
@@ -349,8 +355,8 @@ impl<'tcx> Collector<'tcx> {
349355

350356
let dll_imports = match kind {
351357
Some(NativeLibKind::RawDylib) => {
352-
if let Some((name, span)) = name && name.as_str().contains('\0') {
353-
sess.emit_err(errors::RawDylibNoNul { span });
358+
if name.as_str().contains('\0') {
359+
sess.emit_err(errors::RawDylibNoNul { span: name_span });
354360
}
355361
foreign_mod_items
356362
.iter()
@@ -389,7 +395,6 @@ impl<'tcx> Collector<'tcx> {
389395
}
390396
};
391397

392-
let name = name.map(|(name, _)| name);
393398
let kind = kind.unwrap_or(NativeLibKind::Unspecified);
394399
let filename = find_bundled_library(name, verbatim, kind, cfg.is_some(), sess);
395400
self.libs.push(NativeLib {
@@ -398,7 +403,6 @@ impl<'tcx> Collector<'tcx> {
398403
kind,
399404
cfg,
400405
foreign_module: Some(it.owner_id.to_def_id()),
401-
wasm_import_module: wasm_import_module.map(|(name, _)| name),
402406
verbatim,
403407
dll_imports,
404408
});
@@ -415,11 +419,7 @@ impl<'tcx> Collector<'tcx> {
415419
self.tcx.sess.emit_err(errors::LibFrameworkApple);
416420
}
417421
if let Some(ref new_name) = lib.new_name {
418-
let any_duplicate = self
419-
.libs
420-
.iter()
421-
.filter_map(|lib| lib.name.as_ref())
422-
.any(|n| n.as_str() == lib.name);
422+
let any_duplicate = self.libs.iter().any(|n| n.name.as_str() == lib.name);
423423
if new_name.is_empty() {
424424
self.tcx.sess.emit_err(errors::EmptyRenamingTarget { lib_name: &lib.name });
425425
} else if !any_duplicate {
@@ -444,41 +444,36 @@ impl<'tcx> Collector<'tcx> {
444444
let mut existing = self
445445
.libs
446446
.drain_filter(|lib| {
447-
if let Some(lib_name) = lib.name {
448-
if lib_name.as_str() == passed_lib.name {
449-
// FIXME: This whole logic is questionable, whether modifiers are
450-
// involved or not, library reordering and kind overriding without
451-
// explicit `:rename` in particular.
452-
if lib.has_modifiers() || passed_lib.has_modifiers() {
453-
match lib.foreign_module {
454-
Some(def_id) => {
455-
self.tcx.sess.emit_err(errors::NoLinkModOverride {
456-
span: Some(self.tcx.def_span(def_id)),
457-
})
458-
}
459-
None => self
460-
.tcx
461-
.sess
462-
.emit_err(errors::NoLinkModOverride { span: None }),
463-
};
464-
}
465-
if passed_lib.kind != NativeLibKind::Unspecified {
466-
lib.kind = passed_lib.kind;
467-
}
468-
if let Some(new_name) = &passed_lib.new_name {
469-
lib.name = Some(Symbol::intern(new_name));
470-
}
471-
lib.verbatim = passed_lib.verbatim;
472-
return true;
447+
if lib.name.as_str() == passed_lib.name {
448+
// FIXME: This whole logic is questionable, whether modifiers are
449+
// involved or not, library reordering and kind overriding without
450+
// explicit `:rename` in particular.
451+
if lib.has_modifiers() || passed_lib.has_modifiers() {
452+
match lib.foreign_module {
453+
Some(def_id) => self.tcx.sess.emit_err(errors::NoLinkModOverride {
454+
span: Some(self.tcx.def_span(def_id)),
455+
}),
456+
None => {
457+
self.tcx.sess.emit_err(errors::NoLinkModOverride { span: None })
458+
}
459+
};
460+
}
461+
if passed_lib.kind != NativeLibKind::Unspecified {
462+
lib.kind = passed_lib.kind;
463+
}
464+
if let Some(new_name) = &passed_lib.new_name {
465+
lib.name = Symbol::intern(new_name);
473466
}
467+
lib.verbatim = passed_lib.verbatim;
468+
return true;
474469
}
475470
false
476471
})
477472
.collect::<Vec<_>>();
478473
if existing.is_empty() {
479474
// Add if not found
480475
let new_name: Option<&str> = passed_lib.new_name.as_deref();
481-
let name = Some(Symbol::intern(new_name.unwrap_or(&passed_lib.name)));
476+
let name = Symbol::intern(new_name.unwrap_or(&passed_lib.name));
482477
let sess = self.tcx.sess;
483478
let filename =
484479
find_bundled_library(name, passed_lib.verbatim, passed_lib.kind, false, sess);
@@ -488,7 +483,6 @@ impl<'tcx> Collector<'tcx> {
488483
kind: passed_lib.kind,
489484
cfg: None,
490485
foreign_module: None,
491-
wasm_import_module: None,
492486
verbatim: passed_lib.verbatim,
493487
dll_imports: Vec::new(),
494488
});

compiler/rustc_session/src/cstore.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ pub enum LinkagePreference {
6767
#[derive(Debug, Encodable, Decodable, HashStable_Generic)]
6868
pub struct NativeLib {
6969
pub kind: NativeLibKind,
70-
pub name: Option<Symbol>,
70+
pub name: Symbol,
7171
/// If packed_bundled_libs enabled, actual filename of library is stored.
7272
pub filename: Option<Symbol>,
7373
pub cfg: Option<ast::MetaItem>,
7474
pub foreign_module: Option<DefId>,
75-
pub wasm_import_module: Option<Symbol>,
7675
pub verbatim: Option<bool>,
7776
pub dll_imports: Vec<DllImport>,
7877
}
@@ -81,6 +80,10 @@ impl NativeLib {
8180
pub fn has_modifiers(&self) -> bool {
8281
self.verbatim.is_some() || self.kind.has_modifiers()
8382
}
83+
84+
pub fn wasm_import_module(&self) -> Option<Symbol> {
85+
if self.kind == NativeLibKind::WasmImportModule { Some(self.name) } else { None }
86+
}
8487
}
8588

8689
/// Different ways that the PE Format can decorate a symbol name.

compiler/rustc_session/src/utils.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ pub enum NativeLibKind {
3737
/// Argument which is passed to linker, relative order with libraries and other arguments
3838
/// is preserved
3939
LinkArg,
40+
41+
/// Module imported from WebAssembly
42+
WasmImportModule,
43+
4044
/// The library kind wasn't specified, `Dylib` is currently used as a default.
4145
Unspecified,
4246
}
@@ -50,7 +54,10 @@ impl NativeLibKind {
5054
NativeLibKind::Dylib { as_needed } | NativeLibKind::Framework { as_needed } => {
5155
as_needed.is_some()
5256
}
53-
NativeLibKind::RawDylib | NativeLibKind::Unspecified | NativeLibKind::LinkArg => false,
57+
NativeLibKind::RawDylib
58+
| NativeLibKind::Unspecified
59+
| NativeLibKind::LinkArg
60+
| NativeLibKind::WasmImportModule => false,
5461
}
5562
}
5663

tests/ui/linkage-attr/issue-109144.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![crate_type = "lib"]
2+
#[link(kind = "static", modifiers = "+whole-archive,+bundle")]
3+
//~^ ERROR `#[link]` attribute requires a `name = "string"` argument
4+
extern {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0459]: `#[link]` attribute requires a `name = "string"` argument
2+
--> $DIR/issue-109144.rs:2:1
3+
|
4+
LL | #[link(kind = "static", modifiers = "+whole-archive,+bundle")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `name` argument
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0459`.

0 commit comments

Comments
 (0)