Skip to content

Commit d5aba8d

Browse files
committed
Auto merge of rust-lang#3741 - rust-lang:rustup-2024-07-09, r=RalfJung
Automatic Rustup
2 parents 964f6d9 + 1dcc342 commit d5aba8d

File tree

357 files changed

+7496
-3180
lines changed

Some content is hidden

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

357 files changed

+7496
-3180
lines changed

compiler/rustc_ast/src/ast_traits.rs

-33
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use crate::{AssocItem, Expr, ForeignItem, Item, NodeId};
1010
use crate::{AttrItem, AttrKind, Block, Pat, Path, Ty, Visibility};
1111
use crate::{AttrVec, Attribute, Stmt, StmtKind};
1212

13-
use rustc_span::Span;
14-
1513
use std::fmt;
1614
use std::marker::PhantomData;
1715

@@ -91,37 +89,6 @@ impl<T: AstDeref<Target: HasNodeId>> HasNodeId for T {
9189
}
9290
}
9391

94-
/// A trait for AST nodes having a span.
95-
pub trait HasSpan {
96-
fn span(&self) -> Span;
97-
}
98-
99-
macro_rules! impl_has_span {
100-
($($T:ty),+ $(,)?) => {
101-
$(
102-
impl HasSpan for $T {
103-
fn span(&self) -> Span {
104-
self.span
105-
}
106-
}
107-
)+
108-
};
109-
}
110-
111-
impl_has_span!(AssocItem, Block, Expr, ForeignItem, Item, Pat, Path, Stmt, Ty, Visibility);
112-
113-
impl<T: AstDeref<Target: HasSpan>> HasSpan for T {
114-
fn span(&self) -> Span {
115-
self.ast_deref().span()
116-
}
117-
}
118-
119-
impl HasSpan for AttrItem {
120-
fn span(&self) -> Span {
121-
self.span()
122-
}
123-
}
124-
12592
/// A trait for AST nodes having (or not having) collected tokens.
12693
pub trait HasTokens {
12794
fn tokens(&self) -> Option<&LazyAttrTokenStream>;

compiler/rustc_ast/src/attr/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ impl Attribute {
202202
}
203203
}
204204

205-
pub fn tokens(&self) -> TokenStream {
205+
// Named `get_tokens` to distinguish it from the `<Attribute as HasTokens>::tokens` method.
206+
pub fn get_tokens(&self) -> TokenStream {
206207
match &self.kind {
207208
AttrKind::Normal(normal) => TokenStream::new(
208209
normal

compiler/rustc_ast/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub mod tokenstream;
4444
pub mod visit;
4545

4646
pub use self::ast::*;
47-
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasSpan, HasTokens};
47+
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};
4848

4949
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5050

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ fn visit_attr_tt<T: MutVisitor>(tt: &mut AttrTokenTree, vis: &mut T) {
704704
visit_attr_tts(tts, vis);
705705
visit_delim_span(dspan, vis);
706706
}
707-
AttrTokenTree::Attributes(AttributesData { attrs, tokens }) => {
707+
AttrTokenTree::AttrsTarget(AttrsTarget { attrs, tokens }) => {
708708
visit_attrs(attrs, vis);
709709
visit_lazy_tts_opt_mut(Some(tokens), vis);
710710
}

compiler/rustc_ast/src/tokenstream.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! ownership of the original.
1515
1616
use crate::ast::{AttrStyle, StmtKind};
17-
use crate::ast_traits::{HasAttrs, HasSpan, HasTokens};
17+
use crate::ast_traits::{HasAttrs, HasTokens};
1818
use crate::token::{self, Delimiter, Nonterminal, Token, TokenKind};
1919
use crate::AttrVec;
2020

@@ -170,8 +170,8 @@ pub enum AttrTokenTree {
170170
Delimited(DelimSpan, DelimSpacing, Delimiter, AttrTokenStream),
171171
/// Stores the attributes for an attribute target,
172172
/// along with the tokens for that attribute target.
173-
/// See `AttributesData` for more information
174-
Attributes(AttributesData),
173+
/// See `AttrsTarget` for more information
174+
AttrsTarget(AttrsTarget),
175175
}
176176

177177
impl AttrTokenStream {
@@ -180,7 +180,7 @@ impl AttrTokenStream {
180180
}
181181

182182
/// Converts this `AttrTokenStream` to a plain `Vec<TokenTree>`.
183-
/// During conversion, `AttrTokenTree::Attributes` get 'flattened'
183+
/// During conversion, `AttrTokenTree::AttrsTarget` get 'flattened'
184184
/// back to a `TokenStream` of the form `outer_attr attr_target`.
185185
/// If there are inner attributes, they are inserted into the proper
186186
/// place in the attribute target tokens.
@@ -199,13 +199,13 @@ impl AttrTokenStream {
199199
TokenStream::new(stream.to_token_trees()),
200200
))
201201
}
202-
AttrTokenTree::Attributes(data) => {
203-
let idx = data
202+
AttrTokenTree::AttrsTarget(target) => {
203+
let idx = target
204204
.attrs
205205
.partition_point(|attr| matches!(attr.style, crate::AttrStyle::Outer));
206-
let (outer_attrs, inner_attrs) = data.attrs.split_at(idx);
206+
let (outer_attrs, inner_attrs) = target.attrs.split_at(idx);
207207

208-
let mut target_tokens = data.tokens.to_attr_token_stream().to_token_trees();
208+
let mut target_tokens = target.tokens.to_attr_token_stream().to_token_trees();
209209
if !inner_attrs.is_empty() {
210210
let mut found = false;
211211
// Check the last two trees (to account for a trailing semi)
@@ -227,7 +227,7 @@ impl AttrTokenStream {
227227

228228
let mut stream = TokenStream::default();
229229
for inner_attr in inner_attrs {
230-
stream.push_stream(inner_attr.tokens());
230+
stream.push_stream(inner_attr.get_tokens());
231231
}
232232
stream.push_stream(delim_tokens.clone());
233233
*tree = TokenTree::Delimited(*span, *spacing, *delim, stream);
@@ -242,7 +242,7 @@ impl AttrTokenStream {
242242
);
243243
}
244244
for attr in outer_attrs {
245-
res.extend(attr.tokens().0.iter().cloned());
245+
res.extend(attr.get_tokens().0.iter().cloned());
246246
}
247247
res.extend(target_tokens);
248248
}
@@ -262,7 +262,7 @@ impl AttrTokenStream {
262262
/// have an `attrs` field containing the `#[cfg(FALSE)]` attr,
263263
/// and a `tokens` field storing the (unparsed) tokens `struct Foo {}`
264264
#[derive(Clone, Debug, Encodable, Decodable)]
265-
pub struct AttributesData {
265+
pub struct AttrsTarget {
266266
/// Attributes, both outer and inner.
267267
/// These are stored in the original order that they were parsed in.
268268
pub attrs: AttrVec,
@@ -436,17 +436,17 @@ impl TokenStream {
436436
TokenStream::new(vec![TokenTree::token_alone(kind, span)])
437437
}
438438

439-
pub fn from_ast(node: &(impl HasAttrs + HasSpan + HasTokens + fmt::Debug)) -> TokenStream {
439+
pub fn from_ast(node: &(impl HasAttrs + HasTokens + fmt::Debug)) -> TokenStream {
440440
let Some(tokens) = node.tokens() else {
441-
panic!("missing tokens for node at {:?}: {:?}", node.span(), node);
441+
panic!("missing tokens for node: {:?}", node);
442442
};
443443
let attrs = node.attrs();
444444
let attr_stream = if attrs.is_empty() {
445445
tokens.to_attr_token_stream()
446446
} else {
447-
let attr_data =
448-
AttributesData { attrs: attrs.iter().cloned().collect(), tokens: tokens.clone() };
449-
AttrTokenStream::new(vec![AttrTokenTree::Attributes(attr_data)])
447+
let target =
448+
AttrsTarget { attrs: attrs.iter().cloned().collect(), tokens: tokens.clone() };
449+
AttrTokenStream::new(vec![AttrTokenTree::AttrsTarget(target)])
450450
};
451451
TokenStream::new(attr_stream.to_token_trees())
452452
}
@@ -765,6 +765,7 @@ mod size_asserts {
765765
static_assert_size!(AttrTokenStream, 8);
766766
static_assert_size!(AttrTokenTree, 32);
767767
static_assert_size!(LazyAttrTokenStream, 8);
768+
static_assert_size!(Option<LazyAttrTokenStream>, 8); // must be small, used in many AST nodes
768769
static_assert_size!(TokenStream, 8);
769770
static_assert_size!(TokenTree, 32);
770771
// tidy-alphabetical-end

compiler/rustc_borrowck/src/lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,12 @@ impl<'a, 'mir, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'mir, 'tcx, R>
727727
}
728728
self.mutate_place(loc, (*destination, span), Deep, flow_state);
729729
}
730+
TerminatorKind::TailCall { func, args, fn_span: _ } => {
731+
self.consume_operand(loc, (func, span), flow_state);
732+
for arg in args {
733+
self.consume_operand(loc, (&arg.node, arg.span), flow_state);
734+
}
735+
}
730736
TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {
731737
self.consume_operand(loc, (cond, span), flow_state);
732738
if let AssertKind::BoundsCheck { len, index } = &**msg {
@@ -813,9 +819,8 @@ impl<'a, 'mir, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'mir, 'tcx, R>
813819

814820
TerminatorKind::UnwindResume
815821
| TerminatorKind::Return
822+
| TerminatorKind::TailCall { .. }
816823
| TerminatorKind::CoroutineDrop => {
817-
// Returning from the function implicitly kills storage for all locals and statics.
818-
// Often, the storage will already have been killed by an explicit
819824
// StorageDead, but we don't always emit those (notably on unwind paths),
820825
// so this "extra check" serves as a kind of backup.
821826
let borrow_set = self.borrow_set.clone();

compiler/rustc_borrowck/src/polonius/loan_invalidations.rs

+6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ impl<'cx, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'cx, 'tcx> {
125125
}
126126
self.mutate_place(location, *destination, Deep);
127127
}
128+
TerminatorKind::TailCall { func, args, .. } => {
129+
self.consume_operand(location, func);
130+
for arg in args {
131+
self.consume_operand(location, &arg.node);
132+
}
133+
}
128134
TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {
129135
self.consume_operand(location, cond);
130136
use rustc_middle::mir::AssertKind;

compiler/rustc_borrowck/src/type_check/mod.rs

+83-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_span::def_id::CRATE_DEF_ID;
3838
use rustc_span::source_map::Spanned;
3939
use rustc_span::symbol::sym;
4040
use rustc_span::Span;
41+
use rustc_span::DUMMY_SP;
4142
use rustc_target::abi::{FieldIdx, FIRST_VARIANT};
4243
use rustc_trait_selection::traits::query::type_op::custom::scrape_region_constraints;
4344
use rustc_trait_selection::traits::query::type_op::custom::CustomTypeOp;
@@ -49,6 +50,7 @@ use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
4950
use rustc_mir_dataflow::move_paths::MoveData;
5051
use rustc_mir_dataflow::ResultsCursor;
5152

53+
use crate::renumber::RegionCtxt;
5254
use crate::session_diagnostics::{MoveUnsized, SimdIntrinsicArgConst};
5355
use crate::{
5456
borrow_set::BorrowSet,
@@ -1352,7 +1354,14 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13521354
}
13531355
// FIXME: check the values
13541356
}
1355-
TerminatorKind::Call { func, args, destination, call_source, target, .. } => {
1357+
TerminatorKind::Call { func, args, .. }
1358+
| TerminatorKind::TailCall { func, args, .. } => {
1359+
let call_source = match term.kind {
1360+
TerminatorKind::Call { call_source, .. } => call_source,
1361+
TerminatorKind::TailCall { .. } => CallSource::Normal,
1362+
_ => unreachable!(),
1363+
};
1364+
13561365
self.check_operand(func, term_location);
13571366
for arg in args {
13581367
self.check_operand(&arg.node, term_location);
@@ -1425,7 +1434,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
14251434
);
14261435
}
14271436

1428-
self.check_call_dest(body, term, &sig, *destination, *target, term_location);
1437+
if let TerminatorKind::Call { destination, target, .. } = term.kind {
1438+
self.check_call_dest(body, term, &sig, destination, target, term_location);
1439+
}
14291440

14301441
// The ordinary liveness rules will ensure that all
14311442
// regions in the type of the callee are live here. We
@@ -1443,7 +1454,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
14431454
.add_location(region_vid, term_location);
14441455
}
14451456

1446-
self.check_call_inputs(body, term, func, &sig, args, term_location, *call_source);
1457+
self.check_call_inputs(body, term, func, &sig, args, term_location, call_source);
14471458
}
14481459
TerminatorKind::Assert { cond, msg, .. } => {
14491460
self.check_operand(cond, term_location);
@@ -1675,6 +1686,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
16751686
span_mirbug!(self, block_data, "return on cleanup block")
16761687
}
16771688
}
1689+
TerminatorKind::TailCall { .. } => {
1690+
if is_cleanup {
1691+
span_mirbug!(self, block_data, "tailcall on cleanup block")
1692+
}
1693+
}
16781694
TerminatorKind::CoroutineDrop { .. } => {
16791695
if is_cleanup {
16801696
span_mirbug!(self, block_data, "coroutine_drop in cleanup block")
@@ -2319,7 +2335,57 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
23192335
let cast_ty_from = CastTy::from_ty(ty_from);
23202336
let cast_ty_to = CastTy::from_ty(*ty);
23212337
match (cast_ty_from, cast_ty_to) {
2322-
(Some(CastTy::Ptr(_)), Some(CastTy::Ptr(_))) => (),
2338+
(Some(CastTy::Ptr(src)), Some(CastTy::Ptr(dst))) => {
2339+
let mut normalize = |t| self.normalize(t, location);
2340+
let src_tail =
2341+
tcx.struct_tail_with_normalize(src.ty, &mut normalize, || ());
2342+
let dst_tail =
2343+
tcx.struct_tail_with_normalize(dst.ty, &mut normalize, || ());
2344+
2345+
// This checks (lifetime part of) vtable validity for pointer casts,
2346+
// which is irrelevant when there are aren't principal traits on both sides (aka only auto traits).
2347+
//
2348+
// Note that other checks (such as denying `dyn Send` -> `dyn Debug`) are in `rustc_hir_typeck`.
2349+
if let ty::Dynamic(src_tty, ..) = src_tail.kind()
2350+
&& let ty::Dynamic(dst_tty, ..) = dst_tail.kind()
2351+
&& src_tty.principal().is_some()
2352+
&& dst_tty.principal().is_some()
2353+
{
2354+
// Remove auto traits.
2355+
// Auto trait checks are handled in `rustc_hir_typeck` as FCW.
2356+
let src_obj = tcx.mk_ty_from_kind(ty::Dynamic(
2357+
tcx.mk_poly_existential_predicates(
2358+
&src_tty.without_auto_traits().collect::<Vec<_>>(),
2359+
),
2360+
tcx.lifetimes.re_static,
2361+
ty::Dyn,
2362+
));
2363+
let dst_obj = tcx.mk_ty_from_kind(ty::Dynamic(
2364+
tcx.mk_poly_existential_predicates(
2365+
&dst_tty.without_auto_traits().collect::<Vec<_>>(),
2366+
),
2367+
tcx.lifetimes.re_static,
2368+
ty::Dyn,
2369+
));
2370+
2371+
// Replace trait object lifetimes with fresh vars, to allow casts like
2372+
// `*mut dyn FnOnce() + 'a` -> `*mut dyn FnOnce() + 'static`,
2373+
let src_obj =
2374+
freshen_single_trait_object_lifetime(self.infcx, src_obj);
2375+
let dst_obj =
2376+
freshen_single_trait_object_lifetime(self.infcx, dst_obj);
2377+
2378+
debug!(?src_tty, ?dst_tty, ?src_obj, ?dst_obj);
2379+
2380+
self.eq_types(
2381+
src_obj,
2382+
dst_obj,
2383+
location.to_locations(),
2384+
ConstraintCategory::Cast { unsize_to: None },
2385+
)
2386+
.unwrap();
2387+
}
2388+
}
23232389
_ => {
23242390
span_mirbug!(
23252391
self,
@@ -2842,3 +2908,16 @@ impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {
28422908
Ok(output)
28432909
}
28442910
}
2911+
2912+
fn freshen_single_trait_object_lifetime<'tcx>(
2913+
infcx: &BorrowckInferCtxt<'tcx>,
2914+
ty: Ty<'tcx>,
2915+
) -> Ty<'tcx> {
2916+
let &ty::Dynamic(tty, _, dyn_kind @ ty::Dyn) = ty.kind() else { bug!("expected trait object") };
2917+
2918+
let fresh = infcx
2919+
.next_region_var(rustc_infer::infer::RegionVariableOrigin::MiscVariable(DUMMY_SP), || {
2920+
RegionCtxt::Unknown
2921+
});
2922+
infcx.tcx.mk_ty_from_kind(ty::Dynamic(tty, fresh, dyn_kind))
2923+
}

compiler/rustc_builtin_macros/src/cfg_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl CfgEval<'_> {
193193

194194
// Re-parse the tokens, setting the `capture_cfg` flag to save extra information
195195
// to the captured `AttrTokenStream` (specifically, we capture
196-
// `AttrTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
196+
// `AttrTokenTree::AttrsTarget` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
197197
let mut parser = Parser::new(&self.0.sess.psess, orig_tokens, None);
198198
parser.capture_cfg = true;
199199
match parse_annotatable_with(&mut parser) {

compiler/rustc_codegen_cranelift/src/base.rs

+5
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,11 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
491491
)
492492
});
493493
}
494+
// FIXME(explicit_tail_calls): add support for tail calls to the cranelift backend, once cranelift supports tail calls
495+
TerminatorKind::TailCall { fn_span, .. } => span_bug!(
496+
*fn_span,
497+
"tail calls are not yet supported in `rustc_codegen_cranelift` backend"
498+
),
494499
TerminatorKind::InlineAsm {
495500
template,
496501
operands,

compiler/rustc_codegen_cranelift/src/constant.rs

+1
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
567567
{
568568
return None;
569569
}
570+
TerminatorKind::TailCall { .. } => return None,
570571
TerminatorKind::Call { .. } => {}
571572
}
572573
}

compiler/rustc_codegen_ssa/src/mir/analyze.rs

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKi
281281
| TerminatorKind::UnwindResume
282282
| TerminatorKind::UnwindTerminate(_)
283283
| TerminatorKind::Return
284+
| TerminatorKind::TailCall { .. }
284285
| TerminatorKind::CoroutineDrop
285286
| TerminatorKind::Unreachable
286287
| TerminatorKind::SwitchInt { .. }

0 commit comments

Comments
 (0)