Skip to content

Commit 831fca2

Browse files
authored
Merge branch 'master' into if-let-guard-stable
2 parents 918e5f0 + 356f2d0 commit 831fca2

File tree

301 files changed

+5934
-3037
lines changed

Some content is hidden

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

301 files changed

+5934
-3037
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,6 +3809,7 @@ dependencies = [
38093809
"rustc_abi",
38103810
"rustc_ast",
38113811
"rustc_attr_data_structures",
3812+
"rustc_attr_parsing",
38123813
"rustc_data_structures",
38133814
"rustc_errors",
38143815
"rustc_fluent_macro",

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,44 +70,32 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
7070
}
7171
}
7272

73-
pub(super) fn lower_node(&mut self, def_id: LocalDefId) -> hir::MaybeOwner<'hir> {
73+
pub(super) fn lower_node(&mut self, def_id: LocalDefId) {
7474
let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
7575
if let hir::MaybeOwner::Phantom = owner {
7676
let node = self.ast_index[def_id];
7777
match node {
7878
AstOwner::NonOwner => {}
79-
AstOwner::Crate(c) => self.lower_crate(c),
80-
AstOwner::Item(item) => self.lower_item(item),
81-
AstOwner::AssocItem(item, ctxt) => self.lower_assoc_item(item, ctxt),
82-
AstOwner::ForeignItem(item) => self.lower_foreign_item(item),
79+
AstOwner::Crate(c) => {
80+
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
81+
self.with_lctx(CRATE_NODE_ID, |lctx| {
82+
let module = lctx.lower_mod(&c.items, &c.spans);
83+
// FIXME(jdonszelman): is dummy span ever a problem here?
84+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
85+
hir::OwnerNode::Crate(module)
86+
})
87+
}
88+
AstOwner::Item(item) => {
89+
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
90+
}
91+
AstOwner::AssocItem(item, ctxt) => {
92+
self.with_lctx(item.id, |lctx| lctx.lower_assoc_item(item, ctxt))
93+
}
94+
AstOwner::ForeignItem(item) => self.with_lctx(item.id, |lctx| {
95+
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
96+
}),
8397
}
8498
}
85-
86-
self.owners[def_id]
87-
}
88-
89-
#[instrument(level = "debug", skip(self, c))]
90-
fn lower_crate(&mut self, c: &Crate) {
91-
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
92-
self.with_lctx(CRATE_NODE_ID, |lctx| {
93-
let module = lctx.lower_mod(&c.items, &c.spans);
94-
// FIXME(jdonszelman): is dummy span ever a problem here?
95-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
96-
hir::OwnerNode::Crate(module)
97-
})
98-
}
99-
100-
#[instrument(level = "debug", skip(self))]
101-
fn lower_item(&mut self, item: &Item) {
102-
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
103-
}
104-
105-
fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) {
106-
self.with_lctx(item.id, |lctx| lctx.lower_assoc_item(item, ctxt))
107-
}
108-
109-
fn lower_foreign_item(&mut self, item: &ForeignItem) {
110-
self.with_lctx(item.id, |lctx| hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item)))
11199
}
112100
}
113101

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,14 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
444444
tcx.definitions_untracked().def_index_count(),
445445
);
446446

447+
let mut lowerer = item::ItemLowerer {
448+
tcx,
449+
resolver: &mut resolver,
450+
ast_index: &ast_index,
451+
owners: &mut owners,
452+
};
447453
for def_id in ast_index.indices() {
448-
item::ItemLowerer {
449-
tcx,
450-
resolver: &mut resolver,
451-
ast_index: &ast_index,
452-
owners: &mut owners,
453-
}
454-
.lower_node(def_id);
454+
lowerer.lower_node(def_id);
455455
}
456456

457457
drop(ast_index);

compiler/rustc_attr_parsing/src/attributes/util.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,33 @@ pub fn is_builtin_attr(attr: &impl AttributeExt) -> bool {
2626
pub fn find_crate_name(attrs: &[impl AttributeExt]) -> Option<Symbol> {
2727
first_attr_value_str_by_name(attrs, sym::crate_name)
2828
}
29+
30+
pub fn is_doc_alias_attrs_contain_symbol<'tcx, T: AttributeExt + 'tcx>(
31+
attrs: impl Iterator<Item = &'tcx T>,
32+
symbol: Symbol,
33+
) -> bool {
34+
let doc_attrs = attrs.filter(|attr| attr.has_name(sym::doc));
35+
for attr in doc_attrs {
36+
let Some(values) = attr.meta_item_list() else {
37+
continue;
38+
};
39+
let alias_values = values.iter().filter(|v| v.has_name(sym::alias));
40+
for v in alias_values {
41+
if let Some(nested) = v.meta_item_list() {
42+
// #[doc(alias("foo", "bar"))]
43+
let mut iter = nested.iter().filter_map(|item| item.lit()).map(|item| item.symbol);
44+
if iter.any(|s| s == symbol) {
45+
return true;
46+
}
47+
} else if let Some(meta) = v.meta_item()
48+
&& let Some(lit) = meta.name_value_literal()
49+
{
50+
// #[doc(alias = "foo")]
51+
if lit.symbol == symbol {
52+
return true;
53+
}
54+
}
55+
}
56+
}
57+
false
58+
}

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ pub mod parser;
9090
mod session_diagnostics;
9191

9292
pub use attributes::cfg::*;
93-
pub use attributes::util::{find_crate_name, is_builtin_attr, parse_version};
93+
pub use attributes::util::{
94+
find_crate_name, is_builtin_attr, is_doc_alias_attrs_contain_symbol, parse_version,
95+
};
9496
pub use context::{AttributeParser, OmitDoc};
9597

9698
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ pub trait Machine<'tcx>: Sized {
147147
/// already been checked before.
148148
const ALL_CONSTS_ARE_PRECHECKED: bool = true;
149149

150+
/// Determines whether rustc_const_eval functions that make use of the [Machine] should make
151+
/// tracing calls (to the `tracing` library). By default this is `false`, meaning the tracing
152+
/// calls will supposedly be optimized out. This flag is set to `true` inside Miri, to allow
153+
/// tracing the interpretation steps, among other things.
154+
const TRACING_ENABLED: bool = false;
155+
150156
/// Whether memory accesses should be alignment-checked.
151157
fn enforce_alignment(ecx: &InterpCx<'tcx, Self>) -> bool;
152158

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ pub(crate) fn create_static_alloc<'tcx>(
4545
assert!(ecx.memory.alloc_map.insert(alloc_id, (MemoryKind::Stack, alloc)).is_none());
4646
interp_ok(ecx.ptr_to_mplace(Pointer::from(alloc_id).into(), layout))
4747
}
48+
49+
/// This struct is needed to enforce `#[must_use]` on [tracing::span::EnteredSpan]
50+
/// while wrapping them in an `Option`.
51+
#[must_use]
52+
pub enum MaybeEnteredSpan {
53+
Some(tracing::span::EnteredSpan),
54+
None,
55+
}
56+
57+
#[macro_export]
58+
macro_rules! enter_trace_span {
59+
($machine:ident, $($tt:tt)*) => {
60+
if $machine::TRACING_ENABLED {
61+
$crate::interpret::tracing_utils::MaybeEnteredSpan::Some(tracing::info_span!($($tt)*).entered())
62+
} else {
63+
$crate::interpret::tracing_utils::MaybeEnteredSpan::None
64+
}
65+
}
66+
}

compiler/rustc_hir_typeck/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ itertools = "0.12"
99
rustc_abi = { path = "../rustc_abi" }
1010
rustc_ast = { path = "../rustc_ast" }
1111
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }
12+
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1213
rustc_data_structures = { path = "../rustc_data_structures" }
1314
rustc_errors = { path = "../rustc_errors" }
1415
rustc_fluent_macro = { path = "../rustc_fluent_macro" }

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8484

8585
self.annotate_expected_due_to_let_ty(err, expr, error);
8686
self.annotate_loop_expected_due_to_inference(err, expr, error);
87-
if self.annotate_mut_binding_to_immutable_binding(err, expr, error) {
87+
if self.annotate_mut_binding_to_immutable_binding(err, expr, expr_ty, expected, error) {
8888
return;
8989
}
9090

@@ -799,17 +799,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
799799
/// Detect the following case
800800
///
801801
/// ```text
802-
/// fn change_object(mut a: &Ty) {
802+
/// fn change_object(mut b: &Ty) {
803803
/// let a = Ty::new();
804804
/// b = a;
805805
/// }
806806
/// ```
807807
///
808-
/// where the user likely meant to modify the value behind there reference, use `a` as an out
808+
/// where the user likely meant to modify the value behind there reference, use `b` as an out
809809
/// parameter, instead of mutating the local binding. When encountering this we suggest:
810810
///
811811
/// ```text
812-
/// fn change_object(a: &'_ mut Ty) {
812+
/// fn change_object(b: &'_ mut Ty) {
813813
/// let a = Ty::new();
814814
/// *b = a;
815815
/// }
@@ -818,13 +818,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
818818
&self,
819819
err: &mut Diag<'_>,
820820
expr: &hir::Expr<'_>,
821+
expr_ty: Ty<'tcx>,
822+
expected: Ty<'tcx>,
821823
error: Option<TypeError<'tcx>>,
822824
) -> bool {
823-
if let Some(TypeError::Sorts(ExpectedFound { expected, found })) = error
825+
if let Some(TypeError::Sorts(ExpectedFound { .. })) = error
824826
&& let ty::Ref(_, inner, hir::Mutability::Not) = expected.kind()
825827

826828
// The difference between the expected and found values is one level of borrowing.
827-
&& self.can_eq(self.param_env, *inner, found)
829+
&& self.can_eq(self.param_env, *inner, expr_ty)
828830

829831
// We have an `ident = expr;` assignment.
830832
&& let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Assign(lhs, rhs, _), .. }) =

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
532532
ExprKind::Break(destination, ref expr_opt) => {
533533
self.check_expr_break(destination, expr_opt.as_deref(), expr)
534534
}
535-
ExprKind::Continue(destination) => {
536-
if destination.target_id.is_ok() {
537-
tcx.types.never
538-
} else {
539-
// There was an error; make type-check fail.
540-
Ty::new_misc_error(tcx)
541-
}
542-
}
535+
ExprKind::Continue(destination) => self.check_expr_continue(destination, expr),
543536
ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr),
544537
ExprKind::Become(call) => self.check_expr_become(call, expr),
545538
ExprKind::Let(let_expr) => self.check_expr_let(let_expr, expr.hir_id),
@@ -989,6 +982,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
989982
}
990983
}
991984

985+
fn check_expr_continue(
986+
&self,
987+
destination: hir::Destination,
988+
expr: &'tcx hir::Expr<'tcx>,
989+
) -> Ty<'tcx> {
990+
if let Ok(target_id) = destination.target_id {
991+
if let hir::Node::Expr(hir::Expr { kind: ExprKind::Loop(..), .. }) =
992+
self.tcx.hir_node(target_id)
993+
{
994+
self.tcx.types.never
995+
} else {
996+
// Liveness linting assumes `continue`s all point to loops. We'll report an error
997+
// in `check_mod_loops`, but make sure we don't run liveness (#113379, #121623).
998+
let guar = self.dcx().span_delayed_bug(
999+
expr.span,
1000+
"found `continue` not pointing to loop, but no error reported",
1001+
);
1002+
Ty::new_error(self.tcx, guar)
1003+
}
1004+
} else {
1005+
// There was an error; make type-check fail.
1006+
Ty::new_misc_error(self.tcx)
1007+
}
1008+
}
1009+
9921010
fn check_expr_return(
9931011
&self,
9941012
expr_opt: Option<&'tcx hir::Expr<'tcx>>,

compiler/rustc_hir_typeck/src/gather_locals.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,12 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
218218
);
219219
}
220220
let old_outermost_fn_param_pat = self.outermost_fn_param_pat.take();
221-
intravisit::walk_pat(self, p);
221+
if let PatKind::Guard(subpat, _) = p.kind {
222+
// We'll visit the guard when checking it. Don't gather its locals twice.
223+
self.visit_pat(subpat);
224+
} else {
225+
intravisit::walk_pat(self, p);
226+
}
222227
self.outermost_fn_param_pat = old_outermost_fn_param_pat;
223228
}
224229

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::cell::{Cell, RefCell};
22
use std::cmp::max;
33
use std::ops::Deref;
44

5+
use rustc_attr_parsing::is_doc_alias_attrs_contain_symbol;
56
use rustc_data_structures::fx::FxHashSet;
67
use rustc_data_structures::sso::SsoHashSet;
78
use rustc_errors::Applicability;
@@ -2333,10 +2334,13 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
23332334
};
23342335
let hir_id = self.fcx.tcx.local_def_id_to_hir_id(local_def_id);
23352336
let attrs = self.fcx.tcx.hir_attrs(hir_id);
2337+
2338+
if is_doc_alias_attrs_contain_symbol(attrs.into_iter(), method.name) {
2339+
return true;
2340+
}
2341+
23362342
for attr in attrs {
2337-
if attr.has_name(sym::doc) {
2338-
// do nothing
2339-
} else if attr.has_name(sym::rustc_confusables) {
2343+
if attr.has_name(sym::rustc_confusables) {
23402344
let Some(confusables) = attr.meta_item_list() else {
23412345
continue;
23422346
};
@@ -2348,33 +2352,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
23482352
return true;
23492353
}
23502354
}
2351-
continue;
2352-
} else {
2353-
continue;
2354-
};
2355-
let Some(values) = attr.meta_item_list() else {
2356-
continue;
2357-
};
2358-
for v in values {
2359-
if !v.has_name(sym::alias) {
2360-
continue;
2361-
}
2362-
if let Some(nested) = v.meta_item_list() {
2363-
// #[doc(alias("foo", "bar"))]
2364-
for n in nested {
2365-
if let Some(lit) = n.lit()
2366-
&& method.name == lit.symbol
2367-
{
2368-
return true;
2369-
}
2370-
}
2371-
} else if let Some(meta) = v.meta_item()
2372-
&& let Some(lit) = meta.name_value_literal()
2373-
&& method.name == lit.symbol
2374-
{
2375-
// #[doc(alias = "foo")]
2376-
return true;
2377-
}
23782355
}
23792356
}
23802357
false

0 commit comments

Comments
 (0)