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

Parens: tweak sensitive indentation handling #16248

Merged
merged 9 commits into from
Nov 14, 2023

Conversation

brianrourkeboll
Copy link
Contributor

@brianrourkeboll brianrourkeboll commented Nov 9, 2023

Another followup to #16079.

  • Dedent by one space in certain scenarios.

There are certain cases where we must consider both the context outside of a pair of parentheses as well as the inside in order to reintegrate the inner construct into the outer context after removing the parentheses. That is, sometimes we cannot simply remove the parentheses in place, but we must instead shift the entire parenthesized multiline expression left by one space.

For example:

let _ =
    1,
    (true
     || false
     || true),
    1

must become

let _ =
    1,
    true
    || false
    || true,
    1

not

let _ =
    1,
     true
     || false
     || true,
    1

Or here's a real-world example from FSharp.Core:

ty.InvokeMember(
name,
(BindingFlags.GetProperty
||| BindingFlags.Instance
||| BindingFlags.Public
||| BindingFlags.NonPublic),
null,
obj,
[||],
CultureInfo.InvariantCulture
)

* There are certain scenarios where we must consider both the context
  outside of a pair of parentheses as well as the inside in order to
  reintegrate the inner construct into the outer context after removing
  the parentheses.
@brianrourkeboll brianrourkeboll requested a review from a team as a code owner November 9, 2023 20:00
Copy link
Member

@psfinaki psfinaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks!

@psfinaki psfinaki enabled auto-merge (squash) November 10, 2023 13:15
@brianrourkeboll
Copy link
Contributor Author

Hmm, I think I see one bug... Let me fix that.

@brianrourkeboll
Copy link
Contributor Author

Tricky, tricky:

sourceEvent.Add(fun args2 ->
(match lastArgs with
| None -> ()
| Some args1 -> ev.Trigger(args1, args2))
lastArgs <- Some args2)

@brianrourkeboll
Copy link
Contributor Author

Expect some more PRs after this one, unfortunately—running fix-all on FSharp.Core is surfacing some other oddities I hadn't thought of, like the fact that parens aren't needed after new in this:

type T (x, y) =
    new (x) = T (x, 3) // Can remove parens: `new x = T (x, 3)`.

or in this:

type T (x, y) =
    new (x, y, z) = T (x, y)
    new (x) = T (x, 3) // Can remove parens: `new x = T (x, 3)`.

but they are required if there's another constructor and it comes after:

type T (x, y) =
    new (x) = T (x, 3) // Cannot remove; removing the parens causes parse errors below.
    new (x, y, _z) = T (x, y)

Or the fact that this is fine:

type C = abstract M : unit -> unit
let _ = { new C with override _.M () = () } // I.e., `override _.M (())` can be simplified to `override _.M ()`

but this requires (()) for the override:

type C<'T> = abstract M : 'T -> unit
let _ = { new C<unit> with override _.M (()) = () }

Real example:

let StringBuilderPrintfEnv<'Result>(k, buf) =
{ new PrintfEnv<Text.StringBuilder, unit, 'Result>(buf) with
override _.Finish() : 'Result = k ()
override _.Write(s: string) = ignore(buf.Append s)
override _.WriteT(()) = () }

@psfinaki
Copy link
Member

We are totally ready for the followups :)
Yeah looks like you are facing more and more inconsistencies in the compiler... Feel free to create issues about those, we might do something with the parser to eliminate some root causes.

@psfinaki
Copy link
Member

@brianrourkeboll this LGTM - is there anything else you plan to add here?

@brianrourkeboll
Copy link
Contributor Author

brianrourkeboll commented Nov 14, 2023

@brianrourkeboll this LGTM - is there anything else you plan to add here?

@psfinaki Nope, this one should be all set.

@T-Gro T-Gro merged commit d16430f into dotnet:main Nov 14, 2023
24 checks passed
@brianrourkeboll brianrourkeboll deleted the parens-outer-sensitive-indentation branch November 14, 2023 13:45
@majocha
Copy link
Contributor

majocha commented Nov 15, 2023

It seems this test case started to fail at some point:

Apparently because it's not being caught by the diagnostics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants