Skip to content

Support CallerArgumentExpression (without #line) #17519

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

Open
wants to merge 88 commits into
base: main
Choose a base branch
from

Conversation

ijklam
Copy link
Contributor

@ijklam ijklam commented Aug 11, 2024

Description

Implements fsharp/fslang-suggestions#966

RFC fsharp/fslang-design#798

图片

Checklist

  • Test cases added
  • Performance benchmarks added in case of performance changes
  • Release notes entry updated:

Copy link
Contributor

github-actions bot commented Aug 11, 2024

❗ Release notes required


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/9.0.300.md
LanguageFeatures.fsi docs/release-notes/.Language/preview.md

@ijklam
Copy link
Contributor Author

ijklam commented Aug 11, 2024

Here I encounter a problem about #line. For example, the code is saved in D:\Program.fs:

#1 "C:\\Program.fs"
System.ArgumentNullException.ThrowIfNullOrWhiteSpace("   ")  // will failed to build

// And more complicated case, repeat the file name and line number
#1 "C:\\Program.fs"
System.ArgumentNullException.ThrowIfNullOrWhiteSpace("   ")  // will failed to build

So here I want to get the original range instead of a range modified by #line . Is there anybody know how to do it?

| _ -> String.Empty
}

let getCodeText (m: range) =
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder whether it would be possible to use ISourceText.GetSubTextString instead of all of this. I think the ISourceText for a given file will usually already be cached when this functionality is needed.

I think in theory the source text is available on cenv.tcSink.CurrentSink.Value.CurrentSourceText.Value, but maybe there's a better way to get it, or a better way to bring it in scope for this change.

Copy link
Contributor Author

@ijklam ijklam Aug 25, 2024

Choose a reason for hiding this comment

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

图片

It seems that this cannot work under dotnet fsi and under dotnet build. It is always None.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it would make sense to pass it in, then, as is done when checking format strings:

type FormatStringCheckContext =
{ SourceText: ISourceText
LineStartPositions: int[] }

let makeFmts (context: FormatStringCheckContext) (fragRanges: range list) (fmt: string) =
// Splits the string on interpolation holes based on fragment ranges.
// Returns a list of tuples in the form of: offset * fragment as a string * original range of the fragment
// where "offset" is the offset between beginning of the original range and where the string content begins
let numFrags = fragRanges.Length
let sourceText = context.SourceText

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it would make sense to pass it in, then, as is done when checking format strings:

I guess maybe that's the same source text as in the sink... But still, maybe there's somewhere else we could put it.

@@ -581,3 +581,83 @@ module Range =
| None -> mkRange file (mkPos 1 0) (mkPos 1 80)
with _ ->
mkRange file (mkPos 1 0) (mkPos 1 80)

module internal FileContent =
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel uneasy with this module.

  • It adds a global resource for a rather local feature.
  • It creates another dependency on the file system.
  • It re-reads sources from the file system that have been read before.

Getting the source text out of the checking environment as proposed by @brianrourkeboll would be much cleaner.
A (probably too ambitious) alternative would be to make this a central place for source access, to give ISourceText the role that it was meant to have and to remove file system access elsewhere.

@ijklam
Copy link
Contributor Author

ijklam commented Apr 19, 2025

Hi there, I've make some efforts to make it get the source code from the LexBuffer and pass it to the cenv. Now it won't read files twice, and some parser functions in ParseAndCheckInputs.fs now return ParsedInput * ISourceText option, some checker functions also need a ISourceText option as input.
I created a new field cenv.SourceText: ISourceText option besides the cenv.tcSink.CurrentSink.CurrentSourceText to store the source content, as the cenv.tcSink is seems to be used only in language service scenario.

@edgarfgp edgarfgp requested a review from T-Gro April 28, 2025 10:44
@@ -325,6 +325,9 @@ and [<Sealed>] internal LexBuffer<'Char>
with get () = bufferAcceptAction
and set v = bufferAcceptAction <- v

member val private SourceTextBuilder = System.Text.StringBuilder()
Copy link
Member

Choose a reason for hiding this comment

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

Put data to rest of the fields above of this type.
The style here is - declaration, fields, private let functions, then members.

Make it obvious by design that the StringBuilder is being cleared when appropriate (where does buffer cleanup happen now when lexes moves to a different file?)

@@ -325,6 +325,9 @@ and [<Sealed>] internal LexBuffer<'Char>
with get () = bufferAcceptAction
and set v = bufferAcceptAction <- v

member val private SourceTextBuilder = System.Text.StringBuilder()
member lexbuf.SourceText = lexbuf.SourceTextBuilder.ToString() |> SourceText.ofString

Copy link
Member

Choose a reason for hiding this comment

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

Consider either pooling of the StringBuilder instances or a starting buffer that would reflect average size of an F# source file (the buffer above uses 4096 for example)

// F# view of attributes (these get converted to AbsIL attributes in ilxgen)
let IsMatchingFSharpAttribute g (AttribInfo(_, tcref)) (Attrib(tcref2, _, _, _, _, _, _)) = tyconRefEq g tcref tcref2
let HasFSharpAttribute g tref attrs = List.exists (IsMatchingFSharpAttribute g tref) attrs
let TryFindFSharpAttribute g tref attrs = List.tryFind (IsMatchingFSharpAttribute g tref) attrs
let TryFindFSharpAttributeOpt g tref attrs = match tref with None -> None | Some tref -> List.tryFind (IsMatchingFSharpAttribute g tref) attrs

let TryFindFSharpAttributeByName nm attrs =
Copy link
Member

Choose a reason for hiding this comment

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

Consider: Existing mechanism via TryFindFSharpAttribute and having a reference to the attribute in TcGlobals, i.e. work on strongly typed referenced to attribute definitions and not on strings.

@@ -3513,12 +3513,25 @@ let HasILAttribute tref (attrs: ILAttributes) =
let TryDecodeILAttribute tref (attrs: ILAttributes) =
attrs.AsArray() |> Array.tryPick (fun x -> if isILAttrib tref x then Some(decodeILAttribData x) else None)

let TryDecodeILAttributeByName nm (attrs: ILAttributes) =
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't approach like this one work here:

let compiledNameAttrib = TryFindFSharpStringAttribute g g.attrib_CompiledNameAttribute attrs


module CustomAttributes_CallerArgumentExpression =

[<FactForNETCOREAPP>]
Copy link
Member

Choose a reason for hiding this comment

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

Is there a particular reason for all added tests being FactForNETCOREAPP and not Fact that would also test Desktop framework?

Comment on lines +99 to +118
A.B("abc"
#line 1
: string)
|> assertEqual "\"abc\"
#line 1
: string"


A.B((+) 1
#line 1
123)
|> assertEqual "(+) 1
#line 1
123"


A.B(#line 1
(+) 1
123)
|> assertEqual "(+) 1
Copy link
Member

Choose a reason for hiding this comment

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

For future maintainers, I think this test setup needs more explanation of the context.

open FSharp.Test.Compiler
open FSharp.Test

module CustomAttributes_CallerArgumentExpression =
Copy link
Member

Choose a reason for hiding this comment

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

Did you consider a de-generate scenario of a huge file being a single expression (e.g. a massive collection builder, or a massive async block) passed into a single CallerArgumentExpressionAttribute member?

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

Successfully merging this pull request may close these issues.

5 participants