977 – Expressions inside a struct or array initializer get wrong line number

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 977 - Expressions inside a struct or array initializer get wrong line number
Summary: Expressions inside a struct or array initializer get wrong line number
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 major
Assignee: No Owner
URL:
Keywords: diagnostic
: 3994 (view as issue list)
Depends on: 3994
Blocks:
Show dependency tree / graph
Reported: 2007年02月17日 09:04 UTC by Matti Niemenmaa
Modified: 2015年06月09日 05:10 UTC (History)
5 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Matti Niemenmaa 2007年02月17日 09:04:50 UTC
struct S {
	int[] x;
}
S s =
{
	[
		1, 2, 3,
		4, 5, 6
		7, 8, 9 // error line number should be here (line 12)
	]
} // but is here (line 15)
;
If an array is initialized directly and there's no struct initializer, the line number is reported correctly.
Comment 1 yebblies 2012年02月01日 18:58:42 UTC
*** Issue 3994 has been marked as a duplicate of this issue. ***
Comment 2 yebblies 2012年02月01日 19:02:38 UTC
This happens because the parser scans across the whole initializer to determine if it's an expression or an initializer. Because it's already lexed the whole initializer, the lexer's loc points to the end, and this is what is assigned to each expression. Not sure how to solve this without re-lexing the initializer.
Comment 3 Stewart Gordon 2012年02月02日 12:04:33 UTC
(In reply to comment #2)
> This happens because the parser scans across the whole initializer to determine
> if it's an expression or an initializer. Because it's already lexed the whole
> initializer, the lexer's loc points to the end, and this is what is assigned to
> each expression. Not sure how to solve this without re-lexing the initializer.
But it manages to give line numbers for errors that occur in the semantic analysis phase. The D compiler first lexes/parses the entire source file, then semantically analyses it. So it should be able to get this right.
Two ways I can see to do it:
- Remember the line number of every token just in case
- Realise on hitting the 7 that it is no longer parseable as either an expression or an initialiser, and deliver the error there and then.
Comment 4 yebblies 2012年02月02日 17:47:13 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > This happens because the parser scans across the whole initializer to determine
> > if it's an expression or an initializer. Because it's already lexed the whole
> > initializer, the lexer's loc points to the end, and this is what is assigned to
> > each expression. Not sure how to solve this without re-lexing the initializer.
> 
> But it manages to give line numbers for errors that occur in the semantic
> analysis phase. The D compiler first lexes/parses the entire source file, then
> semantically analyses it. So it should be able to get this right.
> 
It lexes while it parses, so when the parser creates the ast it can use the current location of the lexer instead of storing in the tokens. This is very efficient and works everywhere except when arbitrary lookahead is required.
> Two ways I can see to do it:
> - Remember the line number of every token just in case
As a last resort. This would likely be a performance hit, and complicate the parser code in most places for no benefit.
> - Realise on hitting the 7 that it is no longer parseable as either an
> expression or an initialiser, and deliver the error there and then.
It would have to be parsing to find this error, while it scans it's only lexing.
The relevant code is at 3185 and 3272 in parse.c
The solution is probably to initially parse it one way and convert if wrong, preserving location information, but this might not be possible.
Comment 5 ponce 2012年03月05日 17:03:12 UTC
My patch proposal:
https://github.com/p0nce/dmd/commit/bedcd6e7dc43087afdf816cf00debba03aafd400 
Comment 6 github-bugzilla 2012年03月07日 19:27:09 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd
https://github.com/D-Programming-Language/dmd/commit/dc8b6ea10b4e76cddc0e13119443e286cff6ba6c
Merge pull request #790 from p0nce/master
Bug 977
Comment 7 github-bugzilla 2012年03月07日 21:17:45 UTC
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd
https://github.com/D-Programming-Language/dmd/commit/6f9e42c84fc1d4fdd91d2387fe5bbdf549e8b018
fix Issue 977 - Expressions inside a struct or array initializer get wrong line number


AltStyle によって変換されたページ (->オリジナル) /