Skip to content

Account for use of try!() in 2018 edition and guide users in the right direction #71155

Closed
@estebank

Description

@estebank

Old documentation and blogposts will never be updated to not talk about the try macro. The current output of the following code

fn foo() -> Result<(), ()> {
    Ok(try!(Ok(())))
    
}

is

error: expected expression, found reserved keyword `try`
 --> src/main.rs:2:8
  |
2 |     Ok(try!(Ok(())))
  |        ^^^ expected expression

It should provide structured suggestions to either use r#try or ?:

error: use of deprecated `try` macro
 --> src/main.rs:2:8
  |
2 |     Ok(try!(Ok(())))
  |        ^^^^^      ^
  |        |
  |        deprecated `try` macro
  = note: in the 2018 edition `try` is a reserved keyword, and the `try!()` macro is deprecated
help: you can use the `?` operator instead
  |
2 |     Ok(Ok(())?))
  |        --      ^
help: alternatively, you can still access the deprecated `try!()` macro using the "raw identifier" syntax
  |
2 |     Ok(r#try!(Ok(())))
  |        ^^

This request is similar in spirit to #67204 and feels like a subset of #65148.

This issue has been assigned to @Duddino via this comment.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-parserArea: The lexing & parsing of Rust source code to an AST
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
F-try_blocks`#![feature(try_blocks)]`
D-papercutDiagnostics: An error or lint that needs small tweaks.
D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
D-editionDiagnostics: An error or lint that should account for edition differences.
on Apr 14, 2020
added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Apr 15, 2020
Duddino

Duddino commented on Apr 16, 2020

@Duddino

@rustbot claim

self-assigned this
on Apr 16, 2020
Duddino

Duddino commented on Apr 16, 2020

@Duddino

Would it be fine if I modified

pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a> {
let (span, msg) = match (&self.token.kind, self.subparser_name) {
(&token::Eof, Some(origin)) => {
let sp = self.sess.source_map().next_point(self.token.span);
(sp, format!("expected expression, found end of {}", origin))
}
_ => (
self.token.span,
format!("expected expression, found {}", super::token_descr(&self.token),),
),
};
let mut err = self.struct_span_err(span, &msg);
let sp = self.sess.source_map().start_point(self.token.span);
if let Some(sp) = self.sess.ambiguous_block_expr_parse.borrow().get(&sp) {
self.sess.expr_parentheses_needed(&mut err, *sp, None);
}
err.span_label(span, "expected expression");
err
}
and check if the try keyword is used, or is there another way to tackle this issue

added a commit that references this issue on Apr 17, 2020

Rollup merge of rust-lang#71243 - Duddino:Fix2, r=estebank

4b9eeca
added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
and removed on Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-edition-2018Area: The 2018 editionA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-editionDiagnostics: An error or lint that should account for edition differences.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.F-try_blocks`#![feature(try_blocks)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @estebank@fmease@JohnTitor@Duddino@rustbot

    Issue actions

      Account for use of `try!()` in 2018 edition and guide users in the right direction · Issue #71155 · rust-lang/rust