Skip to content
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

Rollup of 9 pull requests #78451

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
9dadcf8
`warn` -> `debug` in collect
lcnr Oct 25, 2020
0e419ef
check for object safety violations in constants
lcnr Oct 25, 2020
00abef1
libc: 0.2.79 -> 0.2.80
nagisa Oct 25, 2020
8546a80
add fixme
lcnr Oct 26, 2020
60bcc58
debug log `AbstractConst::new`
lcnr Oct 26, 2020
0cf3ce4
build-manifest: refactor checksum generation into a struct
pietroalbini Oct 26, 2020
174ed0c
Remove tokens from foreign items in `TokenStripper`
Aaron1011 Oct 26, 2020
c2f4bbd
build-manifest: add BUILD_MANIFEST_CHECKSUM_CACHE
pietroalbini Oct 26, 2020
53fa22a
Fix some outdated comments
nox Oct 26, 2020
a3623e0
rustc_span: avoid hashing ExpnId tag when using cached hash
tgnottingham Sep 21, 2020
8cf1b0e
Uplift temporary-cstring-as-ptr into rustc
nathanwhit Aug 18, 2020
a2f4afe
Add basic test
nathanwhit Aug 18, 2020
8b65df0
Address review comments
nathanwhit Aug 18, 2020
ce95122
Update doctest
nathanwhit Aug 18, 2020
5643a06
Tweak diagnostic
nathanwhit Aug 18, 2020
737bfef
Change to warn by default / fix typo
nathanwhit Aug 23, 2020
1bcd245
Address review comments
nathanwhit Sep 21, 2020
576eb2a
Write docs for lint / fix review nit
nathanwhit Sep 22, 2020
5ac1688
Remove lint from clippy
nathanwhit Sep 22, 2020
6ba127d
Fix doctest
nathanwhit Sep 22, 2020
0569422
Implement -Z function-sections=yes|no
nox Oct 26, 2020
cb8b901
Address review comments
nathanwhit Oct 26, 2020
39941e6
Fix bootstrap doctest failure
nathanwhit Oct 27, 2020
650e3cb
Prefer new associated numeric consts in float error messages
Rustin170506 Oct 27, 2020
572cd35
Fix test
nathanwhit Oct 27, 2020
da64d07
Fix typo in comment
bugadani Oct 27, 2020
99320b9
Fix typos in arena comments
bugadani Oct 27, 2020
b01c74b
Fix typo in vec_graph
bugadani Oct 27, 2020
b2e935d
Rollup merge of #75671 - nathanwhit:cstring-temp-lint, r=oli-obk
jonas-schievink Oct 27, 2020
3861024
Rollup merge of #78365 - lcnr:const-eval-obj-safety, r=oli-obk
jonas-schievink Oct 27, 2020
3e49068
Rollup merge of #78378 - nagisa:nagisa/libc-up, r=Mark-Simulacrum
jonas-schievink Oct 27, 2020
944a4b0
Rollup merge of #78408 - Aaron1011:fix/remove-foreign-tokens, r=oli-obk
jonas-schievink Oct 27, 2020
163b322
Rollup merge of #78409 - pietroalbini:build-manifest-checksum-cache, …
jonas-schievink Oct 27, 2020
f29e191
Rollup merge of #78414 - nox:function-sections, r=nagisa,bjorn3
jonas-schievink Oct 27, 2020
afbc67b
Rollup merge of #78415 - tgnottingham:expn_id_tag_hash, r=Aaron1011
jonas-schievink Oct 27, 2020
2ddaa60
Rollup merge of #78431 - Rustin-Liu:rustin-patch-lint, r=estebank
jonas-schievink Oct 27, 2020
733e3c5
Rollup merge of #78447 - bugadani:typo, r=matthewjasper
jonas-schievink Oct 27, 2020
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
4 changes: 2 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1707,9 +1707,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"

[[package]]
name = "libc"
version = "0.2.79"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743"
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
dependencies = [
"rustc-std-workspace-core",
]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_arena/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ impl DropArena {
ptr::write(mem, object);
let result = &mut *mem;
// Record the destructor after doing the allocation as that may panic
// and would cause `object`'s destuctor to run twice if it was recorded before
// and would cause `object`'s destructor to run twice if it was recorded before
self.destructors
.borrow_mut()
.push(DropType { drop_fn: drop_for_type::<T>, obj: result as *mut T as *mut u8 });
Expand All @@ -560,7 +560,7 @@ impl DropArena {
mem::forget(vec.drain(..));

// Record the destructors after doing the allocation as that may panic
// and would cause `object`'s destuctor to run twice if it was recorded before
// and would cause `object`'s destructor to run twice if it was recorded before
for i in 0..len {
destructors.push(DropType {
drop_fn: drop_for_type::<T>,
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ pub fn target_machine_factory(
let (opt_level, _) = to_llvm_opt_settings(optlvl);
let use_softfp = sess.opts.cg.soft_float;

let ffunction_sections = sess.target.options.function_sections;
let ffunction_sections =
sess.opts.debugging_opts.function_sections.unwrap_or(sess.target.options.function_sections);
let fdata_sections = ffunction_sections;

let code_model = to_llvm_code_model(sess.code_model());
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/src/graph/vec_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<N: Idx> VecGraph<N> {

// Create the *edge starts* array. We are iterating over over
// the (sorted) edge pairs. We maintain the invariant that the
// length of the `node_starts` arary is enough to store the
// length of the `node_starts` array is enough to store the
// current source node -- so when we see that the source node
// for an edge is greater than the current length, we grow the
// edge-starts array by just enough.
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ impl mut_visit::MutVisitor for TokenStripper {
i.tokens = None;
mut_visit::noop_flat_map_item(i, self)
}
fn flat_map_foreign_item(
&mut self,
mut i: P<ast::ForeignItem>,
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
i.tokens = None;
mut_visit::noop_flat_map_foreign_item(i, self)
}
fn visit_block(&mut self, b: &mut P<ast::Block>) {
b.tokens = None;
mut_visit::noop_visit_block(b, self);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ fn test_debugging_options_tracking_hash() {
tracked!(force_overflow_checks, Some(true));
tracked!(force_unstable_if_unmarked, true);
tracked!(fuel, Some(("abc".to_string(), 99)));
tracked!(function_sections, Some(false));
tracked!(human_readable_cgu_names, true);
tracked!(inline_in_all_cgus, Some(true));
tracked!(insert_sideeffect, true);
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod early;
mod internal;
mod late;
mod levels;
mod methods;
mod non_ascii_idents;
mod nonstandard_style;
mod passes;
Expand All @@ -73,6 +74,7 @@ use rustc_span::Span;
use array_into_iter::ArrayIntoIter;
use builtin::*;
use internal::*;
use methods::*;
use non_ascii_idents::*;
use nonstandard_style::*;
use redundant_semicolon::*;
Expand Down Expand Up @@ -160,6 +162,7 @@ macro_rules! late_lint_passes {
ArrayIntoIter: ArrayIntoIter,
ClashingExternDeclarations: ClashingExternDeclarations::new(),
DropTraitConstraints: DropTraitConstraints,
TemporaryCStringAsPtr: TemporaryCStringAsPtr,
]
);
};
Expand Down
106 changes: 106 additions & 0 deletions compiler/rustc_lint/src/methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::LateContext;
use crate::LateLintPass;
use crate::LintContext;
use rustc_hir::{Expr, ExprKind, PathSegment};
use rustc_middle::ty;
use rustc_span::{symbol::sym, ExpnKind, Span};

declare_lint! {
/// The `temporary_cstring_as_ptr` lint detects getting the inner pointer of
/// a temporary `CString`.
///
/// ### Example
///
/// ```rust
/// # #![allow(unused)]
/// # use std::ffi::CString;
/// let c_str = CString::new("foo").unwrap().as_ptr();
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// The inner pointer of a `CString` lives only as long as the `CString` it
/// points to. Getting the inner pointer of a *temporary* `CString` allows the `CString`
/// to be dropped at the end of the statement, as it is not being referenced as far as the typesystem
/// is concerned. This means outside of the statement the pointer will point to freed memory, which
/// causes undefined behavior if the pointer is later dereferenced.
pub TEMPORARY_CSTRING_AS_PTR,
Warn,
"detects getting the inner pointer of a temporary `CString`"
}

declare_lint_pass!(TemporaryCStringAsPtr => [TEMPORARY_CSTRING_AS_PTR]);

fn in_macro(span: Span) -> bool {
if span.from_expansion() {
!matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Desugaring(..))
} else {
false
}
}

fn first_method_call<'tcx>(
expr: &'tcx Expr<'tcx>,
) -> Option<(&'tcx PathSegment<'tcx>, &'tcx [Expr<'tcx>])> {
if let ExprKind::MethodCall(path, _, args, _) = &expr.kind {
if args.iter().any(|e| e.span.from_expansion()) { None } else { Some((path, *args)) }
} else {
None
}
}

impl<'tcx> LateLintPass<'tcx> for TemporaryCStringAsPtr {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if in_macro(expr.span) {
return;
}

match first_method_call(expr) {
Some((path, args)) if path.ident.name == sym::as_ptr => {
let unwrap_arg = &args[0];
let as_ptr_span = path.ident.span;
match first_method_call(unwrap_arg) {
Some((path, args))
if path.ident.name == sym::unwrap || path.ident.name == sym::expect =>
{
let source_arg = &args[0];
lint_cstring_as_ptr(cx, as_ptr_span, source_arg, unwrap_arg);
}
_ => return,
}
}
_ => return,
}
}
}

fn lint_cstring_as_ptr(
cx: &LateContext<'_>,
as_ptr_span: Span,
source: &rustc_hir::Expr<'_>,
unwrap: &rustc_hir::Expr<'_>,
) {
let source_type = cx.typeck_results().expr_ty(source);
if let ty::Adt(def, substs) = source_type.kind() {
if cx.tcx.is_diagnostic_item(sym::result_type, def.did) {
if let ty::Adt(adt, _) = substs.type_at(0).kind() {
if cx.tcx.is_diagnostic_item(sym::cstring_type, adt.did) {
cx.struct_span_lint(TEMPORARY_CSTRING_AS_PTR, as_ptr_span, |diag| {
let mut diag = diag
.build("getting the inner pointer of a temporary `CString`");
diag.span_label(as_ptr_span, "this pointer will be invalid");
diag.span_label(
unwrap.span,
"this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime",
);
diag.note("pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned");
diag.help("for more information, see https://doc.rust-lang.org/reference/destructors.html");
diag.emit();
});
}
}
}
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ fn lint_literal<'tcx>(
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
lint.build(&format!("literal out of range for `{}`", t.name_str()))
.note(&format!(
"the literal `{}` does not fit into the type `{}` and will be converted to `std::{}::INFINITY`",
"the literal `{}` does not fit into the type `{}` and will be converted to `{}::INFINITY`",
cx.sess()
.source_map()
.span_to_snippet(lit.span)
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
// This list is in alphabetical order.
//
// If you add a new option, please update:
// - src/librustc_interface/tests.rs
// - compiler/rustc_interface/src/tests.rs
// - src/doc/rustc/src/codegen-options/index.md

ar: String = (String::new(), parse_string, [UNTRACKED],
Expand Down Expand Up @@ -814,7 +814,7 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
// This list is in alphabetical order.
//
// If you add a new option, please update:
// - src/librustc_interface/tests.rs
// - compiler/rustc_interface/src/tests.rs
// - src/doc/rustc/src/codegen-options/index.md
}

Expand All @@ -825,7 +825,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
// This list is in alphabetical order.
//
// If you add a new option, please update:
// - src/librustc_interface/tests.rs
// - compiler/rustc_interface/src/tests.rs

allow_features: Option<Vec<String>> = (None, parse_opt_comma_list, [TRACKED],
"only allow the listed language features to be enabled in code (space separated)"),
Expand Down Expand Up @@ -904,6 +904,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"force all crates to be `rustc_private` unstable (default: no)"),
fuel: Option<(String, u64)> = (None, parse_optimization_fuel, [TRACKED],
"set the optimization fuel quota for a crate"),
function_sections: Option<bool> = (None, parse_opt_bool, [TRACKED],
"whether each function should go in its own section"),
graphviz_dark_mode: bool = (false, parse_bool, [UNTRACKED],
"use dark-themed colors in graphviz output (default: no)"),
graphviz_font: String = ("Courier, monospace".to_string(), parse_string, [UNTRACKED],
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,14 +738,14 @@ impl<D: Decoder> Decodable<D> for Span {
}

/// Calls the provided closure, using the provided `SourceMap` to format
/// any spans that are debug-printed during the closure'e exectuino.
/// any spans that are debug-printed during the closure's execution.
///
/// Normally, the global `TyCtxt` is used to retrieve the `SourceMap`
/// (see `rustc_interface::callbacks::span_debug1). However, some parts
/// of the compiler (e.g. `rustc_parse`) may debug-print `Span`s before
/// a `TyCtxt` is available. In this case, we fall back to
/// the `SourceMap` provided to this function. If that is not available,
/// we fall back to printing the raw `Span` field values
/// we fall back to printing the raw `Span` field values.
pub fn with_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) -> T {
SESSION_GLOBALS.with(|session_globals| {
*session_globals.source_map.borrow_mut() = Some(source_map);
Expand Down Expand Up @@ -1925,9 +1925,7 @@ impl<CTX: HashStableContext> HashStable<CTX> for ExpnId {
return;
}

TAG_NOT_ROOT.hash_stable(ctx, hasher);
let index = self.as_u32() as usize;

let res = CACHE.with(|cache| cache.borrow().get(index).copied().flatten());

if let Some(res) = res {
Expand All @@ -1936,6 +1934,7 @@ impl<CTX: HashStableContext> HashStable<CTX> for ExpnId {
let new_len = index + 1;

let mut sub_hasher = StableHasher::new();
TAG_NOT_ROOT.hash_stable(ctx, &mut sub_hasher);
self.expn_data().hash_stable(ctx, &mut sub_hasher);
let sub_hash: Fingerprint = sub_hasher.finish();

Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ symbols! {
ArgumentV1,
Arguments,
C,
CString,
Center,
Clone,
Copy,
Expand Down Expand Up @@ -261,6 +262,7 @@ symbols! {
arm_target_feature,
array,
arrays,
as_ptr,
as_str,
asm,
assert,
Expand Down Expand Up @@ -310,6 +312,7 @@ symbols! {
breakpoint,
bridge,
bswap,
c_str,
c_variadic,
call,
call_mut,
Expand Down Expand Up @@ -397,6 +400,7 @@ symbols! {
crate_type,
crate_visibility_modifier,
crt_dash_static: "crt-static",
cstring_type,
ctlz,
ctlz_nonzero,
ctpop,
Expand Down Expand Up @@ -478,6 +482,7 @@ symbols! {
existential_type,
exp2f32,
exp2f64,
expect,
expected,
expf32,
expf64,
Expand All @@ -501,6 +506,7 @@ symbols! {
fadd_fast,
fdiv_fast,
feature,
ffi,
ffi_const,
ffi_pure,
ffi_returns_twice,
Expand Down Expand Up @@ -1170,6 +1176,7 @@ symbols! {
unused_qualifications,
unwind,
unwind_attributes,
unwrap,
unwrap_or,
use_extern_macros,
use_nested_groups,
Expand Down
Loading