Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1453531

Browse files
Initial support for dynamically linked crates
1 parent 62c5f58 commit 1453531

File tree

70 files changed

+1534
-117
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1534
-117
lines changed

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4031,6 +4031,7 @@ dependencies = [
40314031
"rustc_session",
40324032
"rustc_span",
40334033
"rustc_target",
4034+
"tempfile",
40344035
"tracing",
40354036
]
40364037

‎compiler/rustc_ast_lowering/src/item.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
13101310
// create a fake body so that the entire rest of the compiler doesn't have to deal with
13111311
// this as a special case.
13121312
return self.lower_fn_body(decl, contract, |this| {
1313-
if attrs.iter().any(|a| a.has_name(sym::rustc_intrinsic)) {
1313+
if attrs.iter().any(|a| a.has_name(sym::rustc_intrinsic))
1314+
|| this.tcx.is_sdylib_interface_build()
1315+
{
13141316
let span = this.lower_span(span);
13151317
let empty_block = hir::Block {
13161318
hir_id: this.next_id(),

‎compiler/rustc_ast_passes/src/ast_validation.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ struct AstValidator<'a> {
8484

8585
lint_node_id: NodeId,
8686

87+
is_sdylib_interface: bool,
88+
8789
lint_buffer: &'a mut LintBuffer,
8890
}
8991

@@ -952,7 +954,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
952954
self.check_defaultness(item.span, *defaultness);
953955

954956
let is_intrinsic = item.attrs.iter().any(|a| a.has_name(sym::rustc_intrinsic));
955-
if body.is_none() && !is_intrinsic {
957+
if body.is_none() && !is_intrinsic && !self.is_sdylib_interface{
956958
self.dcx().emit_err(errors::FnWithoutBody {
957959
span: item.span,
958960
replace_span: self.ending_semi_or_hi(item.span),
@@ -1441,7 +1443,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14411443
});
14421444
}
14431445
AssocItemKind::Fn(box Fn { body, .. }) => {
1444-
if body.is_none() {
1446+
if body.is_none() && !self.is_sdylib_interface{
14451447
self.dcx().emit_err(errors::AssocFnWithoutBody {
14461448
span: item.span,
14471449
replace_span: self.ending_semi_or_hi(item.span),
@@ -1689,6 +1691,7 @@ pub fn check_crate(
16891691
sess: &Session,
16901692
features: &Features,
16911693
krate: &Crate,
1694+
is_sdylib_interface: bool,
16921695
lints: &mut LintBuffer,
16931696
) -> bool {
16941697
let mut validator = AstValidator {
@@ -1701,6 +1704,7 @@ pub fn check_crate(
17011704
disallow_tilde_const: Some(TildeConstReason::Item),
17021705
extern_mod_safety: None,
17031706
lint_node_id: CRATE_NODE_ID,
1707+
is_sdylib_interface,
17041708
lint_buffer: lints,
17051709
};
17061710
visit::walk_crate(&mut validator, krate);

‎compiler/rustc_ast_pretty/src/pprust/mod.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use std::borrow::Cow;
77
use rustc_ast as ast;
88
use rustc_ast::token::{Token, TokenKind};
99
use rustc_ast::tokenstream::{TokenStream, TokenTree};
10-
pub use state::{AnnNode, Comments, PpAnn, PrintState, State, print_crate};
10+
pub use state::{
11+
AnnNode, Comments, PpAnn, PrintState, State, print_crate, print_crate_as_interface,
12+
};
1113

1214
/// Print the token kind precisely, without converting `$crate` into its respective crate name.
1315
pub fn token_kind_to_string(tok: &TokenKind) -> Cow<'static, str> {

‎compiler/rustc_ast_pretty/src/pprust/state.rs‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ pub struct State<'a> {
221221
pub s: pp::Printer,
222222
comments: Option<Comments<'a>>,
223223
ann: &'a (dyn PpAnn + 'a),
224+
is_sdylib_interface: bool,
224225
}
225226

226227
const INDENT_UNIT: isize = 4;
@@ -236,10 +237,37 @@ pub fn print_crate<'a>(
236237
is_expanded: bool,
237238
edition: Edition,
238239
g: &AttrIdGenerator,
240+
) -> String {
241+
let mut s = State {
242+
s: pp::Printer::new(),
243+
comments: Some(Comments::new(sm, filename, input)),
244+
ann,
245+
is_sdylib_interface: false,
246+
};
247+
248+
print_crate_inner(&mut s, krate, is_expanded, edition, g);
249+
s.s.eof()
250+
}
251+
252+
pub fn print_crate_as_interface(
253+
krate: &ast::Crate,
254+
edition: Edition,
255+
g: &AttrIdGenerator,
239256
) -> String {
240257
let mut s =
241-
State { s: pp::Printer::new(), comments: Some(Comments::new(sm, filename, input)), ann };
258+
State { s: pp::Printer::new(), comments: None,ann:&NoAnn,is_sdylib_interface:true };
242259

260+
print_crate_inner(&mut s, krate, false, edition, g);
261+
s.s.eof()
262+
}
263+
264+
fn print_crate_inner<'a>(
265+
s: &mut State<'a>,
266+
krate: &ast::Crate,
267+
is_expanded: bool,
268+
edition: Edition,
269+
g: &AttrIdGenerator,
270+
) {
243271
// We need to print shebang before anything else
244272
// otherwise the resulting code will not compile
245273
// and shebang will be useless.
@@ -282,8 +310,7 @@ pub fn print_crate<'a>(
282310
s.print_item(item);
283311
}
284312
s.print_remaining_comments();
285-
s.ann.post(&mut s, AnnNode::Crate(krate));
286-
s.s.eof()
313+
s.ann.post(s, AnnNode::Crate(krate));
287314
}
288315

289316
/// Should two consecutive tokens be printed with a space between them?
@@ -1111,7 +1138,7 @@ impl<'a> PrintState<'a> for State<'a> {
11111138

11121139
impl<'a> State<'a> {
11131140
pub fn new() -> State<'a> {
1114-
State { s: pp::Printer::new(), comments: None, ann: &NoAnn }
1141+
State { s: pp::Printer::new(), comments: None, ann: &NoAnn,is_sdylib_interface:false }
11151142
}
11161143

11171144
fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)

‎compiler/rustc_ast_pretty/src/pprust/state/item.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ impl<'a> State<'a> {
160160

161161
/// Pretty-prints an item.
162162
pub(crate) fn print_item(&mut self, item: &ast::Item) {
163+
if self.is_sdylib_interface && item.span.is_dummy() {
164+
// Do not print prelude for interface files.
165+
return;
166+
}
163167
self.hardbreak_if_not_bol();
164168
self.maybe_print_comment(item.span.lo());
165169
self.print_outer_attributes(&item.attrs);
@@ -682,6 +686,13 @@ impl<'a> State<'a> {
682686
self.print_contract(contract);
683687
}
684688
if let Some((body, (cb, ib))) = body_cb_ib {
689+
if self.is_sdylib_interface {
690+
self.word(";");
691+
self.end(ib); // end inner head-block
692+
self.end(cb); // end outer head-block
693+
return;
694+
}
695+
685696
self.nbsp();
686697
self.print_block_with_attrs(body, attrs, cb, ib);
687698
} else {

‎compiler/rustc_codegen_gcc/src/back/lto.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ use crate::{GccCodegenBackend, GccContext, SyncContext, to_gcc_opt_level};
4444

4545
pub fn crate_type_allows_lto(crate_type: CrateType) -> bool {
4646
match crate_type {
47-
CrateType::Executable | CrateType::Dylib | CrateType::Staticlib | CrateType::Cdylib => true,
47+
CrateType::Executable
48+
| CrateType::Dylib
49+
| CrateType::Staticlib
50+
| CrateType::Cdylib
51+
| CrateType::Sdylib => true,
4852
CrateType::Rlib | CrateType::ProcMacro => false,
4953
}
5054
}

‎compiler/rustc_codegen_llvm/src/back/lto.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ fn crate_type_allows_lto(crate_type: CrateType) -> bool {
4242
| CrateType::Dylib
4343
| CrateType::Staticlib
4444
| CrateType::Cdylib
45-
| CrateType::ProcMacro => true,
45+
| CrateType::ProcMacro
46+
| CrateType::Sdylib => true,
4647
CrateType::Rlib => false,
4748
}
4849
}

‎compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ pub(crate) fn needs_gdb_debug_scripts_section(cx: &CodegenCx<'_, '_>) -> bool {
9595
// in the `.debug_gdb_scripts` section. For that reason, we make sure that the
9696
// section is only emitted for leaf crates.
9797
let embed_visualizers = cx.tcx.crate_types().iter().any(|&crate_type| match crate_type {
98-
CrateType::Executable | CrateType::Dylib | CrateType::Cdylib | CrateType::Staticlib => {
98+
CrateType::Executable
99+
| CrateType::Dylib
100+
| CrateType::Cdylib
101+
| CrateType::Staticlib
102+
| CrateType::Sdylib => {
99103
// These are crate types for which we will embed pretty printers since they
100104
// are treated as leaf crates.
101105
true

‎compiler/rustc_codegen_ssa/src/back/link.rs‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,9 +1053,10 @@ fn link_natively(
10531053
strip_with_external_utility(sess, stripcmd, out_filename, &["--strip-debug"])
10541054
}
10551055
// Per the manpage, `-x` is the maximum safe strip level for dynamic libraries. (#93988)
1056-
(Strip::Symbols, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro) => {
1057-
strip_with_external_utility(sess, stripcmd, out_filename, &["-x"])
1058-
}
1056+
(
1057+
Strip::Symbols,
1058+
CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro | CrateType::Sdylib,
1059+
) => strip_with_external_utility(sess, stripcmd, out_filename, &["-x"]),
10591060
(Strip::Symbols, _) => {
10601061
strip_with_external_utility(sess, stripcmd, out_filename, &["--strip-all"])
10611062
}
@@ -1243,8 +1244,10 @@ fn add_sanitizer_libraries(
12431244
// which should be linked to both executables and dynamic libraries.
12441245
// Everywhere else the runtimes are currently distributed as static
12451246
// libraries which should be linked to executables only.
1246-
if matches!(crate_type, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro)
1247-
&& !(sess.target.is_like_darwin || sess.target.is_like_msvc)
1247+
if matches!(
1248+
crate_type,
1249+
CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro | CrateType::Sdylib
1250+
) && !(sess.target.is_like_darwin || sess.target.is_like_msvc)
12481251
{
12491252
return;
12501253
}
@@ -1938,6 +1941,7 @@ fn add_late_link_args(
19381941
codegen_results: &CodegenResults,
19391942
) {
19401943
let any_dynamic_crate = crate_type == CrateType::Dylib
1944+
|| crate_type == CrateType::Sdylib
19411945
|| codegen_results.crate_info.dependency_formats.iter().any(|(ty, list)| {
19421946
*ty == crate_type && list.iter().any(|&linkage| linkage == Linkage::Dynamic)
19431947
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /