Skip to content

Commit a96e21b

Browse files
committed
Remove QPath::LangItem from try
1 parent bd13c30 commit a96e21b

File tree

12 files changed

+49
-89
lines changed

12 files changed

+49
-89
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
609609
expr: &'hir hir::Expr<'hir>,
610610
overall_span: Span,
611611
) -> &'hir hir::Expr<'hir> {
612-
let constructor = self.arena.alloc(self.expr_lang_item_qpath(method_span, lang_item));
612+
let constructor = self.arena.alloc(self.expr_lang_item_path(method_span, lang_item));
613613
self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
614614
}
615615

@@ -1950,7 +1950,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19501950
// expand <expr>
19511951
let sub_expr = self.lower_expr_mut(sub_expr);
19521952

1953-
self.expr_call_lang_item_qpath_fn(
1953+
self.expr_call_lang_item_fn(
19541954
unstable_span,
19551955
hir::LangItem::TryTraitBranch,
19561956
arena_vec![self; sub_expr],

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
545545
ExprKind::AddrOf(kind, mutbl, oprnd) => {
546546
self.check_expr_addr_of(kind, mutbl, oprnd, expected, expr)
547547
}
548-
ExprKind::Path(QPath::LangItem(lang_item, _)) => {
549-
self.check_lang_item_path(lang_item, expr)
550-
}
548+
ExprKind::Path(QPath::LangItem(..)) => unreachable!(),
551549
ExprKind::Path(ref qpath) => self.check_expr_path(qpath, expr, None),
552550
ExprKind::InlineAsm(asm) => {
553551
// We defer some asm checks as we may not have resolved the input and output types yet (they may still be infer vars).
@@ -748,14 +746,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
748746
}
749747
}
750748

751-
fn check_lang_item_path(
752-
&self,
753-
lang_item: hir::LangItem,
754-
expr: &'tcx hir::Expr<'tcx>,
755-
) -> Ty<'tcx> {
756-
self.resolve_lang_item_path(lang_item, expr.span, expr.hir_id).1
757-
}
758-
759749
pub(crate) fn check_expr_path(
760750
&self,
761751
qpath: &'tcx hir::QPath<'tcx>,
@@ -780,6 +770,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
780770
{
781771
Some(ObligationCauseCode::ForLoopIterator)
782772
}
773+
LangItem::TryTraitFromOutput
774+
if expr.span.is_desugaring(DesugaringKind::TryBlock) =>
775+
{
776+
// FIXME it's a try block, not a question mark
777+
Some(ObligationCauseCode::QuestionMark)
778+
}
779+
LangItem::TryTraitBranch | LangItem::TryTraitFromResidual
780+
if expr.span.is_desugaring(DesugaringKind::QuestionMark) =>
781+
{
782+
Some(ObligationCauseCode::QuestionMark)
783+
}
783784
_ => None,
784785
};
785786
if let Some(code) = code {

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -691,41 +691,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
691691
vec![ty_error; len]
692692
}
693693

694-
pub(crate) fn resolve_lang_item_path(
695-
&self,
696-
lang_item: hir::LangItem,
697-
span: Span,
698-
hir_id: HirId,
699-
) -> (Res, Ty<'tcx>) {
700-
let def_id = self.tcx.require_lang_item(lang_item, span);
701-
let def_kind = self.tcx.def_kind(def_id);
702-
703-
let item_ty = if let DefKind::Variant = def_kind {
704-
self.tcx.type_of(self.tcx.parent(def_id))
705-
} else {
706-
self.tcx.type_of(def_id)
707-
};
708-
let args = self.fresh_args_for_item(span, def_id);
709-
let ty = item_ty.instantiate(self.tcx, args);
710-
711-
self.write_args(hir_id, args);
712-
self.write_resolution(hir_id, Ok((def_kind, def_id)));
713-
714-
let code = match lang_item {
715-
hir::LangItem::TryTraitFromOutput
716-
| hir::LangItem::TryTraitFromResidual
717-
| hir::LangItem::TryTraitBranch => Some(ObligationCauseCode::QuestionMark),
718-
_ => None,
719-
};
720-
if let Some(code) = code {
721-
self.add_required_obligations_with_code(span, def_id, args, move |_, _| code.clone());
722-
} else {
723-
self.add_required_obligations_for_hir(span, def_id, args, hir_id);
724-
}
725-
726-
(Res::Def(def_kind, def_id), ty)
727-
}
728-
729694
/// Resolves an associated value path into a base type and associated constant, or method
730695
/// resolution. The newly resolved definition is written into `type_dependent_defs`.
731696
#[instrument(level = "trace", skip(self), ret)]

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,10 +1291,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12911291

12921292
(result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)), ty)
12931293
}
1294-
QPath::LangItem(lang_item, span) => {
1295-
let (res, ty) = self.resolve_lang_item_path(lang_item, span, hir_id);
1296-
(res, LoweredTy::from_raw(self, path_span, ty))
1297-
}
1294+
QPath::LangItem(..) => unreachable!(),
12981295
}
12991296
}
13001297

src/tools/clippy/clippy_lints/src/matches/try_err.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::ty::option_arg_ty;
66
use rustc_errors::Applicability;
77
use rustc_hir::LangItem::ResultErr;
8-
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource, QPath};
8+
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource};
99
use rustc_lint::LateContext;
1010
use rustc_middle::ty::{self, Ty};
1111
use rustc_span::{hygiene, sym};
@@ -23,8 +23,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
2323
// val,
2424
// };
2525
if let ExprKind::Call(match_fun, [try_arg]) = scrutinee.kind
26-
&& let ExprKind::Path(ref match_fun_path) = match_fun.kind
27-
&& matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, ..))
26+
&& let ExprKind::Path(match_fun_path) = match_fun.kind
27+
&& cx.tcx.qpath_is_lang_item(match_fun_path, LangItem::TryTraitBranch)
2828
&& let ExprKind::Call(err_fun, [err_arg]) = try_arg.kind
2929
&& err_fun.res(cx).ctor_parent(cx).is_lang_item(cx, ResultErr)
3030
&& let Some(return_ty) = find_return_type(cx, &expr.kind)

src/tools/clippy/clippy_lints/src/methods/clone_on_copy.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_context;
33
use clippy_utils::ty::is_copy;
44
use rustc_errors::Applicability;
5-
use rustc_hir::{BindingMode, ByRef, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
5+
use rustc_hir::{BindingMode, ByRef, Expr, ExprKind, MatchSource, Node, PatKind};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty;
88
use rustc_middle::ty::adjustment::Adjust;
@@ -47,7 +47,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>)
4747
// ? is a Call, makes sure not to rec *x?, but rather (*x)?
4848
ExprKind::Call(hir_callee, [_]) => matches!(
4949
hir_callee.kind,
50-
ExprKind::Path(QPath::LangItem(rustc_hir::LangItem::TryTraitBranch, ..))
50+
ExprKind::Path(qpath)
51+
if cx.tcx.qpath_is_lang_item(qpath, rustc_hir::LangItem::TryTraitBranch)
5152
),
5253
ExprKind::MethodCall(_, self_arg, ..) if expr.hir_id == self_arg.hir_id => true,
5354
ExprKind::Match(_, _, MatchSource::TryDesugar(_) | MatchSource::AwaitDesugar)

src/tools/clippy/clippy_lints/src/methods/str_splitn.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use clippy_utils::{paths, sym};
99
use core::ops::ControlFlow;
1010
use rustc_errors::Applicability;
1111
use rustc_hir::{
12-
BindingMode, Expr, ExprKind, HirId, LangItem, LetStmt, MatchSource, Node, Pat, PatKind, QPath, Stmt, StmtKind,
12+
BindingMode, Expr, ExprKind, HirId, LangItem, LetStmt, MatchSource, Node, Pat, PatKind, Stmt, StmtKind,
1313
};
1414
use rustc_lint::LateContext;
1515
use rustc_middle::ty;
@@ -332,12 +332,12 @@ fn parse_iter_usage<'tcx>(
332332
let (unwrap_kind, span) = if let Some((_, Node::Expr(e))) = iter.next() {
333333
match e.kind {
334334
ExprKind::Call(
335-
Expr {
336-
kind: ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)),
335+
&Expr {
336+
kind: ExprKind::Path(qpath),
337337
..
338338
},
339339
[_],
340-
) => {
340+
) if cx.tcx.qpath_is_lang_item(qpath, LangItem::TryTraitBranch) => {
341341
let parent_span = e.span.parent_callsite().unwrap();
342342
if parent_span.ctxt() == ctxt {
343343
(Some(UnwrapKind::QuestionMark), parent_span)

src/tools/clippy/clippy_lints/src/needless_question_mark.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::res::MaybeQPath;
33
use rustc_errors::Applicability;
44
use rustc_hir::def::{DefKind, Res};
5-
use rustc_hir::{Block, Body, Expr, ExprKind, LangItem, MatchSource, QPath};
5+
use rustc_hir::{Block, Body, Expr, ExprKind, LangItem, MatchSource};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::declare_lint_pass;
88

@@ -105,7 +105,8 @@ fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
105105
}
106106
&& let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar(_)) = &arg.kind
107107
&& let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind
108-
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)) = &called.kind
108+
&& let ExprKind::Path(qpath) = called.kind
109+
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::TryTraitBranch)
109110
&& expr.span.eq_ctxt(inner_expr.span)
110111
&& let expr_ty = cx.typeck_results().expr_ty(expr)
111112
&& let inner_ty = cx.typeck_results().expr_ty(inner_expr)

src/tools/clippy/clippy_lints/src/question_mark.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,13 @@ impl QuestionMark {
530530
}
531531
}
532532

533-
fn is_try_block(bl: &Block<'_>) -> bool {
533+
fn is_try_block(cx: &LateContext<'_>, bl: &Block<'_>) -> bool {
534534
if let Some(expr) = bl.expr
535535
&& let ExprKind::Call(callee, [_]) = expr.kind
536+
&& let ExprKind::Path(qpath) = callee.kind
537+
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::TryTraitFromOutput)
536538
{
537-
callee.opt_lang_path() == Some(LangItem::TryTraitFromOutput)
539+
true
538540
} else {
539541
false
540542
}
@@ -590,8 +592,8 @@ impl<'tcx> LateLintPass<'tcx> for QuestionMark {
590592
}
591593
}
592594

593-
fn check_block(&mut self, _: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
594-
if is_try_block(block) {
595+
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
596+
if is_try_block(cx, block) {
595597
*self
596598
.try_block_depth_stack
597599
.last_mut()
@@ -607,8 +609,8 @@ impl<'tcx> LateLintPass<'tcx> for QuestionMark {
607609
self.try_block_depth_stack.pop();
608610
}
609611

610-
fn check_block_post(&mut self, _: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
611-
if is_try_block(block) {
612+
fn check_block_post(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
613+
if is_try_block(cx, block) {
612614
*self
613615
.try_block_depth_stack
614616
.last_mut()

src/tools/clippy/clippy_lints/src/returns/needless_return.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clippy_utils::{
77
use rustc_ast::MetaItemInner;
88
use rustc_errors::Applicability;
99
use rustc_hir::intravisit::FnKind;
10-
use rustc_hir::{Body, Expr, ExprKind, HirId, LangItem, MatchSource, QPath, StmtKind};
10+
use rustc_hir::{Body, Expr, ExprKind, HirId, LangItem, MatchSource, StmtKind};
1111
use rustc_lint::{LateContext, Level, LintContext};
1212
use rustc_middle::ty::{self, Ty};
1313
use rustc_span::{BytePos, Pos, Span};
@@ -134,7 +134,8 @@ fn check_final_expr<'tcx>(
134134
let replacement = if let Some(inner_expr) = inner {
135135
// if desugar of `do yeet`, don't lint
136136
if let ExprKind::Call(path_expr, [_]) = inner_expr.kind
137-
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitFromYeet, ..)) = path_expr.kind
137+
&& let ExprKind::Path(qpath) = path_expr.kind
138+
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::TryTraitFromYeet)
138139
{
139140
return;
140141
}

0 commit comments

Comments
 (0)